Skip to content

Commit e1c09ff

Browse files
gh-132882: Fix copying of unions with members that do not support __or__ (#132883)
1 parent 9f5994b commit e1c09ff

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

Lib/copyreg.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def pickle_complex(c):
3131
pickle(complex, pickle_complex, complex)
3232

3333
def pickle_union(obj):
34-
import functools, operator
35-
return functools.reduce, (operator.or_, obj.__args__)
34+
import typing, operator
35+
return operator.getitem, (typing.Union, obj.__args__)
3636

3737
pickle(type(int | str), pickle_union)
3838

Lib/test/test_typing.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -5283,10 +5283,12 @@ class Node(Generic[T]): ...
52835283
Tuple[Any, Any], Node[T], Node[int], Node[Any], typing.Iterable[T],
52845284
typing.Iterable[Any], typing.Iterable[int], typing.Dict[int, str],
52855285
typing.Dict[T, Any], ClassVar[int], ClassVar[List[T]], Tuple['T', 'T'],
5286-
Union['T', int], List['T'], typing.Mapping['T', int]]
5287-
for t in things + [Any]:
5288-
self.assertEqual(t, copy(t))
5289-
self.assertEqual(t, deepcopy(t))
5286+
Union['T', int], List['T'], typing.Mapping['T', int],
5287+
Union[b"x", b"y"], Any]
5288+
for t in things:
5289+
with self.subTest(thing=t):
5290+
self.assertEqual(t, copy(t))
5291+
self.assertEqual(t, deepcopy(t))
52905292

52915293
def test_immutability_by_copy_and_pickle(self):
52925294
# Special forms like Union, Any, etc., generic aliases to containers like List,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix copying of :class:`typing.Union` objects containing objects that do not
2+
support the ``|`` operator.

0 commit comments

Comments
 (0)