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