Skip to content
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

bpo-43224: Add tests for pickling and copying of unpacked native tuple #32159

Merged
merged 3 commits into from
Apr 5, 2022
Merged
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
73 changes: 56 additions & 17 deletions Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,44 @@
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,
Expand Down Expand Up @@ -351,13 +383,15 @@ class MyType(type):
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):
mrahtz marked this conversation as resolved.
Show resolved Hide resolved
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):
Expand All @@ -366,16 +400,21 @@ def __copy__(self):
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]]
Expand Down