Open francesco-ballarin opened 1 year ago
Hey @francesco-ballarin,
Thanks for opening an issue about this. :) You're right that numpy.typing
currently won't work. The problem is that numpy.typing
types are unfortunately not functional themselves:
>>> isinstance(1, npt.NDArray[int])
TypeError: isinstance() argument 2 cannot be a parameterized generic
They are type hints like objects from typing
, which won't of their own, but need additional support. @beartype seems to come very close, but unfortunately doesn't quite get it right:
>>> from beartype.door import is_bearable, TypeHint
>>> is_bearable(np.ones(1, int), npt.NDArray[int]) # Nice!
True
>>> TypeHint(npt.NDArray[int]) # Nice!
TypeHint(numpy.ndarray[typing.Any, numpy.dtype[int]])
>>> TypeHint(npt.NDArray[int]) == TypeHint(npt.NDArray[float]) # :(
TypeHint(npt.NDArray[int]) == TypeHint(npt.NDArray[float])
The reason that you're getting a beartype.roar.BeartypeDoorNonpepException
whereas I'm not might be due to a different Python version. (I ran the above using Python 3.9.) EDIT: This seems to be the case; see the issue linked below.
I've opened an issue on @beartype to see what @leycec thinks about this.
Hi @wesselb I am testing out https://github.com/wesselb/plum/pull/73 due to the improved numpy support. In the very first example there, you show how to dispatch based on shape and type. In my use cases, I am only interested in dispatching based on type. Since that is actually supported by
np.typing.NDArray
, I thought I could just use that, but that doesn't seem to be the case.I report a minimal example below, in case you want to have a look before the plum 2 release.
and the corresponding output
Thanks!