Skip to content

Commit cfcbfe4

Browse files
authored
gh-105499: Avoid using functools.reduce() to reconstruct Union objects (#155280)
1 parent c3aefdb commit cfcbfe4

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

Lib/test/test_typing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9806,6 +9806,7 @@ def test_order_in_union(self):
98069806
for args in itertools.permutations(get_args(expr1)):
98079807
with self.subTest(args=args):
98089808
self.assertEqual(expr1, reduce(operator.or_, args))
9809+
self.assertEqual(expr1, Union[args])
98099810

98109811
expr2 = Union[Annotated[int, 1], str, Annotated[str, {}], int]
98119812
for args in itertools.permutations(get_args(expr2)):

Lib/typing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ def _eval_type(t, globalns, localns, type_params, *, recursive_guard=frozenset()
494494
if isinstance(t, GenericAlias):
495495
return _rebuild_generic_alias(t, ev_args)
496496
if isinstance(t, Union):
497-
return functools.reduce(operator.or_, ev_args)
497+
return Union[ev_args]
498498
else:
499499
return t.copy_with(ev_args)
500500
return t
@@ -2546,7 +2546,7 @@ def _strip_annotations(t):
25462546
stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
25472547
if stripped_args == t.__args__:
25482548
return t
2549-
return functools.reduce(operator.or_, stripped_args)
2549+
return Union[stripped_args]
25502550

25512551
return t
25522552

0 commit comments

Comments
 (0)