@@ -362,7 +362,7 @@ class C:
362
362
z = 2
363
363
def __init__ (self , x ):
364
364
self .x : int = x
365
- self .assertEqual (C .__annotations__ , {'_C__foo' : int , 's' : str })
365
+ self .assertEqual (C .__annotations__ , {'_C__foo' : ' int' , 's' : ' str' })
366
366
with self .assertRaises (NameError ):
367
367
class CBad :
368
368
no_such_name_defined .attr : int = 0
@@ -378,15 +378,15 @@ def __prepare__(metacls, name, bases, **kwds):
378
378
return {'__annotations__' : CNS ()}
379
379
class CC (metaclass = CMeta ):
380
380
XX : 'ANNOT'
381
- self .assertEqual (CC .__annotations__ ['xx' ], 'ANNOT' )
381
+ self .assertEqual (CC .__annotations__ ['xx' ], repr ( 'ANNOT' ) )
382
382
383
383
def test_var_annot_module_semantics (self ):
384
384
with self .assertRaises (AttributeError ):
385
385
print (test .__annotations__ )
386
386
self .assertEqual (ann_module .__annotations__ ,
387
- {1 : 2 , 'x' : int , 'y' : str , 'f' : typing . Tuple [int , int ]})
387
+ {1 : 2 , 'x' : ' int' , 'y' : ' str' , 'f' : ' Tuple[int, int]' })
388
388
self .assertEqual (ann_module .M .__annotations__ ,
389
- {'123' : 123 , 'o' : type })
389
+ {'123' : 123 , 'o' : ' type' })
390
390
self .assertEqual (ann_module2 .__annotations__ , {})
391
391
392
392
def test_var_annot_in_module (self ):
@@ -405,16 +405,16 @@ def test_var_annot_simple_exec(self):
405
405
exec ("'docstring'\n "
406
406
"__annotations__[1] = 2\n "
407
407
"x: int = 5\n " , gns , lns )
408
- self .assertEqual (lns ["__annotations__" ], {1 : 2 , 'x' : int })
408
+ self .assertEqual (lns ["__annotations__" ], {1 : 2 , 'x' : ' int' })
409
409
with self .assertRaises (KeyError ):
410
410
gns ['__annotations__' ]
411
411
412
412
def test_var_annot_custom_maps (self ):
413
413
# tests with custom locals() and __annotations__
414
414
ns = {'__annotations__' : CNS ()}
415
415
exec ('X: int; Z: str = "Z"; (w): complex = 1j' , ns )
416
- self .assertEqual (ns ['__annotations__' ]['x' ], int )
417
- self .assertEqual (ns ['__annotations__' ]['z' ], str )
416
+ self .assertEqual (ns ['__annotations__' ]['x' ], ' int' )
417
+ self .assertEqual (ns ['__annotations__' ]['z' ], ' str' )
418
418
with self .assertRaises (KeyError ):
419
419
ns ['__annotations__' ]['w' ]
420
420
nonloc_ns = {}
@@ -428,7 +428,7 @@ def __setitem__(self, item, value):
428
428
def __getitem__ (self , item ):
429
429
return self ._dct [item ]
430
430
exec ('x: int = 1' , {}, CNS2 ())
431
- self .assertEqual (nonloc_ns ['__annotations__' ]['x' ], int )
431
+ self .assertEqual (nonloc_ns ['__annotations__' ]['x' ], ' int' )
432
432
433
433
def test_var_annot_refleak (self ):
434
434
# complex case: custom locals plus custom __annotations__
@@ -445,7 +445,7 @@ def __setitem__(self, item, value):
445
445
def __getitem__ (self , item ):
446
446
return self ._dct [item ]
447
447
exec ('X: str' , {}, CNS2 ())
448
- self .assertEqual (nonloc_ns ['__annotations__' ]['x' ], str )
448
+ self .assertEqual (nonloc_ns ['__annotations__' ]['x' ], ' str' )
449
449
450
450
def test_var_annot_rhs (self ):
451
451
ns = {}
@@ -625,50 +625,50 @@ def f(*args, **kwargs):
625
625
626
626
# argument annotation tests
627
627
def f (x ) -> list : pass
628
- self .assertEqual (f .__annotations__ , {'return' : list })
628
+ self .assertEqual (f .__annotations__ , {'return' : ' list' })
629
629
def f (x : int ): pass
630
- self .assertEqual (f .__annotations__ , {'x' : int })
630
+ self .assertEqual (f .__annotations__ , {'x' : ' int' })
631
631
def f (x : int , / ): pass
632
- self .assertEqual (f .__annotations__ , {'x' : int })
632
+ self .assertEqual (f .__annotations__ , {'x' : ' int' })
633
633
def f (x : int = 34 , / ): pass
634
- self .assertEqual (f .__annotations__ , {'x' : int })
634
+ self .assertEqual (f .__annotations__ , {'x' : ' int' })
635
635
def f (* x : str ): pass
636
- self .assertEqual (f .__annotations__ , {'x' : str })
636
+ self .assertEqual (f .__annotations__ , {'x' : ' str' })
637
637
def f (** x : float ): pass
638
- self .assertEqual (f .__annotations__ , {'x' : float })
638
+ self .assertEqual (f .__annotations__ , {'x' : ' float' })
639
639
def f (x , y : 1 + 2 ): pass
640
- self .assertEqual (f .__annotations__ , {'y' : 3 })
640
+ self .assertEqual (f .__annotations__ , {'y' : '1 + 2' })
641
641
def f (x , y : 1 + 2 , / ): pass
642
- self .assertEqual (f .__annotations__ , {'y' : 3 })
642
+ self .assertEqual (f .__annotations__ , {'y' : '1 + 2' })
643
643
def f (a , b : 1 , c : 2 , d ): pass
644
- self .assertEqual (f .__annotations__ , {'b' : 1 , 'c' : 2 })
644
+ self .assertEqual (f .__annotations__ , {'b' : '1' , 'c' : '2' })
645
645
def f (a , b : 1 , / , c : 2 , d ): pass
646
- self .assertEqual (f .__annotations__ , {'b' : 1 , 'c' : 2 })
646
+ self .assertEqual (f .__annotations__ , {'b' : '1' , 'c' : '2' })
647
647
def f (a , b : 1 , c : 2 , d , e : 3 = 4 , f = 5 , * g : 6 ): pass
648
648
self .assertEqual (f .__annotations__ ,
649
- {'b' : 1 , 'c' : 2 , 'e' : 3 , 'g' : 6 })
649
+ {'b' : '1' , 'c' : '2' , 'e' : '3' , 'g' : '6' })
650
650
def f (a , b : 1 , c : 2 , d , e : 3 = 4 , f = 5 , * g : 6 , h : 7 , i = 8 , j : 9 = 10 ,
651
651
** k : 11 ) -> 12 : pass
652
652
self .assertEqual (f .__annotations__ ,
653
- {'b' : 1 , 'c' : 2 , 'e' : 3 , 'g' : 6 , 'h' : 7 , 'j' : 9 ,
654
- 'k' : 11 , 'return' : 12 })
653
+ {'b' : '1' , 'c' : '2' , 'e' : '3' , 'g' : '6' , 'h' : '7' , 'j' : '9' ,
654
+ 'k' : '11' , 'return' : '12' })
655
655
def f (a , b : 1 , c : 2 , d , e : 3 = 4 , f : int = 5 , / , * g : 6 , h : 7 , i = 8 , j : 9 = 10 ,
656
656
** k : 11 ) -> 12 : pass
657
657
self .assertEqual (f .__annotations__ ,
658
- {'b' : 1 , 'c' : 2 , 'e' : 3 , 'f' : int , 'g' : 6 , 'h' : 7 , 'j' : 9 ,
659
- 'k' : 11 , 'return' : 12 })
658
+ {'b' : '1' , 'c' : '2' , 'e' : '3' , 'f' : ' int' , 'g' : '6' , 'h' : '7' , 'j' : '9' ,
659
+ 'k' : '11' , 'return' : '12' })
660
660
# Check for issue #20625 -- annotations mangling
661
661
class Spam :
662
662
def f (self , * , __kw : 1 ):
663
663
pass
664
664
class Ham (Spam ): pass
665
- self .assertEqual (Spam .f .__annotations__ , {'_Spam__kw' : 1 })
666
- self .assertEqual (Ham .f .__annotations__ , {'_Spam__kw' : 1 })
665
+ self .assertEqual (Spam .f .__annotations__ , {'_Spam__kw' : '1' })
666
+ self .assertEqual (Ham .f .__annotations__ , {'_Spam__kw' : '1' })
667
667
# Check for SF Bug #1697248 - mixing decorators and a return annotation
668
668
def null (x ): return x
669
669
@null
670
670
def f (x ) -> list : pass
671
- self .assertEqual (f .__annotations__ , {'return' : list })
671
+ self .assertEqual (f .__annotations__ , {'return' : ' list' })
672
672
673
673
# Test expressions as decorators (PEP 614):
674
674
@False or null
@@ -1116,8 +1116,6 @@ def g(): rest = 4, 5, 6; yield 1, 2, 3, *rest
1116
1116
# Not allowed at class scope
1117
1117
check_syntax_error (self , "class foo:yield 1" )
1118
1118
check_syntax_error (self , "class foo:yield from ()" )
1119
- # Check annotation refleak on SyntaxError
1120
- check_syntax_error (self , "def g(a:(yield)): pass" )
1121
1119
1122
1120
def test_yield_in_comprehensions (self ):
1123
1121
# Check yield in comprehensions
0 commit comments