Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

TYP: allow None in operand sequence of nditer #28039

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 2 commits into from
Dec 20, 2024
Merged

TYP: allow None in operand sequence of nditer #28039

merged 2 commits into from
Dec 20, 2024

Conversation

simonaltrogge
Copy link
Contributor

Prevent type-hint errors when using nditer in an intended way (see https://numpy.org/doc/stable/reference/arrays.nditer.html#iterator-allocated-output-arrays).

Fix #28038

@simonaltrogge
Copy link
Contributor Author

I have the following concern regarding the way I have changed the type annotation:
Sequences only consisting of None elements are now valid according to the type hint, but correctly lead to the error TypeError: no arrays or types available to calculate result type. I am not sure, however, whether/how it is possible to type-hint that at least one element needs to be of type ArrayLike. Since passing an empty sequence is also valid according to the type hint but also leads to an error (ValueError: Must provide at least one operand), maybe this is a similar case...

I leave it up to the maintainers to decide if it is more important to not raise a type-checking error on intended usage or to raise an error when passing a Sequence[None]-type value as op to nditer.

@jorenham
Copy link
Member

Sequences only consisting of None elements are now valid according to the type hint

I'm afraid that's inevitable, as there's no way to express "a Sequence[ArrayLike | None] that has at least 1 element that isn't of type None".

Copy link
Member

@jorenham jorenham left a comment

Choose a reason for hiding this comment

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

In order to prevent the new None from accidentally being deleted in the future, it could help to add a quick type-test for this (in numpy/typing/tests/data, in either reveal or pass)

@charris charris added this to the 2.2.1 release milestone Dec 19, 2024
@simonaltrogge
Copy link
Contributor Author

I added such a type test in cf3dc6b based on the information I found in the testing readme of the archived numpy-stubs repository as I could not find any information in the numpy repository itself. If this information is still up to date, maybe it should be migrated to this repository as well?

@jorenham jorenham self-requested a review December 20, 2024 14:07
@jorenham
Copy link
Member

I added such a type test in cf3dc6b based on the information I found in the testing readme of the archived numpy-stubs repository as I could not find any information in the numpy repository itself. If this information is still up to date, maybe it should be migrated to this repository as well?

Yea that's a good point. There's a good chance we'll be overhauling the type-tests soon, so I suppose we'll take that into consideration once we do that :)

@jorenham jorenham merged commit 031a890 into numpy:main Dec 20, 2024
66 checks passed
@jorenham
Copy link
Member

Thanks, Simon!

@jorenham jorenham added the 09 - Backport-Candidate PRs tagged should be backported label Dec 20, 2024
@charris charris changed the title TYP: allow None in operand sequence of nditer TYP: allow None in operand sequence of nditer Dec 20, 2024
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Dec 20, 2024
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.

TYP: nditer does not allow None-valued elements in operand sequence
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.