]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-105499: Avoid using `functools.reduce()` to reconstruct `Union` objects (#155280)
authorVictorien <65306057+Viicos@users.noreply.github.com>
Fri, 7 Aug 2026 01:53:10 +0000 (03:53 +0200)
committerGitHub <noreply@github.com>
Fri, 7 Aug 2026 01:53:10 +0000 (01:53 +0000)
Lib/test/test_typing.py
Lib/typing.py

index 619d0cb2fe541a66524f4b89c5eddabe67b3f7e4..2875303fb156197d2fd4eb8c9c42fcae7649c76c 100644 (file)
@@ -9806,6 +9806,7 @@ class AnnotatedTests(BaseTestCase):
         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)):
index 809c0ff88607a59067cd0061222063eb072bbad0..b56ba954ad38be1e4bd06013619c5b34fe090dac 100644 (file)
@@ -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