-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
ENH: Add annotations to ndarray
and generic
#17368
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
Comments
So it seems that annotating the comparison operations, The problem here is that A (simplified) example: from numpy.typing import ArrayLike
class ndarray:
def __eq__(self, value: ArrayLike) -> ndarray: ...
array = ndarray()
# The good
reveal_type(array == 1) # Revealed type is 'ndarray'
# The bad
reveal_type(1 == array) # Revealed type is 'builtins.bool' |
In case you're interested, I ran into this issue and it motivated me to make a proposal to python-ideas, which Guido suggest I post to typing-sig. Please feel free to weigh in here. |
Pushing off remaining tasks to 1.21. |
Ah, looks like the problem here is that What is the official Python stance on the type that According to this:
But then following the link on comparisons, we see:
So the documentation seems to contradict itself. Maybe we can try to clarify the official Python stance on what types If the official stance is that equality can return any type, the type system should probably be changed to reflect that. By the way, I know there are multiple static type checkers for python - do all of them exhibit the same behavior here? If the official stance is that they can only return boolean values, perhaps numpy could consider introducing a new function to handle the special cases, and change the comparison operators to conform to the spec. Though I'm sure that would be a disruptive breaking change, so I'm not sure how willing the numpy community would be to adopt that change in order to gain static typing. I wonder if a compromise solution would work in the short term. Maybe something like this? class ndarray:
def __eq__(self, value: ArrayLike) -> ndarray | bool: ... |
You can do the
While I expect some at python may have discontent about how NumPy does comparison operators, there is not much point to discuss it in my opinion. Long story short: I think this is clearly something that typing-sig must solve, there is nothing NumPy can do aside from trying to help with that process. |
There are a number of discussions on the
It's a question of whether or not we want to be incorrect 50% or ~100% of the time. In any case, the only way in see this issue getting "fixed" in the short term is via a mypy plugin, though this will be rather invasive as we'd have to check every single |
Uh oh!
There was an error while loading. Please reload this page.
1.20 will be the first numpy release featuring type hints and while the typing of numpy
is very much on ongoing process it would be great if we could get
ndarray
andgeneric
fully typed before its release.
TODO
ndarray
andgeneric
#17273).ndarray
/generic
non-magic methods #17372).ndarray
/generic
magic methods #17613, MAINT: Refine a number ofnp.generic
annotations #17673).The text was updated successfully, but these errors were encountered: