bpo-39702: Relax grammar restrictions on decorators (PEP 614) (GH-18570)

This commit is contained in:
Brandt Bucher 2020-03-03 14:25:44 -08:00 committed by GitHub
parent 116fd4af73
commit be501ca241
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 534 additions and 537 deletions

View file

@ -151,21 +151,18 @@ class TestDecorators(unittest.TestCase):
self.assertEqual(counts['double'], 4)
def test_errors(self):
# Test syntax restrictions - these are all compile-time errors:
#
for expr in [ "1+2", "x[3]", "(1, 2)" ]:
# Sanity check: is expr is a valid expression by itself?
compile(expr, "testexpr", "exec")
codestr = "@%s\ndef f(): pass" % expr
self.assertRaises(SyntaxError, compile, codestr, "test", "exec")
# Test SyntaxErrors:
for stmt in ("x,", "x, y", "x = y", "pass", "import sys"):
compile(stmt, "test", "exec") # Sanity check.
with self.assertRaises(SyntaxError):
compile(f"@{stmt}\ndef f(): pass", "test", "exec")
# You can't put multiple decorators on a single line:
#
self.assertRaises(SyntaxError, compile,
"@f1 @f2\ndef f(): pass", "test", "exec")
# Test runtime errors
# Test TypeErrors that used to be SyntaxErrors:
for expr in ("1.+2j", "[1, 2][-1]", "(1, 2)", "True", "...", "None"):
compile(expr, "test", "eval") # Sanity check.
with self.assertRaises(TypeError):
exec(f"@{expr}\ndef f(): pass")
def unimp(func):
raise NotImplementedError
@ -179,6 +176,13 @@ class TestDecorators(unittest.TestCase):
code = compile(codestr, "test", "exec")
self.assertRaises(exc, eval, code, context)
def test_expressions(self):
for expr in (
"(x,)", "(x, y)", "x := y", "(x := y)", "x @y", "(x @ y)", "x[0]",
"w[x].y.z", "w + x - (y + z)", "x(y)()(z)", "[w, x, y][z]", "x.y",
):
compile(f"@{expr}\ndef f(): pass", "test", "exec")
def test_double(self):
class C(object):
@funcattrs(abc=1, xyz="haha")