Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect named expressions in decorators before Python 3.9 #16386

Draft
wants to merge 6 commits into
base: brent/syntax-walrus-38
Choose a base branch
from

Conversation

ntBre
Copy link
Contributor

@ntBre ntBre commented Feb 26, 2025

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 is detected for Python 3.8 and lower.

The 3.8 grammar for decorators is here:

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

in contrast to the current grammar here

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 new method to the parser.

Test Plan

New inline parser tests and linter CLI tests.

@ntBre ntBre force-pushed the brent/syntax-decorators-39 branch from 91d9f95 to 31fe82e Compare February 26, 2025 00:53
Copy link
Contributor

github-actions bot commented Feb 26, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@ntBre ntBre force-pushed the brent/syntax-walrus-38 branch from d8231f5 to f73d2b2 Compare February 26, 2025 17:34
@ntBre ntBre force-pushed the brent/syntax-decorators-39 branch from 31fe82e to 894a296 Compare February 26, 2025 17:34
Copy link

codspeed-hq bot commented Feb 26, 2025

CodSpeed Performance Report

Merging #16386 will not alter performance

Comparing brent/syntax-decorators-39 (5801f20) with brent/syntax-walrus-38 (6cda055)

Summary

✅ 32 untouched benchmarks

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant