@@ -306,16 +306,6 @@ def test_eof_error(self):
306306
307307var_annot_global : int # a global annotated is necessary for test_var_annot
308308
309- # custom namespace for testing __annotations__
310-
311- class CNS :
312- def __init__ (self ):
313- self ._dct = {}
314- def __setitem__ (self , item , value ):
315- self ._dct [item .lower ()] = value
316- def __getitem__ (self , item ):
317- return self ._dct [item ]
318-
319309
320310class GrammarTests (unittest .TestCase ):
321311
@@ -446,22 +436,12 @@ class F(C, A):
446436 self .assertEqual (E .__annotations__ , {})
447437 self .assertEqual (F .__annotations__ , {})
448438
449-
450- def test_var_annot_metaclass_semantics (self ):
451- class CMeta (type ):
452- @classmethod
453- def __prepare__ (metacls , name , bases , ** kwds ):
454- return {'__annotations__' : CNS ()}
455- class CC (metaclass = CMeta ):
456- XX : 'ANNOT'
457- self .assertEqual (CC .__annotations__ ['xx' ], 'ANNOT' )
458-
459439 def test_var_annot_module_semantics (self ):
460440 self .assertEqual (test .__annotations__ , {})
461441 self .assertEqual (ann_module .__annotations__ ,
462- { 1 : 2 , 'x' : int , 'y' : str , 'f' : typing .Tuple [int , int ], 'u' : int | float })
442+ { 'x' : int , 'y' : str , 'f' : typing .Tuple [int , int ], 'u' : int | float })
463443 self .assertEqual (ann_module .M .__annotations__ ,
464- { '123' : 123 , 'o' : type })
444+ { 'o' : type })
465445 self .assertEqual (ann_module2 .__annotations__ , {})
466446
467447 def test_var_annot_in_module (self ):
@@ -476,51 +456,12 @@ def test_var_annot_in_module(self):
476456 ann_module3 .D_bad_ann (5 )
477457
478458 def test_var_annot_simple_exec (self ):
479- gns = {}; lns = {}
459+ gns = {}; lns = {}
480460 exec ("'docstring'\n "
481- "__annotations__[1] = 2\n "
482461 "x: int = 5\n " , gns , lns )
483- self .assertEqual (lns ["__annotations__" ], {1 : 2 , 'x' : int })
484- with self .assertRaises (KeyError ):
485- gns ['__annotations__' ]
486-
487- def test_var_annot_custom_maps (self ):
488- # tests with custom locals() and __annotations__
489- ns = {'__annotations__' : CNS ()}
490- exec ('X: int; Z: str = "Z"; (w): complex = 1j' , ns )
491- self .assertEqual (ns ['__annotations__' ]['x' ], int )
492- self .assertEqual (ns ['__annotations__' ]['z' ], str )
462+ self .assertEqual (lns ["__annotate__" ](1 ), {'x' : int })
493463 with self .assertRaises (KeyError ):
494- ns ['__annotations__' ]['w' ]
495- nonloc_ns = {}
496- class CNS2 :
497- def __init__ (self ):
498- self ._dct = {}
499- def __setitem__ (self , item , value ):
500- nonlocal nonloc_ns
501- self ._dct [item ] = value
502- nonloc_ns [item ] = value
503- def __getitem__ (self , item ):
504- return self ._dct [item ]
505- exec ('x: int = 1' , {}, CNS2 ())
506- self .assertEqual (nonloc_ns ['__annotations__' ]['x' ], int )
507-
508- def test_var_annot_refleak (self ):
509- # complex case: custom locals plus custom __annotations__
510- # this was causing refleak
511- cns = CNS ()
512- nonloc_ns = {'__annotations__' : cns }
513- class CNS2 :
514- def __init__ (self ):
515- self ._dct = {'__annotations__' : cns }
516- def __setitem__ (self , item , value ):
517- nonlocal nonloc_ns
518- self ._dct [item ] = value
519- nonloc_ns [item ] = value
520- def __getitem__ (self , item ):
521- return self ._dct [item ]
522- exec ('X: str' , {}, CNS2 ())
523- self .assertEqual (nonloc_ns ['__annotations__' ]['x' ], str )
464+ gns ['__annotate__' ]
524465
525466 def test_var_annot_rhs (self ):
526467 ns = {}
0 commit comments