diff options
Diffstat (limited to 'Lib/test/test_typing.py')
| -rw-r--r-- | Lib/test/test_typing.py | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index f56caa1..09e39fe 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -1057,20 +1057,20 @@ class GenericTests(BaseTestCase): self.assertEqual(x.foo, 42) self.assertEqual(x.bar, 'abc') self.assertEqual(x.__dict__, {'foo': 42, 'bar': 'abc'}) - samples = [Any, Union, Tuple, Callable, ClassVar] + samples = [Any, Union, Tuple, Callable, ClassVar, + Union[int, str], ClassVar[List], Tuple[int, ...], Callable[[str], bytes]] for s in samples: for proto in range(pickle.HIGHEST_PROTOCOL + 1): z = pickle.dumps(s, proto) x = pickle.loads(z) self.assertEqual(s, x) - more_samples = [List, typing.Iterable, typing.Type] + more_samples = [List, typing.Iterable, typing.Type, List[int], + typing.Type[typing.Mapping]] for s in more_samples: for proto in range(pickle.HIGHEST_PROTOCOL + 1): z = pickle.dumps(s, proto) x = pickle.loads(z) - self.assertEqual(repr(s), repr(x)) # TODO: fix this - # see also comment in test_copy_and_deepcopy - # the issue is typing/#512 + self.assertEqual(s, x) def test_copy_and_deepcopy(self): T = TypeVar('T') @@ -1082,7 +1082,27 @@ class GenericTests(BaseTestCase): Union['T', int], List['T'], typing.Mapping['T', int]] for t in things + [Any]: self.assertEqual(t, copy(t)) - self.assertEqual(repr(t), repr(deepcopy(t))) # Use repr() because of TypeVars + 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, + # Mapping, etc., and type variabcles are considered immutable by copy and pickle. + global TP, TPB, TPV # for pickle + TP = TypeVar('TP') + TPB = TypeVar('TPB', bound=int) + TPV = TypeVar('TPV', bytes, str) + for X in [TP, TPB, TPV, List, typing.Mapping, ClassVar, typing.Iterable, + Union, Any, Tuple, Callable]: + self.assertIs(copy(X), X) + self.assertIs(deepcopy(X), X) + self.assertIs(pickle.loads(pickle.dumps(X)), X) + # Check that local type variables are copyable. + TL = TypeVar('TL') + TLB = TypeVar('TLB', bound=int) + TLV = TypeVar('TLV', bytes, str) + for X in [TL, TLB, TLV]: + self.assertIs(copy(X), X) + self.assertIs(deepcopy(X), X) def test_copy_generic_instances(self): T = TypeVar('T') |
