https://github.com/python/cpython/commit/cfcbfe45bc4c013211b2758c5506232615315d6b
commit: cfcbfe45bc4c013211b2758c5506232615315d6b
branch: main
author: Victorien <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-08-07T01:53:10Z
summary:

gh-105499: Avoid using `functools.reduce()` to reconstruct `Union` objects 
(#155280)

files:
M Lib/test/test_typing.py
M Lib/typing.py

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
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to