New submission from Serhiy Storchaka <[email protected]>:
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]
----------
components: Interpreter Core, Library (Lib)
messages: 397612
nosy: gvanrossum, kj, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Preserve natural order of args in the union type
type: behavior
versions: Python 3.10, Python 3.11
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue44652>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com