mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
bpo-39702: Relax grammar restrictions on decorators (PEP 614) (GH-18570)
This commit is contained in:
parent
116fd4af73
commit
be501ca241
8 changed files with 534 additions and 537 deletions
|
@ -460,7 +460,7 @@ class GrammarTests(unittest.TestCase):
|
|||
|
||||
def test_funcdef(self):
|
||||
### [decorators] 'def' NAME parameters ['->' test] ':' suite
|
||||
### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
|
||||
### decorator: '@' namedexpr_test NEWLINE
|
||||
### decorators: decorator+
|
||||
### parameters: '(' [typedargslist] ')'
|
||||
### typedargslist: ((tfpdef ['=' test] ',')*
|
||||
|
@ -666,6 +666,20 @@ class GrammarTests(unittest.TestCase):
|
|||
def f(x) -> list: pass
|
||||
self.assertEqual(f.__annotations__, {'return': list})
|
||||
|
||||
# Test expressions as decorators (PEP 614):
|
||||
@False or null
|
||||
def f(x): pass
|
||||
@d := null
|
||||
def f(x): pass
|
||||
@lambda f: null(f)
|
||||
def f(x): pass
|
||||
@[..., null, ...][1]
|
||||
def f(x): pass
|
||||
@null(null)(null)
|
||||
def f(x): pass
|
||||
@[null][0].__call__.__call__
|
||||
def f(x): pass
|
||||
|
||||
# test closures with a variety of opargs
|
||||
closure = 1
|
||||
def f(): return closure
|
||||
|
@ -1515,13 +1529,27 @@ class GrammarTests(unittest.TestCase):
|
|||
def meth2(self, arg): pass
|
||||
def meth3(self, a1, a2): pass
|
||||
|
||||
# decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
|
||||
# decorator: '@' namedexpr_test NEWLINE
|
||||
# decorators: decorator+
|
||||
# decorated: decorators (classdef | funcdef)
|
||||
def class_decorator(x): return x
|
||||
@class_decorator
|
||||
class G: pass
|
||||
|
||||
# Test expressions as decorators (PEP 614):
|
||||
@False or class_decorator
|
||||
class H: pass
|
||||
@d := class_decorator
|
||||
class I: pass
|
||||
@lambda c: class_decorator(c)
|
||||
class J: pass
|
||||
@[..., class_decorator, ...][1]
|
||||
class K: pass
|
||||
@class_decorator(class_decorator)(class_decorator)
|
||||
class L: pass
|
||||
@[class_decorator][0].__call__.__call__
|
||||
class M: pass
|
||||
|
||||
def test_dictcomps(self):
|
||||
# dictorsetmaker: ( (test ':' test (comp_for |
|
||||
# (',' test ':' test)* [','])) |
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue