[syntax-errors] Extend annotation checks to await (#17282)

Summary
--

This PR extends the changes in #17101 to include `await` in the same
positions.

I also renamed the `valid_annotation_function` test to include `_py313`
and explicitly passed a Python version to contrast it with the `_py314`
version.

Test Plan
--

New test cases added to existing files.
This commit is contained in:
Brent Westbrook 2025-04-08 08:55:43 -04:00 committed by GitHub
parent b662c3ff7e
commit 127a45622f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 1399 additions and 314 deletions

View file

@ -7,7 +7,7 @@ input_file: crates/ruff_python_parser/resources/inline/err/invalid_annotation_cl
```
Module(
ModModule {
range: 0..193,
range: 0..247,
body: [
ClassDef(
StmtClassDef {
@ -319,6 +319,122 @@ Module(
],
},
),
ClassDef(
StmtClassDef {
range: 193..219,
decorator_list: [],
name: Identifier {
id: Name("M"),
range: 199..200,
},
type_params: Some(
TypeParams {
range: 200..203,
type_params: [
TypeVar(
TypeParamTypeVar {
range: 201..202,
name: Identifier {
id: Name("T"),
range: 201..202,
},
bound: None,
default: None,
},
),
],
},
),
arguments: Some(
Arguments {
range: 203..214,
args: [
Await(
ExprAwait {
range: 205..212,
value: NumberLiteral(
ExprNumberLiteral {
range: 211..212,
value: Int(
1,
),
},
),
},
),
],
keywords: [],
},
),
body: [
Expr(
StmtExpr {
range: 216..219,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 216..219,
},
),
},
),
],
},
),
ClassDef(
StmtClassDef {
range: 220..246,
decorator_list: [],
name: Identifier {
id: Name("N"),
range: 226..227,
},
type_params: Some(
TypeParams {
range: 227..241,
type_params: [
TypeVar(
TypeParamTypeVar {
range: 228..240,
name: Identifier {
id: Name("T"),
range: 228..229,
},
bound: Some(
Await(
ExprAwait {
range: 232..239,
value: NumberLiteral(
ExprNumberLiteral {
range: 238..239,
value: Int(
1,
),
},
),
},
),
),
default: None,
},
),
],
},
),
arguments: None,
body: [
Expr(
StmtExpr {
range: 243..246,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 243..246,
},
),
},
),
],
},
),
],
},
)
@ -358,6 +474,7 @@ Module(
4 | class K[T: (yield 1)]: ... # yield in TypeVar
| ^^^^^^^ Syntax Error: yield expression cannot be used within a TypeVar bound
5 | class L[T: (x := 1)]: ... # named expr in TypeVar
6 | class M[T]((await 1)): ...
|
@ -366,4 +483,23 @@ Module(
4 | class K[T: (yield 1)]: ... # yield in TypeVar
5 | class L[T: (x := 1)]: ... # named expr in TypeVar
| ^^^^^^ Syntax Error: named expression cannot be used within a TypeVar bound
6 | class M[T]((await 1)): ...
7 | class N[T: (await 1)]: ...
|
|
4 | class K[T: (yield 1)]: ... # yield in TypeVar
5 | class L[T: (x := 1)]: ... # named expr in TypeVar
6 | class M[T]((await 1)): ...
| ^^^^^^^ Syntax Error: await expression cannot be used within a generic definition
7 | class N[T: (await 1)]: ...
|
|
5 | class L[T: (x := 1)]: ... # named expr in TypeVar
6 | class M[T]((await 1)): ...
7 | class N[T: (await 1)]: ...
| ^^^^^^^ Syntax Error: await expression cannot be used within a TypeVar bound
|

View file

@ -7,7 +7,7 @@ input_file: crates/ruff_python_parser/resources/inline/err/invalid_annotation_fu
```
Module(
ModModule {
range: 0..236,
range: 0..316,
body: [
FunctionDef(
StmtFunctionDef {
@ -371,6 +371,136 @@ Module(
],
},
),
FunctionDef(
StmtFunctionDef {
range: 236..315,
is_async: true,
decorator_list: [],
name: Identifier {
id: Name("outer"),
range: 246..251,
},
type_params: None,
parameters: Parameters {
range: 251..253,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
FunctionDef(
StmtFunctionDef {
range: 259..284,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("f"),
range: 263..264,
},
type_params: None,
parameters: Parameters {
range: 264..266,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: Some(
Await(
ExprAwait {
range: 271..278,
value: NumberLiteral(
ExprNumberLiteral {
range: 277..278,
value: Int(
1,
),
},
),
},
),
),
body: [
Expr(
StmtExpr {
range: 281..284,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 281..284,
},
),
},
),
],
},
),
FunctionDef(
StmtFunctionDef {
range: 289..315,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("g"),
range: 293..294,
},
type_params: None,
parameters: Parameters {
range: 294..310,
posonlyargs: [],
args: [
ParameterWithDefault {
range: 295..309,
parameter: Parameter {
range: 295..309,
name: Identifier {
id: Name("arg"),
range: 295..298,
},
annotation: Some(
Await(
ExprAwait {
range: 301..308,
value: NumberLiteral(
ExprNumberLiteral {
range: 307..308,
value: Int(
1,
),
},
),
},
),
),
},
default: None,
},
],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
Expr(
StmtExpr {
range: 312..315,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 312..315,
},
),
},
),
],
},
),
],
},
),
],
},
)
@ -422,12 +552,32 @@ Module(
7 | def m(x: (yield from 1)): ...
| ^^^^^^^^^^^^ Syntax Error: yield expression cannot be used within a type annotation
8 | def o() -> (yield from 1): ...
9 | async def outer():
|
|
6 | def k() -> (yield 1): ...
7 | def m(x: (yield from 1)): ...
8 | def o() -> (yield from 1): ...
| ^^^^^^^^^^^^ Syntax Error: yield expression cannot be used within a type annotation
|
|
6 | def k() -> (yield 1): ...
7 | def m(x: (yield from 1)): ...
8 | def o() -> (yield from 1): ...
| ^^^^^^^^^^^^ Syntax Error: yield expression cannot be used within a type annotation
9 | async def outer():
10 | def f() -> (await 1): ...
|
|
8 | def o() -> (yield from 1): ...
9 | async def outer():
10 | def f() -> (await 1): ...
| ^^^^^^^ Syntax Error: await expression cannot be used within a type annotation
11 | def g(arg: (await 1)): ...
|
|
9 | async def outer():
10 | def f() -> (await 1): ...
11 | def g(arg: (await 1)): ...
| ^^^^^^^ Syntax Error: await expression cannot be used within a type annotation
|

View file

@ -7,7 +7,7 @@ input_file: crates/ruff_python_parser/resources/inline/err/invalid_annotation_ty
```
Module(
ModModule {
range: 0..308,
range: 0..406,
body: [
TypeAlias(
StmtTypeAlias {
@ -280,6 +280,83 @@ Module(
),
},
),
TypeAlias(
StmtTypeAlias {
range: 308..334,
name: Name(
ExprName {
range: 313..314,
id: Name("Y"),
ctx: Store,
},
),
type_params: Some(
TypeParams {
range: 314..328,
type_params: [
TypeVar(
TypeParamTypeVar {
range: 315..327,
name: Identifier {
id: Name("T"),
range: 315..316,
},
bound: Some(
Await(
ExprAwait {
range: 319..326,
value: NumberLiteral(
ExprNumberLiteral {
range: 325..326,
value: Int(
1,
),
},
),
},
),
),
default: None,
},
),
],
},
),
value: Name(
ExprName {
range: 331..334,
id: Name("int"),
ctx: Load,
},
),
},
),
TypeAlias(
StmtTypeAlias {
range: 357..375,
name: Name(
ExprName {
range: 362..363,
id: Name("Y"),
ctx: Store,
},
),
type_params: None,
value: Await(
ExprAwait {
range: 367..374,
value: NumberLiteral(
ExprNumberLiteral {
range: 373..374,
value: Int(
1,
),
},
),
},
),
},
),
],
},
)
@ -329,6 +406,7 @@ Module(
5 | type Y = (yield 1) # yield in value
| ^^^^^^^ Syntax Error: yield expression cannot be used within a type alias
6 | type Y = (x := 1) # named expr in value
7 | type Y[T: (await 1)] = int # await in bound
|
@ -337,4 +415,23 @@ Module(
5 | type Y = (yield 1) # yield in value
6 | type Y = (x := 1) # named expr in value
| ^^^^^^ Syntax Error: named expression cannot be used within a type alias
7 | type Y[T: (await 1)] = int # await in bound
8 | type Y = (await 1) # await in value
|
|
5 | type Y = (yield 1) # yield in value
6 | type Y = (x := 1) # named expr in value
7 | type Y[T: (await 1)] = int # await in bound
| ^^^^^^^ Syntax Error: await expression cannot be used within a TypeVar bound
8 | type Y = (await 1) # await in value
|
|
6 | type Y = (x := 1) # named expr in value
7 | type Y[T: (await 1)] = int # await in bound
8 | type Y = (await 1) # await in value
| ^^^^^^^ Syntax Error: await expression cannot be used within a type alias
|

View file

@ -7,7 +7,7 @@ input_file: crates/ruff_python_parser/resources/inline/ok/valid_annotation_class
```
Module(
ModModule {
range: 0..94,
range: 0..137,
body: [
ClassDef(
StmtClassDef {
@ -172,6 +172,73 @@ Module(
],
},
),
FunctionDef(
StmtFunctionDef {
range: 94..136,
is_async: true,
decorator_list: [],
name: Identifier {
id: Name("f"),
range: 104..105,
},
type_params: None,
parameters: Parameters {
range: 105..107,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
ClassDef(
StmtClassDef {
range: 113..136,
decorator_list: [],
name: Identifier {
id: Name("G"),
range: 119..120,
},
type_params: None,
arguments: Some(
Arguments {
range: 120..131,
args: [
Await(
ExprAwait {
range: 122..129,
value: NumberLiteral(
ExprNumberLiteral {
range: 128..129,
value: Int(
1,
),
},
),
},
),
],
keywords: [],
},
),
body: [
Expr(
StmtExpr {
range: 133..136,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 133..136,
},
),
},
),
],
},
),
],
},
),
],
},
)

View file

@ -1,26 +1,26 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/ok/valid_annotation_function.py
input_file: crates/ruff_python_parser/resources/inline/ok/valid_annotation_function_py313.py
---
## AST
```
Module(
ModModule {
range: 0..192,
range: 0..316,
body: [
FunctionDef(
StmtFunctionDef {
range: 0..24,
range: 44..68,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("f"),
range: 4..5,
range: 48..49,
},
type_params: None,
parameters: Parameters {
range: 5..7,
range: 49..51,
posonlyargs: [],
args: [],
vararg: None,
@ -30,17 +30,17 @@ Module(
returns: Some(
Named(
ExprNamed {
range: 12..18,
range: 56..62,
target: Name(
ExprName {
range: 12..13,
range: 56..57,
id: Name("y"),
ctx: Store,
},
),
value: NumberLiteral(
ExprNumberLiteral {
range: 17..18,
range: 61..62,
value: Int(
3,
),
@ -52,10 +52,10 @@ Module(
body: [
Expr(
StmtExpr {
range: 21..24,
range: 65..68,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 21..24,
range: 65..68,
},
),
},
@ -65,40 +65,40 @@ Module(
),
FunctionDef(
StmtFunctionDef {
range: 25..50,
range: 69..94,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("g"),
range: 29..30,
range: 73..74,
},
type_params: None,
parameters: Parameters {
range: 30..45,
range: 74..89,
posonlyargs: [],
args: [
ParameterWithDefault {
range: 31..44,
range: 75..88,
parameter: Parameter {
range: 31..44,
range: 75..88,
name: Identifier {
id: Name("arg"),
range: 31..34,
range: 75..78,
},
annotation: Some(
Named(
ExprNamed {
range: 37..43,
range: 81..87,
target: Name(
ExprName {
range: 37..38,
range: 81..82,
id: Name("x"),
ctx: Store,
},
),
value: NumberLiteral(
ExprNumberLiteral {
range: 42..43,
range: 86..87,
value: Int(
1,
),
@ -119,10 +119,10 @@ Module(
body: [
Expr(
StmtExpr {
range: 47..50,
range: 91..94,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 47..50,
range: 91..94,
},
),
},
@ -132,16 +132,16 @@ Module(
),
FunctionDef(
StmtFunctionDef {
range: 51..191,
range: 95..235,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("outer"),
range: 55..60,
range: 99..104,
},
type_params: None,
parameters: Parameters {
range: 60..62,
range: 104..106,
posonlyargs: [],
args: [],
vararg: None,
@ -152,34 +152,34 @@ Module(
body: [
FunctionDef(
StmtFunctionDef {
range: 68..92,
range: 112..136,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("i"),
range: 72..73,
range: 116..117,
},
type_params: None,
parameters: Parameters {
range: 73..87,
range: 117..131,
posonlyargs: [],
args: [
ParameterWithDefault {
range: 74..86,
range: 118..130,
parameter: Parameter {
range: 74..86,
range: 118..130,
name: Identifier {
id: Name("x"),
range: 74..75,
range: 118..119,
},
annotation: Some(
Yield(
ExprYield {
range: 78..85,
range: 122..129,
value: Some(
NumberLiteral(
ExprNumberLiteral {
range: 84..85,
range: 128..129,
value: Int(
1,
),
@ -201,10 +201,10 @@ Module(
body: [
Expr(
StmtExpr {
range: 89..92,
range: 133..136,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 89..92,
range: 133..136,
},
),
},
@ -214,16 +214,16 @@ Module(
),
FunctionDef(
StmtFunctionDef {
range: 97..122,
range: 141..166,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("k"),
range: 101..102,
range: 145..146,
},
type_params: None,
parameters: Parameters {
range: 102..104,
range: 146..148,
posonlyargs: [],
args: [],
vararg: None,
@ -233,11 +233,11 @@ Module(
returns: Some(
Yield(
ExprYield {
range: 109..116,
range: 153..160,
value: Some(
NumberLiteral(
ExprNumberLiteral {
range: 115..116,
range: 159..160,
value: Int(
1,
),
@ -250,10 +250,10 @@ Module(
body: [
Expr(
StmtExpr {
range: 119..122,
range: 163..166,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 119..122,
range: 163..166,
},
),
},
@ -263,33 +263,33 @@ Module(
),
FunctionDef(
StmtFunctionDef {
range: 127..156,
range: 171..200,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("m"),
range: 131..132,
range: 175..176,
},
type_params: None,
parameters: Parameters {
range: 132..151,
range: 176..195,
posonlyargs: [],
args: [
ParameterWithDefault {
range: 133..150,
range: 177..194,
parameter: Parameter {
range: 133..150,
range: 177..194,
name: Identifier {
id: Name("x"),
range: 133..134,
range: 177..178,
},
annotation: Some(
YieldFrom(
ExprYieldFrom {
range: 137..149,
range: 181..193,
value: NumberLiteral(
ExprNumberLiteral {
range: 148..149,
range: 192..193,
value: Int(
1,
),
@ -310,10 +310,10 @@ Module(
body: [
Expr(
StmtExpr {
range: 153..156,
range: 197..200,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 153..156,
range: 197..200,
},
),
},
@ -323,16 +323,16 @@ Module(
),
FunctionDef(
StmtFunctionDef {
range: 161..191,
range: 205..235,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("o"),
range: 165..166,
range: 209..210,
},
type_params: None,
parameters: Parameters {
range: 166..168,
range: 210..212,
posonlyargs: [],
args: [],
vararg: None,
@ -342,10 +342,10 @@ Module(
returns: Some(
YieldFrom(
ExprYieldFrom {
range: 173..185,
range: 217..229,
value: NumberLiteral(
ExprNumberLiteral {
range: 184..185,
range: 228..229,
value: Int(
1,
),
@ -357,10 +357,140 @@ Module(
body: [
Expr(
StmtExpr {
range: 188..191,
range: 232..235,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 188..191,
range: 232..235,
},
),
},
),
],
},
),
],
},
),
FunctionDef(
StmtFunctionDef {
range: 236..315,
is_async: true,
decorator_list: [],
name: Identifier {
id: Name("outer"),
range: 246..251,
},
type_params: None,
parameters: Parameters {
range: 251..253,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
FunctionDef(
StmtFunctionDef {
range: 259..284,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("f"),
range: 263..264,
},
type_params: None,
parameters: Parameters {
range: 264..266,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: Some(
Await(
ExprAwait {
range: 271..278,
value: NumberLiteral(
ExprNumberLiteral {
range: 277..278,
value: Int(
1,
),
},
),
},
),
),
body: [
Expr(
StmtExpr {
range: 281..284,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 281..284,
},
),
},
),
],
},
),
FunctionDef(
StmtFunctionDef {
range: 289..315,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("g"),
range: 293..294,
},
type_params: None,
parameters: Parameters {
range: 294..310,
posonlyargs: [],
args: [
ParameterWithDefault {
range: 295..309,
parameter: Parameter {
range: 295..309,
name: Identifier {
id: Name("arg"),
range: 295..298,
},
annotation: Some(
Await(
ExprAwait {
range: 301..308,
value: NumberLiteral(
ExprNumberLiteral {
range: 307..308,
value: Int(
1,
),
},
),
},
),
),
},
default: None,
},
],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
Expr(
StmtExpr {
range: 312..315,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 312..315,
},
),
},