MAINT: Fix various issues with the np.generic annotations#17214
MAINT: Fix various issues with the np.generic annotations#17214charris merged 11 commits intonumpy:masterfrom
np.generic annotations#17214Conversation
numpy/__init__.pyi
Outdated
| class int8(signedinteger): | ||
| def __init__(self, __value: SupportsInt = ...) -> None: ... | ||
| def __init__( | ||
| self, __value: Union[SupportsInt, SupportsIndex, str, bytes] = ... |
There was a problem hiding this comment.
np.numbers apparently also support objects with SupportsIndex protocol, just as their builtin counterparts:
https://github.com/python/typeshed/blob/ccfc1850e9b61bb891e45a679a55d7d654992b7c/stdlib/2and3/builtins.pyi#L182
|
This is odd, ExamplesSlimmed down example: >>> import numpy as np
>>> class D:
... def __index__(self) -> int:
... return 0
>>> np.complex64(D())
Traceback (most recent call last):
...
TypeError: must be real number, not D |
|
That looks like it's probably a bug, although I'm not quite clear enough on the semantics of |
|
Oh hang, I've found the issue: https://docs.python.org/3/whatsnew/3.8.html#other-language-changes (third bullet point) It seems support for the |
|
Sounds like we should change numpy to be consistent then, perhaps conditional on python >=3.8 |
|
Alright, c9bb44b should fix the python < 3.8 |
|
Thanks @BvB93 . |
This pull request fixes a number of things related to the annotations of various
np.genericsubclasses:np.str_andnp.float128.np.datetime64a subclass ofnp.generic(again), thus partially reverting Added typing information for datetime64, timedelta64 numpy-stubs#31 (comment).The issues encountered in the linked comment have been partially addressed by explicitly defining a few
__r<op>__methods. Once the, currently untyped, magic methods innp._ArrayOrScalarCommonhave been annotated then the remaining issues will likelly resolve themselves.np.complexfloatinggeneric w.r.t.np.floating#17172 this limits thenp.generic.realand.imagproperties to numericnp.genericsubclasses.On the side of caution I've also included
np.object_andnp.void, as the latter two may or may not contain numeric types (any thoughts on this?).