10000 TST,TYP: Fix a python 3.11 failure for the `GenericAlias` tests by BvB93 · Pull Request #21543 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

TST,TYP: Fix a python 3.11 failure for the GenericAlias tests #21543

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

Merged
merged 2 commits into from
May 21, 2022

Conversation

BvB93
Copy link
Member
@BvB93 BvB93 commented May 20, 2022

Closes #21526

Fixes (sort of...) a python 3.11 issue wherein the npt._GenericAlias class (not instance) can now raise when used as a type parameter, as typing expects an iterable for the __parameter__ attribute but instead gets a property descriptor object.

The fix consists of simply not using npt._GenericAlias as a type parameter, and instead just stringify the expression. As the relevant class is never exposed in Python >=3.9 in the first place (and never will), I don't feel it's worth to implement a more rigorous fix.

@mattip
Copy link
Member
mattip commented May 20, 2022

As the relevant class is never exposed in Python >=3.9 in the first place

So could we avoid it altogether, or is it needed for some other obscure reason? If we cannot drop it now, but can drop FuncType once we drop support for 3.9, a comment would be nice.

@BvB93
Copy link
Member Author
BvB93 commented May 20, 2022

So could we avoid it altogether, or is it needed for some other obscure reason?

npt._GenericAlias is a python 3.8 backport of types.GenericAlias and the purpose of these tests is to verify that they share the same semantics. As the latter does not exist in 3.8 we have to run tests on later python versions if we want to directly compare the backport to its original.

Adding a comment is a good idea though, I'll add one in a bit.

@mattip mattip merged commit bcdecb3 into numpy:main May 21, 2022
@mattip
Copy link
Member
mattip commented May 21, 2022

Thanks @BvB93

@EwoutH
Copy link
Contributor
EwoutH commented May 23, 2022

Thanks a lot for this effort! While the previous error is now gone, it seems that test_generic_alias.py still doesn't pass in the CI with Python 3.11, generating the following failures:

Failure summary
=================================== FAILURES ===================================
_________________ TestGenericAlias.test_pass[__dir__-<lambda>] _________________

self = <numpy.typing.tests.test_generic_alias.TestGenericAlias object at 0x7f7475f1a810>
name = '__dir__', func = <function TestGenericAlias.<lambda> at 0x7f74850974c0>

    @pytest.mark.parametrize("name,func", [
        ("__init__", lambda n: n),
        ("__init__", lambda n: _GenericAlias(np.ndarray, Any)),
        ("__init__", lambda n: _GenericAlias(np.ndarray, (Any,))),
        ("__init__", lambda n: _GenericAlias(np.ndarray, (Any, Any))),
        ("__init__", lambda n: _GenericAlias(np.ndarray, T1)),
        ("__init__", lambda n: _GenericAlias(np.ndarray, (T1,))),
        ("__init__", lambda n: _GenericAlias(np.ndarray, (T1, T2))),
        ("__origin__", lambda n: n.__origin__),
        ("__args__", lambda n: n.__args__),
        ("__parameters__", lambda n: n.__parameters__),
        ("__reduce__", lambda n: n.__reduce__()[1:]),
        ("__reduce_ex__", lambda n: n.__reduce_ex__(1)[1:]),
        ("__mro_entries__", lambda n: n.__mro_entries__([object])),
        ("__hash__", lambda n: hash(n)),
        ("__repr__", lambda n: repr(n)),
        ("__getitem__", lambda n: n[np.float64]),
        ("__getitem__", lambda n: n[ScalarType][np.float64]),
        ("__getitem__", lambda n: n[Union[np.int64, ScalarType]][np.float64]),
        ("__getitem__", lambda n: n[Union[T1, T2]][np.float32, np.float64]),
        ("__eq__", lambda n: n == n),
        ("__ne__", lambda n: n != np.ndarray),
        ("__dir__", lambda n: dir(n)),
        ("__call__", lambda n: n((1,), np.int64, BUFFER)),
        ("__call__", lambda n: n(shape=(1,), dtype=np.int64, buffer=BUFFER)),
        ("subclassing", lambda n: _get_subclass_mro(n)),
        ("pickle", lambda n: n == pickle.loads(pickle.dumps(n))),
    ])
    def test_pass(self, name: str, func: FuncType) -> None:
        """Compare `types.GenericAlias` with its numpy-based backport.
    
        Checker whether ``func`` runs as intended and that both `GenericAlias`
        and `_GenericAlias` return the same result.
    
        """
        value = func(NDArray)
    
        if sys.version_info >= (3, 9):
            value_ref = func(NDArray_ref)
>           assert value == value_ref
E           AssertionError: assert ['T', '__abs_...array__', ...] == ['T', '__abs_...array__', ...]
E             At index 99 diff: '__xor__' != '__unpacked__'
E             Right contains one more item: 'view'
E             Use -v to get the full diff

func       = <function TestGenericAlias.<lambda> at 0x7f74850974c0>
name       = '__dir__'
self       = <numpy.typing.tests.test_generic_alias.TestGenericAlias object at 0x7f7475f1a810>
value      = ['T', '__abs__', '__add__', '__and__', '__args__', '__array__', ...]
value_ref  = ['T', '__abs__', '__add__', '__and__', '__args__', '__array__', ...]

../../builds/venv/lib/python3.11/site-packages/numpy-1.23.0.dev0+1280.ga2d2b5691-py3.11-linux-x86_64.egg/numpy/typing/tests/test_generic_alias.py:86: AssertionError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Python 3.11.0b1, test_generic_alias, TypeError: 'property' object is not iterable
3 participants
0