You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TST,TYP: Fix a python 3.11 failure for the GenericAlias tests - #21543
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.
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.
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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #21526
Fixes (sort of...) a python 3.11 issue wherein the
npt._GenericAliasclass (not instance) can now raise when used as a type parameter, as typing expects an iterable for the__parameter__attribute but instead gets apropertydescriptor object.The fix consists of simply not using
npt._GenericAliasas 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.