Author serhiy.storchaka
Recipients gvanrossum, kj, serhiy.storchaka
Date 2021-07-16.11:07:49
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
There are two issues related to the order of __args__ in typing.Union and the union type.

1. 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.

2. 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]
Date User Action Args
2021-07-16 11:07:49serhiy.storchakasetrecipients: + serhiy.storchaka, gvanrossum, kj
2021-07-16 11:07:49serhiy.storchakasetmessageid: <>
2021-07-16 11:07:49serhiy.storchakalinkissue44652 messages
2021-07-16 11:07:49serhiy.storchakacreate