[syntax-errors] Star annotations before Python 3.11 (#16545)

Summary
--

This is closely related to (and stacked on)
https://github.com/astral-sh/ruff/pull/16544 and detects star
annotations in function definitions.

I initially called the variant `StarExpressionInAnnotation` to mirror
`StarExpressionInIndex`, but I realized it's not really a "star
expression" in this position and renamed it. `StarAnnotation` seems in
line with the PEP.

Test Plan
--

Two new inline tests. It looked like there was pretty good existing
coverage of this syntax, so I just added simple examples to test the
version cutoff.
This commit is contained in:
Brent Westbrook 2025-03-14 11:20:44 -04:00 committed by GitHub
parent 4f2851982d
commit 6311412373
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 199 additions and 2 deletions

View file

@ -0,0 +1,78 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/param_with_star_annotation_py310.py
---
## AST
```
Module(
ModModule {
range: 0..69,
body: [
FunctionDef(
StmtFunctionDef {
range: 44..68,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("foo"),
range: 48..51,
},
type_params: None,
parameters: Parameters {
range: 51..63,
posonlyargs: [],
args: [],
vararg: Some(
Parameter {
range: 52..62,
name: Identifier {
id: Name("args"),
range: 53..57,
},
annotation: Some(
Starred(
ExprStarred {
range: 59..62,
value: Name(
ExprName {
range: 60..62,
id: Name("Ts"),
ctx: Load,
},
),
ctx: Load,
},
),
),
},
),
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
Expr(
StmtExpr {
range: 65..68,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 65..68,
},
),
},
),
],
},
),
],
},
)
```
## Unsupported Syntax Errors
|
1 | # parse_options: {"target-version": "3.10"}
2 | def foo(*args: *Ts): ...
| ^^^ Syntax Error: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
|

View file

@ -0,0 +1,71 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/ok/param_with_star_annotation_py311.py
---
## AST
```
Module(
ModModule {
range: 0..69,
body: [
FunctionDef(
StmtFunctionDef {
range: 44..68,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("foo"),
range: 48..51,
},
type_params: None,
parameters: Parameters {
range: 51..63,
posonlyargs: [],
args: [],
vararg: Some(
Parameter {
range: 52..62,
name: Identifier {
id: Name("args"),
range: 53..57,
},
annotation: Some(
Starred(
ExprStarred {
range: 59..62,
value: Name(
ExprName {
range: 60..62,
id: Name("Ts"),
ctx: Load,
},
),
ctx: Load,
},
),
),
},
),
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
Expr(
StmtExpr {
range: 65..68,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 65..68,
},
),
},
),
],
},
),
],
},
)
```