Skip to content

Commit

Permalink
Detect '@' dotted_name '(' ')' NEWLINE as a simple decorator
Browse files Browse the repository at this point in the history
Previously the RELAXED_DECORATOR detection would be falsely True on that
example. The problem was that an argument-less parentheses pair didn't
pass the `is_simple_decorator_trailer` check even it should. OTOH a
parentheses pair containing an argument or more passed as expected.
  • Loading branch information
ichard26 committed May 3, 2021
1 parent 35e8d15 commit c2a6c26
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -6,6 +6,9 @@

- Set `--pyi` mode if `--stdin-filename` ends in `.pyi` (#2169)

- Stop detecting target version as Python 3.9+ with pre-PEP-614 decorators that are
being called but with no arguments (#2182)

### 21.4b2

#### _Black_
Expand Down
9 changes: 9 additions & 0 deletions src/black/__init__.py
Expand Up @@ -5761,6 +5761,13 @@ def is_simple_decorator_trailer(node: LN, last: bool = False) -> bool:
and node.children[0].type == token.DOT
and node.children[1].type == token.NAME
)
# last trailer can be an argument-less parentheses pair
or (
last
and len(node.children) == 2
and node.children[0].type == token.LPAR
and node.children[1].type == token.RPAR
)
# last trailer can be arguments
or (
last
Expand All @@ -5781,6 +5788,7 @@ def is_simple_decorator_expression(node: LN) -> bool:
The old grammar was: decorator: @ dotted_name [arguments] NEWLINE
The new grammar is : decorator: @ namedexpr_test NEWLINE
"""
print(f"{node!r}")
if node.type == token.NAME:
return True
if node.type == syms.power:
Expand Down Expand Up @@ -6003,6 +6011,7 @@ def get_features_used(node: Node) -> Set[Feature]:
features.add(Feature.ASSIGNMENT_EXPRESSIONS)

elif n.type == syms.decorator:
print("uh")
if len(n.children) > 1 and not is_simple_decorator_expression(
n.children[1]
):
Expand Down
6 changes: 6 additions & 0 deletions tests/data/decorators.py
Expand Up @@ -13,6 +13,12 @@ def f():

##

@decorator()
def f():
...

##

@decorator(arg)
def f():
...
Expand Down

0 comments on commit c2a6c26

Please sign in to comment.