8000 BUG: Fix misuse of .names and .fields in various places (backport 14290 to 1.17) by eric-wieser · Pull Request #14339 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fix misuse of .names and .fields in various places (backport 14290 to 1.17) #14339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Aug 23, 2019

Conversation

eric-wieser
Copy link
Member
@eric-wieser eric-wieser commented Aug 23, 2019

Backport of gh-14290. All but the merge commit are identical.

This replaces some more uses of `bool(dt.fields)` and `bool(dt.names)` with `dt.names is not None`.

`dt.fields is not None` would have worked too, but checking `.names` is more prevalent elsewhere
Previously attempting to access a field of such an array (such as when printing it!) would result in `ValueError: Changing the dtype of a 0d array is only supported if the itemsize is unchanged`.
Previously this would fail with `ValueError: could not assign tuple of length 2 to structure with 3 fields.`, now it raises `NotImplementedError`.
Replacing empty tuples with `None` is a bad idea, and just results in an API that is hard to consume - especially since the behavior was never documented.

This affects `get_names`, `get_names_flat`, and `get_fieldstructure`.
In these instances the behavior isn't changed, since the for loop below acts like an if.
However, in general this is an antipattern that crashes on 0-field structured types, and is warned against in the docs.
If we remove instances of the antipattern, it will hopefully not reappear via copy-paste code.
Without this change, `np.dtype('V0')` and `np.dtype([])` produced types with the same name, which was misleading as they are different types.

This is mostly cosmetic.
Also adjust the code to more clearly indicate what actually happens.
The behavior is identical before and after this patch.
Previously passing `dtype=[], names=['a']` would append an extra field, even though `dtype=['a'], names=['b', 'c']` does not.
…mtxt

This only affects arrays with `dtype([])`, but also follows the recommended way to check for structured arrays in our docs
This check would fail on the structured type `np.dtype([])`.

No test, since I don't really understand mrecords
It's not clear that these have any visible effect, but we should be consistent with how we detect structured types.
@eric-wieser eric-wieser added the 08 - Backport Used to tag backport PRs label Aug 23, 2019
@eric-wieser eric-wieser added this to the 1.17.2 release milestone Aug 23, 2019
@charris charris merged commit 94a2142 into numpy:maintenance/1.17.x Aug 23, 2019
@eric-wieser eric-wieser deleted the fix-if-fields-1.17 branch September 14, 2019 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
00 - Bug 08 - Backport Used to tag backport PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0