@@ -226,8 +226,69 @@ def test_module(self):
226
226
self .assertEqual (mod_generics_cache .OldStyle .__module__ ,
227
227
mod_generics_cache .__name__ )
228
228
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 ):
229
243
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 )
0 commit comments