diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 619d0cb2fe541a..2875303fb15619 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -9806,6 +9806,7 @@ def test_order_in_union(self): for args in itertools.permutations(get_args(expr1)): with self.subTest(args=args): self.assertEqual(expr1, reduce(operator.or_, args)) + self.assertEqual(expr1, Union[args]) expr2 = Union[Annotated[int, 1], str, Annotated[str, {}], int] for args in itertools.permutations(get_args(expr2)): diff --git a/Lib/typing.py b/Lib/typing.py index 809c0ff88607a5..b56ba954ad38be 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -494,7 +494,7 @@ def _eval_type(t, globalns, localns, type_params, *, recursive_guard=frozenset() if isinstance(t, GenericAlias): return _rebuild_generic_alias(t, ev_args) if isinstance(t, Union): - return functools.reduce(operator.or_, ev_args) + return Union[ev_args] else: return t.copy_with(ev_args) return t @@ -2546,7 +2546,7 @@ def _strip_annotations(t): stripped_args = tuple(_strip_annotations(a) for a in t.__args__) if stripped_args == t.__args__: return t - return functools.reduce(operator.or_, stripped_args) + return Union[stripped_args] return t