diff options
author | Matthew Rahtz <matthew.rahtz@gmail.com> | 2022-04-05 02:52:42 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-05 02:52:42 (GMT) |
commit | 772d8080c9fd635c3999673ca9fad8b674385c7f (patch) | |
tree | 36e89ff95ed417758ff5042a2cb8705a5d3935ee /Lib/test/test_genericalias.py | |
parent | cae0f5d3dad6db0d13690e5952ae2015ad8b3a05 (diff) | |
download | cpython-772d8080c9fd635c3999673ca9fad8b674385c7f.zip cpython-772d8080c9fd635c3999673ca9fad8b674385c7f.tar.gz cpython-772d8080c9fd635c3999673ca9fad8b674385c7f.tar.bz2 |
bpo-43224: typing: Add tests for pickling and copying of unpacked native tuple (GH-32159)
Diffstat (limited to 'Lib/test/test_genericalias.py')
-rw-r--r-- | Lib/test/test_genericalias.py | 73 |
1 files changed, 56 insertions, 17 deletions
diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index 39c56f2..bf96ba0 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -47,12 +47,44 @@ from unittest.case import _AssertRaisesContext from queue import Queue, SimpleQueue from weakref import WeakSet, ReferenceType, ref import typing +from typing import Unpack from typing import TypeVar T = TypeVar('T') K = TypeVar('K') V = TypeVar('V') +_UNPACKED_TUPLES = [ + # Unpacked tuple using `*` + (*tuple[int],)[0], + (*tuple[T],)[0], + (*tuple[int, str],)[0], + (*tuple[int, ...],)[0], + (*tuple[T, ...],)[0], + tuple[*tuple[int, ...]], + tuple[*tuple[T, ...]], + tuple[str, *tuple[int, ...]], + tuple[*tuple[int, ...], str], + tuple[float, *tuple[int, ...], str], + tuple[*tuple[*tuple[int, ...]]], + # Unpacked tuple using `Unpack` + Unpack[tuple[int]], + Unpack[tuple[T]], + Unpack[tuple[int, str]], + Unpack[tuple[int, ...]], + Unpack[tuple[T, ...]], + tuple[Unpack[tuple[int, ...]]], + tuple[Unpack[tuple[T, ...]]], + tuple[str, Unpack[tuple[int, ...]]], + tuple[Unpack[tuple[int, ...]], str], + tuple[float, Unpack[tuple[int, ...]], str], + tuple[Unpack[tuple[Unpack[tuple[int, ...]]]]], + # Unpacked tuple using `*` AND `Unpack` + tuple[Unpack[tuple[*tuple[int, ...]]]], + tuple[*tuple[Unpack[tuple[int, ...]]]], +] + + class BaseTest(unittest.TestCase): """Test basics.""" generic_types = [type, tuple, list, dict, set, frozenset, enumerate, @@ -351,13 +383,15 @@ class BaseTest(unittest.TestCase): MyType[int] def test_pickle(self): - alias = GenericAlias(list, T) - for proto in range(pickle.HIGHEST_PROTOCOL + 1): - s = pickle.dumps(alias, proto) - loaded = pickle.loads(s) - self.assertEqual(loaded.__origin__, alias.__origin__) - self.assertEqual(loaded.__args__, alias.__args__) - self.assertEqual(loaded.__parameters__, alias.__parameters__) + aliases = [GenericAlias(list, T)] + _UNPACKED_TUPLES + for alias in aliases: + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + with self.subTest(alias=alias, proto=proto): + s = pickle.dumps(alias, proto) + loaded = pickle.loads(s) + self.assertEqual(loaded.__origin__, alias.__origin__) + self.assertEqual(loaded.__args__, alias.__args__) + self.assertEqual(loaded.__parameters__, alias.__parameters__) def test_copy(self): class X(list): @@ -366,16 +400,21 @@ class BaseTest(unittest.TestCase): def __deepcopy__(self, memo): return self - for origin in list, deque, X: - alias = GenericAlias(origin, T) - copied = copy.copy(alias) - self.assertEqual(copied.__origin__, alias.__origin__) - self.assertEqual(copied.__args__, alias.__args__) - self.assertEqual(copied.__parameters__, alias.__parameters__) - copied = copy.deepcopy(alias) - self.assertEqual(copied.__origin__, alias.__origin__) - self.assertEqual(copied.__args__, alias.__args__) - self.assertEqual(copied.__parameters__, alias.__parameters__) + aliases = [ + GenericAlias(list, T), + GenericAlias(deque, T), + GenericAlias(X, T) + ] + _UNPACKED_TUPLES + for alias in aliases: + with self.subTest(alias=alias): + copied = copy.copy(alias) + self.assertEqual(copied.__origin__, alias.__origin__) + self.assertEqual(copied.__args__, alias.__args__) + self.assertEqual(copied.__parameters__, alias.__parameters__) + copied = copy.deepcopy(alias) + self.assertEqual(copied.__origin__, alias.__origin__) + self.assertEqual(copied.__args__, alias.__args__) + self.assertEqual(copied.__parameters__, alias.__parameters__) def test_union(self): a = typing.Union[list[int], list[str]] |