8000 [3.10] bpo-44652: Preserve natural order of args in the union type. (… · python/cpython@80844d1 · GitHub
[go: up one dir, main page]

Skip to content
/ cpython Public

Commit 80844d1

Browse files
[3.10] bpo-44652: Preserve natural order of args in the union type. (GH-27185) (GH-27190)
(cherry picked from commit 0cd2d51) Automerge-Triggered-By: GH:ambv
1 parent 948e39a commit 80844d1

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

Lib/test/test_types.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class Example:
1818

1919
class Forward: ...
2020

21+
def clear_typing_caches():
22+
for f in typing._cleanups:
23+
f()
24+
25+
2126
class TypesTests(unittest.TestCase):
2227

2328
def test_truth_values(self):
@@ -708,11 +713,34 @@ def test_or_type_operator_with_TypeVar(self):
708713
assert str | TV == typing.Union[str, TV]
709714

710715
def test_union_args(self):
711-
self.assertEqual((int | str).__args__, (int, str))
712-
self.assertEqual(((int | str) | list).__args__, (int, str, list))
713-
self.assertEqual((int | (str | list)).__args__, (int, str, list))
714-
self.assertEqual((int | None).__args__, (int, type(None)))
715-
self.assertEqual((int | type(None)).__args__, (int, type(None)))
716+
def check(arg, expected):
717+
clear_typing_caches()
718+
self.assertEqual(arg.__args__, expected)
719+
720+
check(int | str, (int, str))
721+
check((int | str) | list, (int, str, list))
722+
check(int | (str | list), (int, str, list))
723+
check((int | str) | int, (int, str))
724+
check(int | (str | int), (int, str))
725+
check((int | str) | (str | int), (int, str))
726+
check(typing.Union[int, str] | list, (int, str, list))
727+
check(int | typing.Union[str, list], (int, str, list))
728+
check((int | str) | (list | int), (int, str, list))
729+
check((int | str) | typing.Union[list, int], (int, str, list))
730+
check(typing.Union[int, str] | (list | int), (int, str, list))
731+
check((str | int) | (int | list), (str, int, list))
732+
check((str | int) | typing.Union[int, list], (str, int, list))
733+
check(typing.Union[str, int] | (int | list), (str, int, list))
734+
check(int | type(None), (int, type(None)))
735+
check(type(None) | int, (type(None), int))
736+
737+
args = (int, list[int], typing.List[int],
738+
typing.Tuple[int, int], typing.Callable[[int], int],
739+
typing.Hashable, typing.TypeVar('T'))
740+
for x in args:
741+
with self.subTest(x):
742+
check(x | None, (x, type(None)))
743+
check(None | x, (type(None), x))
716744

717745
def test_or_type_operator_with_forward(self):
718746
T = typing.TypeVar('T')

Lib/typing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -987,8 +987,8 @@ def __hash__(self):
987987
def __or__(self, right):
988988
return Union[self, right]
989989

990-
def __ror__(self, right):
991-
return Union[self, right]
990+
def __ror__(self, left):
991+
return Union[left, self]
992992

993993
@_tp_cache
994994
def __getitem__(self, params):
@@ -1098,8 +1098,8 @@ def __reduce__(self):
10981098
def __or__(self, right):
10991099
return Union[self, right]
11001100

1101-
def __ror__(self, right):
1102-
return Union[self, right]
1101+
def __ror__(self, left):
1102+
return Union[left, self]
11031103

11041104
class _CallableGenericAlias(_GenericAlias, _root=True):
11051105
def __repr__(self):

Objects/unionobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ dedup_and_flatten_args(PyObject* args)
257257
for (Py_ssize_t i = 0; i < arg_length; i++) {
258258
int is_duplicate = 0;
259259
PyObject* i_element = PyTuple_GET_ITEM(args, i);
260-
for (Py_ssize_t j = i + 1; j < arg_length; j++) {
261-
PyObject* j_element = PyTuple_GET_ITEM(args, j);
260+
for (Py_ssize_t j = 0; j < added_items; j++) {
261+
PyObject* j_element = PyTuple_GET_ITEM(new_args, j);
262262
int is_ga = PyObject_TypeCheck(i_element, &Py_GenericAliasType) &&
263263
PyObject_TypeCheck(j_element, &Py_GenericAliasType);
264264
// RichCompare to also deduplicate GenericAlias types (slower)

0 commit comments

Comments
 (0)
0