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

Skip to content

BUG: Fix misuse of .names and .fields in various places #14290

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 22, 2019

Conversation

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

Easiest to review commit-by-commit.

Done with the 1.16.x branch point as its base, so it can be easily backported to either 1.16 or 1.17 if desired

@eric-wieser

This comment has been minimized.

@seberg seberg self-requested a review August 17, 2019 14:38
@pv
Copy link
Member
pv commented Aug 17, 2019

pytest 5.1.0 crashes on python3.5-dbg (not limited to numpy --- occurs on all projects I know), triggers some fatal assert in Python ast parser

@eric-wieser

This comment has been minimized.

@charris
Copy link
Member
charris commented Aug 18, 2019

close/reopen

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`.
@eric-wieser
Copy link
Member Author

Had to remove a change because it's test ran into gh-13438. I'll push the follow-up commit once this goes in, and leave it with a CI failure until we fix gh-13438.

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 marked this pull request as ready for review August 20, 2019 00:36
@ahaldane
Copy link
Member

LGTM. There are probably still things broken with zero-field dtypes, eg perhaps the view issue from #14310, but its good to fix all these.

@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Aug 21, 2019
@charris
Copy link
Member
charris commented Aug 21, 2019

@ahaldane Do you think this should go in before your fixes?

@charris charris added this to the 1.16.5 release milestone Aug 21, 2019
@eric-wieser
Copy link
Member Author

I don't think it matters, this doesn't overlap in its current form.

@charris
Copy link
Member
charris commented Aug 22, 2019

@eric-wieser If you are happy with this I'll go ahead and merge. Looks like it might close some issues, are they included in the commit messages?

@eric-wieser
Copy link
Member Author
eric-wieser commented Aug 22, 2019

I haven't found any issues this will close yet - they're deep enough corner cases they were probably never reported.

If you put this in (without squashing), I'll kick off backport PRs for 1.16 and 1.17 tonight (assuming you want both)

@charris
Copy link
Member
charris commented Aug 22, 2019

@seberg has marked this for his review, so I'll wait on that.

So tomorrow night. I figure if the bugs are corner cases, any bugs in the fixes are likely be corner cases too :)

Copy link
Member
@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, pretty deep corner case, I guess. Not too surprising that there are no issues about it...

EDIT: I think this is good for backport (not that it is likely that many users will actually stumble over it).

@charris charris merged commit cd4cda8 into numpy:master Aug 22, 2019
@charris
Copy link
Member
charris commented Aug 22, 2019

@eric-wieser Merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0