Merge pull request #1931 from TheAnyKey/TheAnyKey/p39_generalize_decorators

Implement Py39 generalized decorators (PEP 614)
This commit is contained in:
Noah authored and GitHub committed 2020-05-21 23:19:34 -05:00
commit 2ecfd48841
3 files changed
+64 -29

No files matched your search

+62 -14
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,18 @@ class TestDecorators(unittest.TestCase):
code = compile(codestr, "test", "exec")
self.assertRaises(exc, eval, code, context)
def test_expressions(self):
for expr in (
## original tests
# "(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",
##same without :=
"(x,)", "(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")
@@ -265,6 +274,45 @@ class TestDecorators(unittest.TestCase):
self.assertEqual(bar(), 42)
self.assertEqual(actions, expected_actions)
def test_wrapped_descriptor_inside_classmethod(self):
class BoundWrapper:
def __init__(self, wrapped):
self.__wrapped__ = wrapped
def __call__(self, *args, **kwargs):
return self.__wrapped__(*args, **kwargs)
class Wrapper:
def __init__(self, wrapped):
self.__wrapped__ = wrapped
def __get__(self, instance, owner):
bound_function = self.__wrapped__.__get__(instance, owner)
return BoundWrapper(bound_function)
def decorator(wrapped):
return Wrapper(wrapped)
class Class:
@decorator
@classmethod
def inner(cls):
# This should already work.
return 'spam'
@classmethod
@decorator
def outer(cls):
# Raised TypeError with a message saying that the 'Wrapper'
# object is not callable.
return 'eggs'
self.assertEqual(Class.inner(), 'spam')
#self.assertEqual(Class.outer(), 'eggs') # TODO RustPython
self.assertEqual(Class().inner(), 'spam')
#self.assertEqual(Class().outer(), 'eggs') # TODO RustPython
class TestClassDecorators(unittest.TestCase):
def test_simple(self):
@@ -301,4 +349,4 @@ class TestClassDecorators(unittest.TestCase):
self.assertEqual(C.extra, 'second')
if __name__ == "__main__":
unittest.main()
unittest.main()
-1
View File
@@ -390,7 +390,6 @@ class TypesTests(unittest.TestCase):
self.assertEqual(locale.format_string('%g', x, grouping=True), format(x, 'n'))
self.assertEqual(locale.format_string('%.10g', x, grouping=True), format(x, '.10n'))
# TODO: RUSTPYTHON
@unittest.expectedFailure
@run_with_locale('LC_NUMERIC', 'en_US.UTF8')
def test_int__format__locale(self):
+2 -14
View File
@@ -606,20 +606,8 @@ Path: ast::Expression = {
// Decorators:
Decorator: ast::Expression = {
"@" <p:Path> <a: (@L "(" ArgumentList ")")?> "\n" => {
match a {
Some((location, _, arg, _)) => {
ast::Expression {
location,
node: ast::ExpressionType::Call {
function: Box::new(p),
args: arg.args,
keywords: arg.keywords,
}
}
},
None => p,
}
<location:@L>"@" <p:Test> "\n" => {
p
},
};