Repository navigation
Preserve natural order of args in the union type #88818
Copy link
Copy link
Closed
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Activity
There are two issues related to the order of __args__ in typing.Union and the union type.
- Indexing typing.Union preserves the order of arguments, but it is not always true when use the | operator.
>>> A = typing.NewType('A', str) >>> typing.Union[typing.List[int], A] typing.Union[typing.List[int], A] >>> typing.Union[A, typing.List[int]] typing.Union[A, typing.List[int]] >>> typing.List[int] | A typing.Union[typing.List[int], A] >>> A | typing.List[int] typing.Union[typing.List[int], A]
The cause is errors in __ror__ implementations.
- There is a difference between deduplication algorithms for typing.Union and the union type.
>>> typing.Union[int, str, int] typing.Union[int, str] >>> int | str | int str | int
It is not particularly important, because the order of __args__ mostly affects only representation. But it is better to be consistent, and it is easy to fix these tiny issues.
Note that it does not make the order of __args__ deterministic in all cases. Due to using caching for typing.Union it can be not always preserved if we merger typing.Union objects which differs only by order of arguments.
>>> import typing >>> typing.Union[typing.Union[int, str], list] typing.Union[int, str, list] >>> typing.Union[typing.Union[str, int], list] typing.Union[int, str, list]
- added3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jul 16, 2021
Metadata
Metadata
Assignees
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: