Skip to content

gh-132882: Fix copying of unions with members that do not support __or__ #132883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/copyreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 6 additions & 4 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5283,10 +5283,12 @@ class Node(Generic[T]): ...
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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix copying of :class:`typing.Union` objects containing objects that do not
support the ``|`` operator.
Loading