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 assignment expressions before Python 3.8 #16383

Merged
merged 10 commits into from
Feb 28, 2025
Merged

Conversation

ntBre
Copy link
Contributor

@ntBre ntBre commented Feb 25, 2025

Summary

This PR is the first in a series derived from #16308, each of which add support
for detecting one version-related syntax error from #6591. This one should be
the largest because it also includes a couple of additional changes:

  1. the syntax_errors! macro, which makes adding more variants a bit easier
  2. the Parser::add_unsupported_syntax_error method

Otherwise I think the general structure will be the same for each syntax error:

  • Detecting the error in the parser
  • Inline parser tests for the new error
  • New ruff CLI tests for the new error

Because of the second point here, this PR is currently stacked on #16357.

Test Plan

As noted above, there are new inline parser tests, as well as new ruff CLI
tests. Once #16379 is resolved, there should also be new mdtests for red-knot,
but this PR does not currently include those.

Copy link
Contributor

github-actions bot commented Feb 25, 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 added a commit that referenced this pull request 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](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.
ntBre added a commit that referenced this pull request 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](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.
@ntBre ntBre force-pushed the brent/syntax-walrus-38 branch from d8231f5 to f73d2b2 Compare February 26, 2025 17:34
ntBre added a commit that referenced this pull request 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](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.
@ntBre ntBre force-pushed the brent/syntax-walrus-38 branch from fdd6918 to 6cda055 Compare February 26, 2025 17:46
ntBre added a commit that referenced this pull request 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](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.
ntBre added a commit that referenced this pull request 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](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.
Base automatically changed from brent/parser-tests to main February 27, 2025 15:23
This PR is the first in a series derived from #16308, each of which add support
for detecting one version-related syntax error from #6591. This one should be
the largest because it also includes a couple of additional changes:
1. the `syntax_errors!` macro, which makes adding more variants a bit easier
2. the `Parser::add_unsupported_syntax_error` method

Otherwise I think the general structure will be the same for each syntax error:
* Detecting the error in the parser
* Inline parser tests for the new error
* New ruff CLI tests for the new error

Because of the second point here, this PR is currently stacked on #16357.

As noted above, there are new inline parser tests, as well as new ruff CLI
tests. Once #16379 is resolved, there should also be new mdtests for red-knot,
but this PR does not currently include those.
@ntBre ntBre force-pushed the brent/syntax-walrus-38 branch from 6cda055 to 3c98d84 Compare February 27, 2025 15:55
@ntBre ntBre added the preview Related to preview mode features label Feb 27, 2025
@ntBre ntBre marked this pull request as ready for review February 27, 2025 21:24
@ntBre ntBre merged commit 4431978 into main Feb 28, 2025
21 checks passed
@ntBre ntBre deleted the brent/syntax-walrus-38 branch February 28, 2025 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Related to preview mode features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants