Skip to content

Make sure that typing cache differentiate Union[int, str] and Union[str, int] #103749

Open
@sobolevn

Description

@sobolevn

Previous discussions:

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 :)

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions