classification
Title: Preserve natural order of args in the union type
Type: behavior Stage: resolved
Components: Interpreter Core, Library (Lib) Versions: Python 3.11, Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, kj, miss-islington, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2021-07-16 11:07 by serhiy.storchaka, last changed 2021-07-16 13:43 by kj. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 27185 merged serhiy.storchaka, 2021-07-16 11:10
PR 27190 merged serhiy.storchaka, 2021-07-16 13:19
Messages (3)
msg397612 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-07-16 11:07
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]
msg397618 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-07-16 13:11
New changeset 0cd2d51aadcd2a0c0739a5df0a6235d64f35619e by Serhiy Storchaka in branch 'main':
bpo-44652: Preserve natural order of args in the union type. (GH-27185)
https://github.com/python/cpython/commit/0cd2d51aadcd2a0c0739a5df0a6235d64f35619e
msg397628 - (view) Author: miss-islington (miss-islington) Date: 2021-07-16 13:42
New changeset 80844d1ebc03e1cf3ffdeb47751522499e90b0bc by Serhiy Storchaka in branch '3.10':
[3.10] bpo-44652: Preserve natural order of args in the union type. (GH-27185) (GH-27190)
https://github.com/python/cpython/commit/80844d1ebc03e1cf3ffdeb47751522499e90b0bc
History
Date User Action Args
2021-07-16 13:43:52kjsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-07-16 13:42:12miss-islingtonsetnosy: + miss-islington
messages: + msg397628
2021-07-16 13:19:08serhiy.storchakasetpull_requests: + pull_request25726
2021-07-16 13:11:41serhiy.storchakasetmessages: + msg397618
2021-07-16 11:10:37serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request25722
2021-07-16 11:07:49serhiy.storchakacreate