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

This commit is contained in:
Jelle Zijlstra 2025-04-24 09:49:09 -07:00 committed by GitHub
parent 9f5994b94c
commit e1c09fff05
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 10 additions and 6 deletions

View 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)

View 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,

View file

@ -0,0 +1,2 @@
Fix copying of :class:`typing.Union` objects containing objects that do not
support the ``|`` operator.