ramonhagenaars / nptyping

💡 Type hints for Numpy and Pandas
MIT License
593 stars 30 forks source link

beartype and typeguard don't like the NDArray repr #115

Open bnavigator opened 9 months ago

bnavigator commented 9 months ago

Testing with beartype 0.17 produces the following output:

[    8s] ======================================================================
[    8s] ERROR: tests.test_beartype (unittest.loader._FailedTest)
...
[    8s] beartype.roar.BeartypeDecorHintPep604Exception: Type hint NDArray[Shape['2, 2'], Float] inconsistent with respect to repr() strings. Since @beartype requires consistency between type hints and repr() strings, this hint is unsupported by @beartype. Consider reporting this issue to the third-party developer implementing this hint: e.g.,
[    8s]        >>> repr(NDArray[Shape['2, 2'], Float])
[    8s]        NDArray[Shape['2, 2'], Float]  # <-- this is fine
[    8s]        >>> repr(NDArray[Shape['2, 2'], Float] | int)
[    8s]        nptyping.ndarray.NDArray | int  # <-- *THIS IS REALLY SUPER BAD*
[    8s] 
[    8s]        # Ideally, that output should instead resemble:
[    8s]        >>> repr(NDArray[Shape['2, 2'], Float] | int)
[    8s]        NDArray[Shape['2, 2'], Float] | int  # <-- what @beartype wants!
bnavigator commented 9 months ago

And with typeguard:

[    6s] ======================================================================
[    6s] ERROR: test_success (tests.test_typeguard.TypeguardTest)
[    6s] ----------------------------------------------------------------------
[    6s] Traceback (most recent call last):
[    6s]   File "/home/abuild/rpmbuild/BUILD/nptyping-2.5.0/tests/test_typeguard.py", line 24, in test_success
[    6s]     fun(np.random.randn(2, 2))
[    6s]   File "/home/abuild/rpmbuild/BUILD/nptyping-2.5.0/tests/test_typeguard.py", line 14, in fun
[    6s]     def fun(_: NDArray[Shape["2, 2"], Float]) -> None:
[    6s]   File "/home/abuild/rpmbuild/BUILD/nptyping-2.5.0/nptyping/base_meta_classes.py", line 145, in __getitem__
[    6s]     args = cls._get_item(item)
[    6s]   File "/home/abuild/rpmbuild/BUILD/nptyping-2.5.0/nptyping/base_meta_classes.py", line 213, in _get_item
[    6s]     raise InvalidArgumentsError(
[    6s] nptyping.error.InvalidArgumentsError: Unexpected argument of type <class 'tuple'>, expecting a string.
[    6s] 
[    6s] ----------------------------------------------------------------------
Danguilhen commented 9 months ago

Hello, I have the same problem. Beartype is testing that PEP 604 is respected this way: repr(your_nptyping_type | int) == "your_nptyping_type | 1" The result is different because NDArrayMeta class doesn't have an or dunder method. Btw, the equivalent need to be done for for the DataFrame meta class I guess. I think that adding one would solve the issue. However, it looks like that nptyping is not maintained anymore, no pull request accepted or commit made since a year. It would be sad to loose such a great tool!