beartype icon indicating copy to clipboard operation
beartype copied to clipboard

[DOOR Feature Request] `TypeHint` Support For `numpy.typing`

Open wesselb opened this issue 1 year ago • 5 comments

Hey @leycec! I'm opening an issue to report an issue with TypeHint originally reported by @francesco-ballarin in this issue.

On Python 3.11, TypeHint appears to complain when it receives a type hint from numpy.typing:

>>> TypeHint(npt.NDArray[int])
BeartypeDoorNonpepException: Type hint numpy.ndarray[typing.Any, numpy.dtype[int]] invalid (i.e., either PEP-noncompliant or PEP-compliant but currently unsupported by "beartype.door.TypeHint").

On Python 3.9, TypeHint happily accepts a numpy.typing type hint, but the comparisons aren't quite right:

>>> TypeHint(npt.NDArray[int])  # Yay!
TypeHint(numpy.ndarray[typing.Any, numpy.dtype[int]]) 

>>> TypeHint(npt.NDArray[int]) == TypeHint(npt.NDArray[float])  # :(
True

I'm aware that non-typing type hints likely won't work out of the box and might need additional support by @beartype, but I thought that it would be helpful to report these issues nevertheless, since numpy.typing is widely used.

wesselb avatar Mar 06 '23 16:03 wesselb