Open
Description
Previous discussions:
- gh-101859: Add caching of
types.GenericAlias
objects #103541 (comment) - https://discuss.python.org/t/changing-runtime-behavior-of-types-uniontype-to-respect-order-in-equality-and-hashing/26071
It was decided (@gvanrossum and @samuelcolvin) that there's not need to change __hash__
or __eq__
of Union
objects, we can just change how the typing cache works.
In other words, we need to change this behavior:
>>> from typing import List, Union
>>> List[Union[str, int]] is List[Union[str, int]]
True
>>> List[Union[str, int]] is List[Union[int, str]]
True
To be:
>>> from typing import List, Union
>>> List[Union[str, int]] is List[Union[str, int]] # still the same
True
>>> List[Union[str, int]] is List[Union[int, str]] # change, because order of `Union.__args__` changed
False
This way we can fix this confusing behavior:
>>> List[Union[str, int]].__args__
(typing.Union[str, int],)
>>> List[Union[int, str]].__args__ # order of args changes
(typing.Union[str, int],)
Instead it would be:
>>> List[Union[str, int]].__args__
(typing.Union[str, int],)
>>> List[Union[int, str]].__args__ # order is the same
(typing.Union[int, str],)
I am interested in working on this: typing.py
is my favourite module + I have a highly related opened PR with types.GenericAlias
cache.
If someone else is already working on it, please let me know :)