]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-132882: Fix copying of unions with members that do not support __or__ (#132883)
authorJelle Zijlstra <jelle.zijlstra@gmail.com>
Thu, 24 Apr 2025 16:49:09 +0000 (09:49 -0700)
committerGitHub <noreply@github.com>
Thu, 24 Apr 2025 16:49:09 +0000 (16:49 +0000)
Lib/copyreg.py
Lib/test/test_typing.py
Misc/NEWS.d/next/Library/2025-04-24-09-10-04.gh-issue-132882.6zoyp5.rst [new file with mode: 0644]

index 17c5dde67c887cd125492cde1504c69c5f3238e4..a5e8add4a554d7942b8d59a73872ebfe6549408e 100644 (file)
@@ -31,8 +31,8 @@ def pickle_complex(c):
 pickle(complex, pickle_complex, complex)
 
 def pickle_union(obj):
-    import functools, operator
-    return functools.reduce, (operator.or_, obj.__args__)
+    import typing, operator
+    return operator.getitem, (typing.Union, obj.__args__)
 
 pickle(type(int | str), pickle_union)
 
index 4fd3d53b72c01bb5fd85af3f9f2a7687ac076fbe..93cc2a8ca83b8fbfa0b16331abee55732d49ecda 100644 (file)
@@ -5283,10 +5283,12 @@ class GenericTests(BaseTestCase):
                   Tuple[Any, Any], Node[T], Node[int], Node[Any], typing.Iterable[T],
                   typing.Iterable[Any], typing.Iterable[int], typing.Dict[int, str],
                   typing.Dict[T, Any], ClassVar[int], ClassVar[List[T]], Tuple['T', 'T'],
-                  Union['T', int], List['T'], typing.Mapping['T', int]]
-        for t in things + [Any]:
-            self.assertEqual(t, copy(t))
-            self.assertEqual(t, deepcopy(t))
+                  Union['T', int], List['T'], typing.Mapping['T', int],
+                  Union[b"x", b"y"], Any]
+        for t in things:
+            with self.subTest(thing=t):
+                self.assertEqual(t, copy(t))
+                self.assertEqual(t, deepcopy(t))
 
     def test_immutability_by_copy_and_pickle(self):
         # Special forms like Union, Any, etc., generic aliases to containers like List,
diff --git a/Misc/NEWS.d/next/Library/2025-04-24-09-10-04.gh-issue-132882.6zoyp5.rst b/Misc/NEWS.d/next/Library/2025-04-24-09-10-04.gh-issue-132882.6zoyp5.rst
new file mode 100644 (file)
index 0000000..a93469f
--- /dev/null
@@ -0,0 +1,2 @@
+Fix copying of :class:`typing.Union` objects containing objects that do not
+support the ``|`` operator.