Skip to content

Commit 14e7c2d

Browse files
committed
Update tests for: syntax, opcodes, posonly, grammar...
1 parent fdcea4d commit 14e7c2d

File tree

4 files changed

+31
-52
lines changed

4 files changed

+31
-52
lines changed

Lib/test/test_grammar.py

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ class C:
362362
z = 2
363363
def __init__(self, x):
364364
self.x: int = x
365-
self.assertEqual(C.__annotations__, {'_C__foo': int, 's': str})
365+
self.assertEqual(C.__annotations__, {'_C__foo': 'int', 's': 'str'})
366366
with self.assertRaises(NameError):
367367
class CBad:
368368
no_such_name_defined.attr: int = 0
@@ -378,15 +378,15 @@ def __prepare__(metacls, name, bases, **kwds):
378378
return {'__annotations__': CNS()}
379379
class CC(metaclass=CMeta):
380380
XX: 'ANNOT'
381-
self.assertEqual(CC.__annotations__['xx'], 'ANNOT')
381+
self.assertEqual(CC.__annotations__['xx'], repr('ANNOT'))
382382

383383
def test_var_annot_module_semantics(self):
384384
with self.assertRaises(AttributeError):
385385
print(test.__annotations__)
386386
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]'})
388388
self.assertEqual(ann_module.M.__annotations__,
389-
{'123': 123, 'o': type})
389+
{'123': 123, 'o': 'type'})
390390
self.assertEqual(ann_module2.__annotations__, {})
391391

392392
def test_var_annot_in_module(self):
@@ -405,16 +405,16 @@ def test_var_annot_simple_exec(self):
405405
exec("'docstring'\n"
406406
"__annotations__[1] = 2\n"
407407
"x: int = 5\n", gns, lns)
408-
self.assertEqual(lns["__annotations__"], {1: 2, 'x': int})
408+
self.assertEqual(lns["__annotations__"], {1: 2, 'x': 'int'})
409409
with self.assertRaises(KeyError):
410410
gns['__annotations__']
411411

412412
def test_var_annot_custom_maps(self):
413413
# tests with custom locals() and __annotations__
414414
ns = {'__annotations__': CNS()}
415415
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')
418418
with self.assertRaises(KeyError):
419419
ns['__annotations__']['w']
420420
nonloc_ns = {}
@@ -428,7 +428,7 @@ def __setitem__(self, item, value):
428428
def __getitem__(self, item):
429429
return self._dct[item]
430430
exec('x: int = 1', {}, CNS2())
431-
self.assertEqual(nonloc_ns['__annotations__']['x'], int)
431+
self.assertEqual(nonloc_ns['__annotations__']['x'], 'int')
432432

433433
def test_var_annot_refleak(self):
434434
# complex case: custom locals plus custom __annotations__
@@ -445,7 +445,7 @@ def __setitem__(self, item, value):
445445
def __getitem__(self, item):
446446
return self._dct[item]
447447
exec('X: str', {}, CNS2())
448-
self.assertEqual(nonloc_ns['__annotations__']['x'], str)
448+
self.assertEqual(nonloc_ns['__annotations__']['x'], 'str')
449449

450450
def test_var_annot_rhs(self):
451451
ns = {}
@@ -625,50 +625,50 @@ def f(*args, **kwargs):
625625

626626
# argument annotation tests
627627
def f(x) -> list: pass
628-
self.assertEqual(f.__annotations__, {'return': list})
628+
self.assertEqual(f.__annotations__, {'return': 'list'})
629629
def f(x: int): pass
630-
self.assertEqual(f.__annotations__, {'x': int})
630+
self.assertEqual(f.__annotations__, {'x': 'int'})
631631
def f(x: int, /): pass
632-
self.assertEqual(f.__annotations__, {'x': int})
632+
self.assertEqual(f.__annotations__, {'x': 'int'})
633633
def f(x: int = 34, /): pass
634-
self.assertEqual(f.__annotations__, {'x': int})
634+
self.assertEqual(f.__annotations__, {'x': 'int'})
635635
def f(*x: str): pass
636-
self.assertEqual(f.__annotations__, {'x': str})
636+
self.assertEqual(f.__annotations__, {'x': 'str'})
637637
def f(**x: float): pass
638-
self.assertEqual(f.__annotations__, {'x': float})
638+
self.assertEqual(f.__annotations__, {'x': 'float'})
639639
def f(x, y: 1+2): pass
640-
self.assertEqual(f.__annotations__, {'y': 3})
640+
self.assertEqual(f.__annotations__, {'y': '1 + 2'})
641641
def f(x, y: 1+2, /): pass
642-
self.assertEqual(f.__annotations__, {'y': 3})
642+
self.assertEqual(f.__annotations__, {'y': '1 + 2'})
643643
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'})
645645
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'})
647647
def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6): pass
648648
self.assertEqual(f.__annotations__,
649-
{'b': 1, 'c': 2, 'e': 3, 'g': 6})
649+
{'b': '1', 'c': '2', 'e': '3', 'g': '6'})
650650
def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6, h: 7, i=8, j: 9 = 10,
651651
**k: 11) -> 12: pass
652652
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'})
655655
def f(a, b: 1, c: 2, d, e: 3 = 4, f: int = 5, /, *g: 6, h: 7, i=8, j: 9 = 10,
656656
**k: 11) -> 12: pass
657657
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'})
660660
# Check for issue #20625 -- annotations mangling
661661
class Spam:
662662
def f(self, *, __kw: 1):
663663
pass
664664
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'})
667667
# Check for SF Bug #1697248 - mixing decorators and a return annotation
668668
def null(x): return x
669669
@null
670670
def f(x) -> list: pass
671-
self.assertEqual(f.__annotations__, {'return': list})
671+
self.assertEqual(f.__annotations__, {'return': 'list'})
672672

673673
# Test expressions as decorators (PEP 614):
674674
@False or null
@@ -1116,8 +1116,6 @@ def g(): rest = 4, 5, 6; yield 1, 2, 3, *rest
11161116
# Not allowed at class scope
11171117
check_syntax_error(self, "class foo:yield 1")
11181118
check_syntax_error(self, "class foo:yield from ()")
1119-
# Check annotation refleak on SyntaxError
1120-
check_syntax_error(self, "def g(a:(yield)): pass")
11211119

11221120
def test_yield_in_comprehensions(self):
11231121
# Check yield in comprehensions

Lib/test/test_opcodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class C: pass
3939
def test_use_existing_annotations(self):
4040
ns = {'__annotations__': {1: 2}}
4141
exec('x: int', ns)
42-
self.assertEqual(ns['__annotations__'], {'x': int, 1: 2})
42+
self.assertEqual(ns['__annotations__'], {'x': 'int', 1: 2})
4343

4444
def test_do_not_recreate_annotations(self):
4545
# Don't rely on the existence of the '__annotations__' global.

Lib/test/test_positional_only_arg.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -302,22 +302,22 @@ def inner_has_pos_only():
302302
def f(x: int, /): ...
303303
return f
304304

305-
assert inner_has_pos_only().__annotations__ == {'x': int}
305+
assert inner_has_pos_only().__annotations__ == {'x': 'int'}
306306

307307
class Something:
308308
def method(self):
309309
def f(x: int, /): ...
310310
return f
311311

312-
assert Something().method().__annotations__ == {'x': int}
312+
assert Something().method().__annotations__ == {'x': 'int'}
313313

314314
def multiple_levels():
315315
def inner_has_pos_only():
316316
def f(x: int, /): ...
317317
return f
318318
return inner_has_pos_only()
319319

320-
assert multiple_levels().__annotations__ == {'x': int}
320+
assert multiple_levels().__annotations__ == {'x': 'int'}
321321

322322
def test_same_keyword_as_positional_with_kwargs(self):
323323
def f(something,/,**kwargs):
@@ -429,17 +429,6 @@ def method(self, /):
429429

430430
self.assertEqual(C().method(), sentinel)
431431

432-
def test_annotations_constant_fold(self):
433-
def g():
434-
def f(x: not (int is int), /): ...
435-
436-
# without constant folding we end up with
437-
# COMPARE_OP(is), IS_OP (0)
438-
# with constant folding we should expect a IS_OP (1)
439-
codes = [(i.opname, i.argval) for i in dis.get_instructions(g)]
440-
self.assertNotIn(('UNARY_NOT', None), codes)
441-
self.assertIn(('IS_OP', 1), codes)
442-
443432

444433
if __name__ == "__main__":
445434
unittest.main()

Lib/test/test_syntax.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -752,14 +752,6 @@
752752
Traceback (most recent call last):
753753
SyntaxError: cannot assign to __debug__
754754
755-
>>> def f(*args:(lambda __debug__:0)): pass
756-
Traceback (most recent call last):
757-
SyntaxError: cannot assign to __debug__
758-
759-
>>> def f(**kwargs:(lambda __debug__:0)): pass
760-
Traceback (most recent call last):
761-
SyntaxError: cannot assign to __debug__
762-
763755
>>> with (lambda *:0): pass
764756
Traceback (most recent call last):
765757
SyntaxError: named arguments must follow bare *

0 commit comments

Comments
 (0)