Skip to content

Commit 15b0172

Browse files
committed
pythongh-105433: Add pickle tests for PEP695
1 parent 27c68a6 commit 15b0172

File tree

2 files changed

+111
-4
lines changed

2 files changed

+111
-4
lines changed

Lib/test/test_type_aliases.py

+65-4
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,69 @@ def test_module(self):
226226
self.assertEqual(mod_generics_cache.OldStyle.__module__,
227227
mod_generics_cache.__name__)
228228

229+
230+
# All these type aliases are used for pickling tests:
231+
T = TypeVar('T')
232+
type SimpleAlias = int
233+
type RecursiveAlias = dict[str, RecursiveAlias]
234+
type GenericAlias[X] = list[X]
235+
type GenericAliasMultipleTypes[X, Y] = dict[X, Y]
236+
type RecursiveGenericAlias[X] = dict[str, RecursiveAlias[X]]
237+
type BoundGenericAlias[X: int] = set[X]
238+
type ConstrainedGenericAlias[LongName: (str, bytes)] = list[LongName]
239+
type AllTypesAlias[A, *B, **C] = Callable[C, A] | tuple[*B]
240+
241+
242+
class TypeAliasPickleTest(unittest.TestCase):
229243
def test_pickling(self):
230-
pickled = pickle.dumps(mod_generics_cache.Alias)
231-
self.assertIs(pickle.loads(pickled), mod_generics_cache.Alias)
232-
pickled = pickle.dumps(mod_generics_cache.OldStyle)
233-
self.assertIs(pickle.loads(pickled), mod_generics_cache.OldStyle)
244+
things_to_test = [
245+
SimpleAlias,
246+
RecursiveAlias,
247+
248+
GenericAlias,
249+
GenericAlias[T],
250+
GenericAlias[int],
251+
252+
GenericAliasMultipleTypes,
253+
GenericAliasMultipleTypes[str, T],
254+
GenericAliasMultipleTypes[T, str],
255+
GenericAliasMultipleTypes[int, str],
256+
257+
RecursiveGenericAlias,
258+
RecursiveGenericAlias[T],
259+
RecursiveGenericAlias[int],
260+
261+
BoundGenericAlias,
262+
BoundGenericAlias[int],
263+
BoundGenericAlias[T], # type-checker won't be happy
264+
265+
ConstrainedGenericAlias,
266+
ConstrainedGenericAlias[str],
267+
ConstrainedGenericAlias[T], # type-checker won't be happy
268+
269+
AllTypesAlias,
270+
AllTypesAlias[int, str, T, [T, object]],
271+
272+
# Other modules:
273+
mod_generics_cache.Alias,
274+
mod_generics_cache.OldStyle,
275+
]
276+
for thing in things_to_test:
277+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
278+
with self.subTest(thing=thing, proto=proto):
279+
pickled = pickle.dumps(thing, protocol=proto)
280+
self.assertEqual(pickle.loads(pickled), thing)
281+
282+
type ClassLevel = str
283+
284+
def test_pickling_local(self):
285+
type A = int
286+
things_to_test = [
287+
self.ClassLevel,
288+
A,
289+
]
290+
for thing in things_to_test:
291+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
292+
with self.subTest(thing=thing, proto=proto):
293+
with self.assertRaises(pickle.PickleError):
294+
pickle.dumps(thing, protocol=proto)

Lib/test/test_type_params.py

+46
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import textwrap
33
import types
44
import unittest
5+
import pickle
56
from test.support import requires_working_socket, check_syntax_error, run_code
67

78
from typing import Generic, Sequence, TypeVar, TypeVarTuple, ParamSpec, get_args
@@ -849,3 +850,48 @@ def func[A]():
849850

850851
ns = run_code(code)
851852
self.assertEqual(ns["func"].__type_params__, ())
853+
854+
855+
856+
# All these type aliases are used for pickling tests:
857+
T = TypeVar('T')
858+
def func1[X](x: X) -> X: ...
859+
def func2[X, Y](x: X | Y) -> X | Y: ...
860+
def func3[X, *Y, **Z](x: X, y: tuple[*Y], z: Z) -> X: ...
861+
def func4[X: int, Y: (bytes, str)](x: X, y: Y) -> X | Y: ...
862+
863+
class Class1[X]: ...
864+
class Class2[X, Y]: ...
865+
class Class3[X, *Y, **Z]: ...
866+
class Class4[X: int, Y: (bytes, str)]: ...
867+
868+
869+
class TypeParamsPickleTest(unittest.TestCase):
870+
def test_pickling(self):
871+
things_to_test = [
872+
func1,
873+
func2,
874+
func3,
875+
func4,
876+
877+
Class1,
878+
Class1[int],
879+
Class1[T],
880+
881+
Class2,
882+
Class2[int, T],
883+
Class2[T, int],
884+
Class2[int, str],
885+
886+
Class3,
887+
Class3[int, T, str, bytes, [float, object, T]],
888+
889+
Class4,
890+
Class4[int, bytes],
891+
Class4[T, T], # type-checker won't be happy
892+
]
893+
for thing in things_to_test:
894+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
895+
with self.subTest(thing=thing, proto=proto):
896+
pickled = pickle.dumps(thing, protocol=proto)
897+
self.assertEqual(pickle.loads(pickled), thing)

0 commit comments

Comments
 (0)