Skip to content

Commit

Permalink
Detect named expressions in decorators before Python 3.9
Browse files Browse the repository at this point in the history
Summary
--

This PR detects another syntax error from #6591 and is stacked on #16383. This
time the relaxed grammar for decorators proposed in [PEP
614](https://peps.python.org/pep-0614/) is detected for Python 3.8 and lower.

The 3.8 grammar for decorators is
[here](https://docs.python.org/3.8/reference/compound_stmts.html#grammar-token-decorators):

```
decorators                ::=  decorator+
decorator                 ::=  "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE
dotted_name               ::=  identifier ("." identifier)*
```

in contrast to the current grammar
[here](https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-decorators)

```
decorators                ::= decorator+
decorator                 ::= "@" assignment_expression NEWLINE
assignment_expression ::= [identifier ":="] expression
```

This was the trickiest one of these to detect yet. It seemed like the best
approach was to attempt to parse the old version and fall back on the new
grammar if anything goes wrong, but I'm not as confident in this approach since
it required adding a `Parser::try_parse_old_decorators` method.

Test Plan
--

New inline parser tests and linter CLI tests.
  • Loading branch information
ntBre committed Feb 26, 2025
1 parent 570986f commit 6909145
Showing 0 changed files with 0 additions and 0 deletions.

0 comments on commit 6909145

Please sign in to comment.