From 127a45622f707789718a0abb1299a570bb023c21 Mon Sep 17 00:00:00 2001 From: Brent Westbrook <36778786+ntBre@users.noreply.github.com> Date: Tue, 8 Apr 2025 08:55:43 -0400 Subject: [PATCH] [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. --- .../inline/err/invalid_annotation_class.py | 2 + .../inline/err/invalid_annotation_function.py | 6 + .../err/invalid_annotation_function_py314.py | 3 + .../err/invalid_annotation_type_alias.py | 2 + .../inline/ok/valid_annotation_class.py | 2 + ....py => valid_annotation_function_py313.py} | 4 + .../ruff_python_parser/src/semantic_errors.rs | 33 +- ...id_syntax@invalid_annotation_class.py.snap | 138 ++- ...syntax@invalid_annotation_function.py.snap | 945 +++++++++++++----- ...@invalid_annotation_function_py314.py.snap | 164 ++- ...ntax@invalid_annotation_type_alias.py.snap | 99 +- ...alid_syntax@valid_annotation_class.py.snap | 69 +- ...x@valid_annotation_function_py313.py.snap} | 246 +++-- 13 files changed, 1399 insertions(+), 314 deletions(-) rename crates/ruff_python_parser/resources/inline/ok/{valid_annotation_function.py => valid_annotation_function_py313.py} (60%) rename crates/ruff_python_parser/tests/snapshots/{valid_syntax@valid_annotation_function.py.snap => valid_syntax@valid_annotation_function_py313.py.snap} (61%) diff --git a/crates/ruff_python_parser/resources/inline/err/invalid_annotation_class.py b/crates/ruff_python_parser/resources/inline/err/invalid_annotation_class.py index d5de922472..9e484e34a4 100644 --- a/crates/ruff_python_parser/resources/inline/err/invalid_annotation_class.py +++ b/crates/ruff_python_parser/resources/inline/err/invalid_annotation_class.py @@ -3,3 +3,5 @@ class I[T]((yield 1)): ... class J[T]((yield from 1)): ... class K[T: (yield 1)]: ... # yield in TypeVar class L[T: (x := 1)]: ... # named expr in TypeVar +class M[T]((await 1)): ... +class N[T: (await 1)]: ... diff --git a/crates/ruff_python_parser/resources/inline/err/invalid_annotation_function.py b/crates/ruff_python_parser/resources/inline/err/invalid_annotation_function.py index c16c9c0ff9..91f354ef5c 100644 --- a/crates/ruff_python_parser/resources/inline/err/invalid_annotation_function.py +++ b/crates/ruff_python_parser/resources/inline/err/invalid_annotation_function.py @@ -1,3 +1,5 @@ +def d[T]() -> (await 1): ... +def e[T](arg: (await 1)): ... def f[T]() -> (y := 3): ... def g[T](arg: (x := 1)): ... def h[T](x: (yield 1)): ... @@ -12,3 +14,7 @@ def t[T: (x := 1)](): ... # named expr in TypeVar bound def u[T = (x := 1)](): ... # named expr in TypeVar default def v[*Ts = (x := 1)](): ... # named expr in TypeVarTuple default def w[**Ts = (x := 1)](): ... # named expr in ParamSpec default +def t[T: (await 1)](): ... # await in TypeVar bound +def u[T = (await 1)](): ... # await in TypeVar default +def v[*Ts = (await 1)](): ... # await in TypeVarTuple default +def w[**Ts = (await 1)](): ... # await in ParamSpec default diff --git a/crates/ruff_python_parser/resources/inline/err/invalid_annotation_function_py314.py b/crates/ruff_python_parser/resources/inline/err/invalid_annotation_function_py314.py index 8944d1f6f1..4a0193b173 100644 --- a/crates/ruff_python_parser/resources/inline/err/invalid_annotation_function_py314.py +++ b/crates/ruff_python_parser/resources/inline/err/invalid_annotation_function_py314.py @@ -6,3 +6,6 @@ def outer(): def k() -> (yield 1): ... def m(x: (yield from 1)): ... def o() -> (yield from 1): ... +async def outer(): + def f() -> (await 1): ... + def g(arg: (await 1)): ... diff --git a/crates/ruff_python_parser/resources/inline/err/invalid_annotation_type_alias.py b/crates/ruff_python_parser/resources/inline/err/invalid_annotation_type_alias.py index 4e6cc04d11..1ce79ce4de 100644 --- a/crates/ruff_python_parser/resources/inline/err/invalid_annotation_type_alias.py +++ b/crates/ruff_python_parser/resources/inline/err/invalid_annotation_type_alias.py @@ -4,3 +4,5 @@ type X[*Ts = (yield 1)] = int # TypeVarTuple default type X[**Ts = (yield 1)] = int # ParamSpec default type Y = (yield 1) # yield in value type Y = (x := 1) # named expr in value +type Y[T: (await 1)] = int # await in bound +type Y = (await 1) # await in value diff --git a/crates/ruff_python_parser/resources/inline/ok/valid_annotation_class.py b/crates/ruff_python_parser/resources/inline/ok/valid_annotation_class.py index da0d319440..e8baa9996a 100644 --- a/crates/ruff_python_parser/resources/inline/ok/valid_annotation_class.py +++ b/crates/ruff_python_parser/resources/inline/ok/valid_annotation_class.py @@ -2,3 +2,5 @@ class F(y := list): ... def f(): class G((yield 1)): ... class H((yield from 1)): ... +async def f(): + class G((await 1)): ... diff --git a/crates/ruff_python_parser/resources/inline/ok/valid_annotation_function.py b/crates/ruff_python_parser/resources/inline/ok/valid_annotation_function_py313.py similarity index 60% rename from crates/ruff_python_parser/resources/inline/ok/valid_annotation_function.py rename to crates/ruff_python_parser/resources/inline/ok/valid_annotation_function_py313.py index 3f370741ab..366d224295 100644 --- a/crates/ruff_python_parser/resources/inline/ok/valid_annotation_function.py +++ b/crates/ruff_python_parser/resources/inline/ok/valid_annotation_function_py313.py @@ -1,3 +1,4 @@ +# parse_options: {"target-version": "3.13"} def f() -> (y := 3): ... def g(arg: (x := 1)): ... def outer(): @@ -5,3 +6,6 @@ def outer(): def k() -> (yield 1): ... def m(x: (yield from 1)): ... def o() -> (yield from 1): ... +async def outer(): + def f() -> (await 1): ... + def g(arg: (await 1)): ... diff --git a/crates/ruff_python_parser/src/semantic_errors.rs b/crates/ruff_python_parser/src/semantic_errors.rs index e7cfb48324..07a4e516c4 100644 --- a/crates/ruff_python_parser/src/semantic_errors.rs +++ b/crates/ruff_python_parser/src/semantic_errors.rs @@ -125,7 +125,8 @@ impl SemanticSyntaxChecker { returns, .. }) => { - // test_ok valid_annotation_function + // test_ok valid_annotation_function_py313 + // # parse_options: {"target-version": "3.13"} // def f() -> (y := 3): ... // def g(arg: (x := 1)): ... // def outer(): @@ -133,6 +134,9 @@ impl SemanticSyntaxChecker { // def k() -> (yield 1): ... // def m(x: (yield from 1)): ... // def o() -> (yield from 1): ... + // async def outer(): + // def f() -> (await 1): ... + // def g(arg: (await 1)): ... // test_err invalid_annotation_function_py314 // # parse_options: {"target-version": "3.14"} @@ -143,8 +147,13 @@ impl SemanticSyntaxChecker { // def k() -> (yield 1): ... // def m(x: (yield from 1)): ... // def o() -> (yield from 1): ... + // async def outer(): + // def f() -> (await 1): ... + // def g(arg: (await 1)): ... // test_err invalid_annotation_function + // def d[T]() -> (await 1): ... + // def e[T](arg: (await 1)): ... // def f[T]() -> (y := 3): ... // def g[T](arg: (x := 1)): ... // def h[T](x: (yield 1)): ... @@ -159,6 +168,10 @@ impl SemanticSyntaxChecker { // def u[T = (x := 1)](): ... # named expr in TypeVar default // def v[*Ts = (x := 1)](): ... # named expr in TypeVarTuple default // def w[**Ts = (x := 1)](): ... # named expr in ParamSpec default + // def t[T: (await 1)](): ... # await in TypeVar bound + // def u[T = (await 1)](): ... # await in TypeVar default + // def v[*Ts = (await 1)](): ... # await in TypeVarTuple default + // def w[**Ts = (await 1)](): ... # await in ParamSpec default let mut visitor = InvalidExpressionVisitor { position: InvalidExpressionPosition::TypeAnnotation, ctx, @@ -194,6 +207,8 @@ impl SemanticSyntaxChecker { // def f(): // class G((yield 1)): ... // class H((yield from 1)): ... + // async def f(): + // class G((await 1)): ... // test_err invalid_annotation_class // class F[T](y := list): ... @@ -201,6 +216,8 @@ impl SemanticSyntaxChecker { // class J[T]((yield from 1)): ... // class K[T: (yield 1)]: ... # yield in TypeVar // class L[T: (x := 1)]: ... # named expr in TypeVar + // class M[T]((await 1)): ... + // class N[T: (await 1)]: ... let mut visitor = InvalidExpressionVisitor { position: InvalidExpressionPosition::TypeAnnotation, ctx, @@ -221,6 +238,8 @@ impl SemanticSyntaxChecker { // type X[**Ts = (yield 1)] = int # ParamSpec default // type Y = (yield 1) # yield in value // type Y = (x := 1) # named expr in value + // type Y[T: (await 1)] = int # await in bound + // type Y = (await 1) # await in value let mut visitor = InvalidExpressionVisitor { position: InvalidExpressionPosition::TypeAlias, ctx, @@ -878,6 +897,7 @@ impl Display for InvalidExpressionPosition { pub enum InvalidExpressionKind { Yield, NamedExpr, + Await, } impl Display for InvalidExpressionKind { @@ -885,6 +905,7 @@ impl Display for InvalidExpressionKind { f.write_str(match self { InvalidExpressionKind::Yield => "yield expression", InvalidExpressionKind::NamedExpr => "named expression", + InvalidExpressionKind::Await => "await expression", }) } } @@ -1115,6 +1136,16 @@ where *range, ); } + Expr::Await(ast::ExprAwait { range, .. }) => { + SemanticSyntaxChecker::add_error( + self.ctx, + SemanticSyntaxErrorKind::InvalidExpression( + InvalidExpressionKind::Await, + self.position, + ), + *range, + ); + } _ => {} } ast::visitor::walk_expr(self, expr); diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_annotation_class.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_annotation_class.py.snap index a9d4fdad90..a53ebf2fd2 100644 --- a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_annotation_class.py.snap +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_annotation_class.py.snap @@ -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 | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_annotation_function.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_annotation_function.py.snap index 24ce2db115..b21adb9b16 100644 --- a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_annotation_function.py.snap +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_annotation_function.py.snap @@ -7,15 +7,15 @@ input_file: crates/ruff_python_parser/resources/inline/err/invalid_annotation_fu ``` Module( ModModule { - range: 0..683, + range: 0..987, body: [ FunctionDef( StmtFunctionDef { - range: 0..27, + range: 0..28, is_async: false, decorator_list: [], name: Identifier { - id: Name("f"), + id: Name("d"), range: 4..5, }, type_params: Some( @@ -45,21 +45,14 @@ Module( kwarg: None, }, returns: Some( - Named( - ExprNamed { - range: 15..21, - target: Name( - ExprName { - range: 15..16, - id: Name("y"), - ctx: Store, - }, - ), + Await( + ExprAwait { + range: 15..22, value: NumberLiteral( ExprNumberLiteral { - range: 20..21, + range: 21..22, value: Int( - 3, + 1, ), }, ), @@ -69,10 +62,10 @@ Module( body: [ Expr( StmtExpr { - range: 24..27, + range: 25..28, value: EllipsisLiteral( ExprEllipsisLiteral { - range: 24..27, + range: 25..28, }, ), }, @@ -82,23 +75,23 @@ Module( ), FunctionDef( StmtFunctionDef { - range: 28..56, + range: 29..58, is_async: false, decorator_list: [], name: Identifier { - id: Name("g"), - range: 32..33, + id: Name("e"), + range: 33..34, }, type_params: Some( TypeParams { - range: 33..36, + range: 34..37, type_params: [ TypeVar( TypeParamTypeVar { - range: 34..35, + range: 35..36, name: Identifier { id: Name("T"), - range: 34..35, + range: 35..36, }, bound: None, default: None, @@ -108,31 +101,24 @@ Module( }, ), parameters: Parameters { - range: 36..51, + range: 37..53, posonlyargs: [], args: [ ParameterWithDefault { - range: 37..50, + range: 38..52, parameter: Parameter { - range: 37..50, + range: 38..52, name: Identifier { id: Name("arg"), - range: 37..40, + range: 38..41, }, annotation: Some( - Named( - ExprNamed { - range: 43..49, - target: Name( - ExprName { - range: 43..44, - id: Name("x"), - ctx: Store, - }, - ), + Await( + ExprAwait { + range: 44..51, value: NumberLiteral( ExprNumberLiteral { - range: 48..49, + range: 50..51, value: Int( 1, ), @@ -153,10 +139,10 @@ Module( body: [ Expr( StmtExpr { - range: 53..56, + range: 55..58, value: EllipsisLiteral( ExprEllipsisLiteral { - range: 53..56, + range: 55..58, }, ), }, @@ -166,23 +152,23 @@ Module( ), FunctionDef( StmtFunctionDef { - range: 57..84, + range: 59..86, is_async: false, decorator_list: [], name: Identifier { - id: Name("h"), - range: 61..62, + id: Name("f"), + range: 63..64, }, type_params: Some( TypeParams { - range: 62..65, + range: 64..67, type_params: [ TypeVar( TypeParamTypeVar { - range: 63..64, + range: 65..66, name: Identifier { id: Name("T"), - range: 63..64, + range: 65..66, }, bound: None, default: None, @@ -192,25 +178,180 @@ Module( }, ), parameters: Parameters { - range: 65..79, + range: 67..69, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Named( + ExprNamed { + range: 74..80, + target: Name( + ExprName { + range: 74..75, + id: Name("y"), + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 79..80, + value: Int( + 3, + ), + }, + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 83..86, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 83..86, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 87..115, + is_async: false, + decorator_list: [], + name: Identifier { + id: Name("g"), + range: 91..92, + }, + type_params: Some( + TypeParams { + range: 92..95, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 93..94, + name: Identifier { + id: Name("T"), + range: 93..94, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 95..110, posonlyargs: [], args: [ ParameterWithDefault { - range: 66..78, + range: 96..109, parameter: Parameter { - range: 66..78, + range: 96..109, + name: Identifier { + id: Name("arg"), + range: 96..99, + }, + annotation: Some( + Named( + ExprNamed { + range: 102..108, + target: Name( + ExprName { + range: 102..103, + id: Name("x"), + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 107..108, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 112..115, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 112..115, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 116..143, + is_async: false, + decorator_list: [], + name: Identifier { + id: Name("h"), + range: 120..121, + }, + type_params: Some( + TypeParams { + range: 121..124, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 122..123, + name: Identifier { + id: Name("T"), + range: 122..123, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 124..138, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 125..137, + parameter: Parameter { + range: 125..137, name: Identifier { id: Name("x"), - range: 66..67, + range: 125..126, }, annotation: Some( Yield( ExprYield { - range: 70..77, + range: 129..136, value: Some( NumberLiteral( ExprNumberLiteral { - range: 76..77, + range: 135..136, value: Int( 1, ), @@ -232,10 +373,10 @@ Module( body: [ Expr( StmtExpr { - range: 81..84, + range: 140..143, value: EllipsisLiteral( ExprEllipsisLiteral { - range: 81..84, + range: 140..143, }, ), }, @@ -245,23 +386,23 @@ Module( ), FunctionDef( StmtFunctionDef { - range: 85..113, + range: 144..172, is_async: false, decorator_list: [], name: Identifier { id: Name("j"), - range: 89..90, + range: 148..149, }, type_params: Some( TypeParams { - range: 90..93, + range: 149..152, type_params: [ TypeVar( TypeParamTypeVar { - range: 91..92, + range: 150..151, name: Identifier { id: Name("T"), - range: 91..92, + range: 150..151, }, bound: None, default: None, @@ -271,7 +412,7 @@ Module( }, ), parameters: Parameters { - range: 93..95, + range: 152..154, posonlyargs: [], args: [], vararg: None, @@ -281,11 +422,11 @@ Module( returns: Some( Yield( ExprYield { - range: 100..107, + range: 159..166, value: Some( NumberLiteral( ExprNumberLiteral { - range: 106..107, + range: 165..166, value: Int( 1, ), @@ -298,10 +439,10 @@ Module( body: [ Expr( StmtExpr { - range: 110..113, + range: 169..172, value: EllipsisLiteral( ExprEllipsisLiteral { - range: 110..113, + range: 169..172, }, ), }, @@ -311,23 +452,23 @@ Module( ), FunctionDef( StmtFunctionDef { - range: 114..146, + range: 173..205, is_async: false, decorator_list: [], name: Identifier { id: Name("l"), - range: 118..119, + range: 177..178, }, type_params: Some( TypeParams { - range: 119..122, + range: 178..181, type_params: [ TypeVar( TypeParamTypeVar { - range: 120..121, + range: 179..180, name: Identifier { id: Name("T"), - range: 120..121, + range: 179..180, }, bound: None, default: None, @@ -337,24 +478,24 @@ Module( }, ), parameters: Parameters { - range: 122..141, + range: 181..200, posonlyargs: [], args: [ ParameterWithDefault { - range: 123..140, + range: 182..199, parameter: Parameter { - range: 123..140, + range: 182..199, name: Identifier { id: Name("x"), - range: 123..124, + range: 182..183, }, annotation: Some( YieldFrom( ExprYieldFrom { - range: 127..139, + range: 186..198, value: NumberLiteral( ExprNumberLiteral { - range: 138..139, + range: 197..198, value: Int( 1, ), @@ -375,10 +516,10 @@ Module( body: [ Expr( StmtExpr { - range: 143..146, + range: 202..205, value: EllipsisLiteral( ExprEllipsisLiteral { - range: 143..146, + range: 202..205, }, ), }, @@ -388,23 +529,23 @@ Module( ), FunctionDef( StmtFunctionDef { - range: 147..180, + range: 206..239, is_async: false, decorator_list: [], name: Identifier { id: Name("n"), - range: 151..152, + range: 210..211, }, type_params: Some( TypeParams { - range: 152..155, + range: 211..214, type_params: [ TypeVar( TypeParamTypeVar { - range: 153..154, + range: 212..213, name: Identifier { id: Name("T"), - range: 153..154, + range: 212..213, }, bound: None, default: None, @@ -414,7 +555,7 @@ Module( }, ), parameters: Parameters { - range: 155..157, + range: 214..216, posonlyargs: [], args: [], vararg: None, @@ -424,10 +565,10 @@ Module( returns: Some( YieldFrom( ExprYieldFrom { - range: 162..174, + range: 221..233, value: NumberLiteral( ExprNumberLiteral { - range: 173..174, + range: 232..233, value: Int( 1, ), @@ -439,10 +580,10 @@ Module( body: [ Expr( StmtExpr { - range: 177..180, + range: 236..239, value: EllipsisLiteral( ExprEllipsisLiteral { - range: 177..180, + range: 236..239, }, ), }, @@ -452,32 +593,32 @@ Module( ), FunctionDef( StmtFunctionDef { - range: 181..207, + range: 240..266, is_async: false, decorator_list: [], name: Identifier { id: Name("p"), - range: 185..186, + range: 244..245, }, type_params: Some( TypeParams { - range: 186..200, + range: 245..259, type_params: [ TypeVar( TypeParamTypeVar { - range: 187..199, + range: 246..258, name: Identifier { id: Name("T"), - range: 187..188, + range: 246..247, }, bound: Some( Yield( ExprYield { - range: 191..198, + range: 250..257, value: Some( NumberLiteral( ExprNumberLiteral { - range: 197..198, + range: 256..257, value: Int( 1, ), @@ -494,7 +635,7 @@ Module( }, ), parameters: Parameters { - range: 200..202, + range: 259..261, posonlyargs: [], args: [], vararg: None, @@ -505,10 +646,10 @@ Module( body: [ Expr( StmtExpr { - range: 204..207, + range: 263..266, value: EllipsisLiteral( ExprEllipsisLiteral { - range: 204..207, + range: 263..266, }, ), }, @@ -518,33 +659,33 @@ Module( ), FunctionDef( StmtFunctionDef { - range: 238..265, + range: 297..324, is_async: false, decorator_list: [], name: Identifier { id: Name("q"), - range: 242..243, + range: 301..302, }, type_params: Some( TypeParams { - range: 243..258, + range: 302..317, type_params: [ TypeVar( TypeParamTypeVar { - range: 244..257, + range: 303..316, name: Identifier { id: Name("T"), - range: 244..245, + range: 303..304, }, bound: None, default: Some( Yield( ExprYield { - range: 249..256, + range: 308..315, value: Some( NumberLiteral( ExprNumberLiteral { - range: 255..256, + range: 314..315, value: Int( 1, ), @@ -560,7 +701,7 @@ Module( }, ), parameters: Parameters { - range: 258..260, + range: 317..319, posonlyargs: [], args: [], vararg: None, @@ -571,10 +712,10 @@ Module( body: [ Expr( StmtExpr { - range: 262..265, + range: 321..324, value: EllipsisLiteral( ExprEllipsisLiteral { - range: 262..265, + range: 321..324, }, ), }, @@ -584,32 +725,32 @@ Module( ), FunctionDef( StmtFunctionDef { - range: 297..326, + range: 356..385, is_async: false, decorator_list: [], name: Identifier { id: Name("r"), - range: 301..302, + range: 360..361, }, type_params: Some( TypeParams { - range: 302..319, + range: 361..378, type_params: [ TypeVarTuple( TypeParamTypeVarTuple { - range: 303..318, + range: 362..377, name: Identifier { id: Name("Ts"), - range: 304..306, + range: 363..365, }, default: Some( Yield( ExprYield { - range: 310..317, + range: 369..376, value: Some( NumberLiteral( ExprNumberLiteral { - range: 316..317, + range: 375..376, value: Int( 1, ), @@ -625,7 +766,7 @@ Module( }, ), parameters: Parameters { - range: 319..321, + range: 378..380, posonlyargs: [], args: [], vararg: None, @@ -636,10 +777,10 @@ Module( body: [ Expr( StmtExpr { - range: 323..326, + range: 382..385, value: EllipsisLiteral( ExprEllipsisLiteral { - range: 323..326, + range: 382..385, }, ), }, @@ -649,32 +790,32 @@ Module( ), FunctionDef( StmtFunctionDef { - range: 361..391, + range: 420..450, is_async: false, decorator_list: [], name: Identifier { id: Name("s"), - range: 365..366, + range: 424..425, }, type_params: Some( TypeParams { - range: 366..384, + range: 425..443, type_params: [ ParamSpec( TypeParamParamSpec { - range: 367..383, + range: 426..442, name: Identifier { id: Name("Ts"), - range: 369..371, + range: 428..430, }, default: Some( Yield( ExprYield { - range: 375..382, + range: 434..441, value: Some( NumberLiteral( ExprNumberLiteral { - range: 381..382, + range: 440..441, value: Int( 1, ), @@ -690,7 +831,7 @@ Module( }, ), parameters: Parameters { - range: 384..386, + range: 443..445, posonlyargs: [], args: [], vararg: None, @@ -701,10 +842,10 @@ Module( body: [ Expr( StmtExpr { - range: 388..391, + range: 447..450, value: EllipsisLiteral( ExprEllipsisLiteral { - range: 388..391, + range: 447..450, }, ), }, @@ -714,38 +855,38 @@ Module( ), FunctionDef( StmtFunctionDef { - range: 422..447, + range: 481..506, is_async: false, decorator_list: [], name: Identifier { id: Name("t"), - range: 426..427, + range: 485..486, }, type_params: Some( TypeParams { - range: 427..440, + range: 486..499, type_params: [ TypeVar( TypeParamTypeVar { - range: 428..439, + range: 487..498, name: Identifier { id: Name("T"), - range: 428..429, + range: 487..488, }, bound: Some( Named( ExprNamed { - range: 432..438, + range: 491..497, target: Name( ExprName { - range: 432..433, + range: 491..492, id: Name("x"), ctx: Store, }, ), value: NumberLiteral( ExprNumberLiteral { - range: 437..438, + range: 496..497, value: Int( 1, ), @@ -761,7 +902,7 @@ Module( }, ), parameters: Parameters { - range: 440..442, + range: 499..501, posonlyargs: [], args: [], vararg: None, @@ -772,10 +913,10 @@ Module( body: [ Expr( StmtExpr { - range: 444..447, + range: 503..506, value: EllipsisLiteral( ExprEllipsisLiteral { - range: 444..447, + range: 503..506, }, ), }, @@ -785,39 +926,39 @@ Module( ), FunctionDef( StmtFunctionDef { - range: 484..510, + range: 543..569, is_async: false, decorator_list: [], name: Identifier { id: Name("u"), - range: 488..489, + range: 547..548, }, type_params: Some( TypeParams { - range: 489..503, + range: 548..562, type_params: [ TypeVar( TypeParamTypeVar { - range: 490..502, + range: 549..561, name: Identifier { id: Name("T"), - range: 490..491, + range: 549..550, }, bound: None, default: Some( Named( ExprNamed { - range: 495..501, + range: 554..560, target: Name( ExprName { - range: 495..496, + range: 554..555, id: Name("x"), ctx: Store, }, ), value: NumberLiteral( ExprNumberLiteral { - range: 500..501, + range: 559..560, value: Int( 1, ), @@ -832,7 +973,7 @@ Module( }, ), parameters: Parameters { - range: 503..505, + range: 562..564, posonlyargs: [], args: [], vararg: None, @@ -843,10 +984,10 @@ Module( body: [ Expr( StmtExpr { - range: 507..510, + range: 566..569, value: EllipsisLiteral( ExprEllipsisLiteral { - range: 507..510, + range: 566..569, }, ), }, @@ -856,38 +997,38 @@ Module( ), FunctionDef( StmtFunctionDef { - range: 548..576, + range: 607..635, is_async: false, decorator_list: [], name: Identifier { id: Name("v"), - range: 552..553, + range: 611..612, }, type_params: Some( TypeParams { - range: 553..569, + range: 612..628, type_params: [ TypeVarTuple( TypeParamTypeVarTuple { - range: 554..568, + range: 613..627, name: Identifier { id: Name("Ts"), - range: 555..557, + range: 614..616, }, default: Some( Named( ExprNamed { - range: 561..567, + range: 620..626, target: Name( ExprName { - range: 561..562, + range: 620..621, id: Name("x"), ctx: Store, }, ), value: NumberLiteral( ExprNumberLiteral { - range: 566..567, + range: 625..626, value: Int( 1, ), @@ -902,7 +1043,7 @@ Module( }, ), parameters: Parameters { - range: 569..571, + range: 628..630, posonlyargs: [], args: [], vararg: None, @@ -913,10 +1054,10 @@ Module( body: [ Expr( StmtExpr { - range: 573..576, + range: 632..635, value: EllipsisLiteral( ExprEllipsisLiteral { - range: 573..576, + range: 632..635, }, ), }, @@ -926,38 +1067,38 @@ Module( ), FunctionDef( StmtFunctionDef { - range: 617..646, + range: 676..705, is_async: false, decorator_list: [], name: Identifier { id: Name("w"), - range: 621..622, + range: 680..681, }, type_params: Some( TypeParams { - range: 622..639, + range: 681..698, type_params: [ ParamSpec( TypeParamParamSpec { - range: 623..638, + range: 682..697, name: Identifier { id: Name("Ts"), - range: 625..627, + range: 684..686, }, default: Some( Named( ExprNamed { - range: 631..637, + range: 690..696, target: Name( ExprName { - range: 631..632, + range: 690..691, id: Name("x"), ctx: Store, }, ), value: NumberLiteral( ExprNumberLiteral { - range: 636..637, + range: 695..696, value: Int( 1, ), @@ -972,7 +1113,7 @@ Module( }, ), parameters: Parameters { - range: 639..641, + range: 698..700, posonlyargs: [], args: [], vararg: None, @@ -983,10 +1124,264 @@ Module( body: [ Expr( StmtExpr { - range: 643..646, + range: 702..705, value: EllipsisLiteral( ExprEllipsisLiteral { - range: 643..646, + range: 702..705, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 742..768, + is_async: false, + decorator_list: [], + name: Identifier { + id: Name("t"), + range: 746..747, + }, + type_params: Some( + TypeParams { + range: 747..761, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 748..760, + name: Identifier { + id: Name("T"), + range: 748..749, + }, + bound: Some( + Await( + ExprAwait { + range: 752..759, + value: NumberLiteral( + ExprNumberLiteral { + range: 758..759, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + default: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 761..763, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 765..768, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 765..768, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 800..827, + is_async: false, + decorator_list: [], + name: Identifier { + id: Name("u"), + range: 804..805, + }, + type_params: Some( + TypeParams { + range: 805..820, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 806..819, + name: Identifier { + id: Name("T"), + range: 806..807, + }, + bound: None, + default: Some( + Await( + ExprAwait { + range: 811..818, + value: NumberLiteral( + ExprNumberLiteral { + range: 817..818, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + }, + ), + ], + }, + ), + parameters: Parameters { + range: 820..822, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 824..827, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 824..827, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 860..889, + is_async: false, + decorator_list: [], + name: Identifier { + id: Name("v"), + range: 864..865, + }, + type_params: Some( + TypeParams { + range: 865..882, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 866..881, + name: Identifier { + id: Name("Ts"), + range: 867..869, + }, + default: Some( + Await( + ExprAwait { + range: 873..880, + value: NumberLiteral( + ExprNumberLiteral { + range: 879..880, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + }, + ), + ], + }, + ), + parameters: Parameters { + range: 882..884, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 886..889, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 886..889, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 925..955, + is_async: false, + decorator_list: [], + name: Identifier { + id: Name("w"), + range: 929..930, + }, + type_params: Some( + TypeParams { + range: 930..948, + type_params: [ + ParamSpec( + TypeParamParamSpec { + range: 931..947, + name: Identifier { + id: Name("Ts"), + range: 933..935, + }, + default: Some( + Await( + ExprAwait { + range: 939..946, + value: NumberLiteral( + ExprNumberLiteral { + range: 945..946, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + }, + ), + ], + }, + ), + parameters: Parameters { + range: 948..950, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 952..955, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 952..955, }, ), }, @@ -1001,134 +1396,194 @@ Module( ## Semantic Syntax Errors | -1 | def f[T]() -> (y := 3): ... +1 | def d[T]() -> (await 1): ... + | ^^^^^^^ Syntax Error: await expression cannot be used within a generic definition +2 | def e[T](arg: (await 1)): ... +3 | def f[T]() -> (y := 3): ... + | + + + | +1 | def d[T]() -> (await 1): ... +2 | def e[T](arg: (await 1)): ... + | ^^^^^^^ Syntax Error: await expression cannot be used within a generic definition +3 | def f[T]() -> (y := 3): ... +4 | def g[T](arg: (x := 1)): ... + | + + + | +1 | def d[T]() -> (await 1): ... +2 | def e[T](arg: (await 1)): ... +3 | def f[T]() -> (y := 3): ... | ^^^^^^ Syntax Error: named expression cannot be used within a generic definition -2 | def g[T](arg: (x := 1)): ... -3 | def h[T](x: (yield 1)): ... +4 | def g[T](arg: (x := 1)): ... +5 | def h[T](x: (yield 1)): ... | | -1 | def f[T]() -> (y := 3): ... -2 | def g[T](arg: (x := 1)): ... +2 | def e[T](arg: (await 1)): ... +3 | def f[T]() -> (y := 3): ... +4 | def g[T](arg: (x := 1)): ... | ^^^^^^ Syntax Error: named expression cannot be used within a generic definition -3 | def h[T](x: (yield 1)): ... -4 | def j[T]() -> (yield 1): ... +5 | def h[T](x: (yield 1)): ... +6 | def j[T]() -> (yield 1): ... | | -1 | def f[T]() -> (y := 3): ... -2 | def g[T](arg: (x := 1)): ... -3 | def h[T](x: (yield 1)): ... +3 | def f[T]() -> (y := 3): ... +4 | def g[T](arg: (x := 1)): ... +5 | def h[T](x: (yield 1)): ... | ^^^^^^^ Syntax Error: yield expression cannot be used within a generic definition -4 | def j[T]() -> (yield 1): ... -5 | def l[T](x: (yield from 1)): ... +6 | def j[T]() -> (yield 1): ... +7 | def l[T](x: (yield from 1)): ... | | -2 | def g[T](arg: (x := 1)): ... -3 | def h[T](x: (yield 1)): ... -4 | def j[T]() -> (yield 1): ... +4 | def g[T](arg: (x := 1)): ... +5 | def h[T](x: (yield 1)): ... +6 | def j[T]() -> (yield 1): ... | ^^^^^^^ Syntax Error: yield expression cannot be used within a generic definition -5 | def l[T](x: (yield from 1)): ... -6 | def n[T]() -> (yield from 1): ... +7 | def l[T](x: (yield from 1)): ... +8 | def n[T]() -> (yield from 1): ... | | -3 | def h[T](x: (yield 1)): ... -4 | def j[T]() -> (yield 1): ... -5 | def l[T](x: (yield from 1)): ... +5 | def h[T](x: (yield 1)): ... +6 | def j[T]() -> (yield 1): ... +7 | def l[T](x: (yield from 1)): ... | ^^^^^^^^^^^^ Syntax Error: yield expression cannot be used within a generic definition -6 | def n[T]() -> (yield from 1): ... -7 | def p[T: (yield 1)](): ... # yield in TypeVar bound - | - - - | -4 | def j[T]() -> (yield 1): ... -5 | def l[T](x: (yield from 1)): ... -6 | def n[T]() -> (yield from 1): ... - | ^^^^^^^^^^^^ Syntax Error: yield expression cannot be used within a generic definition -7 | def p[T: (yield 1)](): ... # yield in TypeVar bound -8 | def q[T = (yield 1)](): ... # yield in TypeVar default - | - - - | -5 | def l[T](x: (yield from 1)): ... -6 | def n[T]() -> (yield from 1): ... -7 | def p[T: (yield 1)](): ... # yield in TypeVar bound - | ^^^^^^^ Syntax Error: yield expression cannot be used within a TypeVar bound -8 | def q[T = (yield 1)](): ... # yield in TypeVar default -9 | def r[*Ts = (yield 1)](): ... # yield in TypeVarTuple default +8 | def n[T]() -> (yield from 1): ... +9 | def p[T: (yield 1)](): ... # yield in TypeVar bound | | - 6 | def n[T]() -> (yield from 1): ... - 7 | def p[T: (yield 1)](): ... # yield in TypeVar bound - 8 | def q[T = (yield 1)](): ... # yield in TypeVar default + 6 | def j[T]() -> (yield 1): ... + 7 | def l[T](x: (yield from 1)): ... + 8 | def n[T]() -> (yield from 1): ... + | ^^^^^^^^^^^^ Syntax Error: yield expression cannot be used within a generic definition + 9 | def p[T: (yield 1)](): ... # yield in TypeVar bound +10 | def q[T = (yield 1)](): ... # yield in TypeVar default + | + + + | + 7 | def l[T](x: (yield from 1)): ... + 8 | def n[T]() -> (yield from 1): ... + 9 | def p[T: (yield 1)](): ... # yield in TypeVar bound + | ^^^^^^^ Syntax Error: yield expression cannot be used within a TypeVar bound +10 | def q[T = (yield 1)](): ... # yield in TypeVar default +11 | def r[*Ts = (yield 1)](): ... # yield in TypeVarTuple default + | + + + | + 8 | def n[T]() -> (yield from 1): ... + 9 | def p[T: (yield 1)](): ... # yield in TypeVar bound +10 | def q[T = (yield 1)](): ... # yield in TypeVar default | ^^^^^^^ Syntax Error: yield expression cannot be used within a TypeVar default - 9 | def r[*Ts = (yield 1)](): ... # yield in TypeVarTuple default -10 | def s[**Ts = (yield 1)](): ... # yield in ParamSpec default +11 | def r[*Ts = (yield 1)](): ... # yield in TypeVarTuple default +12 | def s[**Ts = (yield 1)](): ... # yield in ParamSpec default | | - 7 | def p[T: (yield 1)](): ... # yield in TypeVar bound - 8 | def q[T = (yield 1)](): ... # yield in TypeVar default - 9 | def r[*Ts = (yield 1)](): ... # yield in TypeVarTuple default + 9 | def p[T: (yield 1)](): ... # yield in TypeVar bound +10 | def q[T = (yield 1)](): ... # yield in TypeVar default +11 | def r[*Ts = (yield 1)](): ... # yield in TypeVarTuple default | ^^^^^^^ Syntax Error: yield expression cannot be used within a TypeVarTuple default -10 | def s[**Ts = (yield 1)](): ... # yield in ParamSpec default -11 | def t[T: (x := 1)](): ... # named expr in TypeVar bound +12 | def s[**Ts = (yield 1)](): ... # yield in ParamSpec default +13 | def t[T: (x := 1)](): ... # named expr in TypeVar bound | | - 8 | def q[T = (yield 1)](): ... # yield in TypeVar default - 9 | def r[*Ts = (yield 1)](): ... # yield in TypeVarTuple default -10 | def s[**Ts = (yield 1)](): ... # yield in ParamSpec default +10 | def q[T = (yield 1)](): ... # yield in TypeVar default +11 | def r[*Ts = (yield 1)](): ... # yield in TypeVarTuple default +12 | def s[**Ts = (yield 1)](): ... # yield in ParamSpec default | ^^^^^^^ Syntax Error: yield expression cannot be used within a ParamSpec default -11 | def t[T: (x := 1)](): ... # named expr in TypeVar bound -12 | def u[T = (x := 1)](): ... # named expr in TypeVar default +13 | def t[T: (x := 1)](): ... # named expr in TypeVar bound +14 | def u[T = (x := 1)](): ... # named expr in TypeVar default | | - 9 | def r[*Ts = (yield 1)](): ... # yield in TypeVarTuple default -10 | def s[**Ts = (yield 1)](): ... # yield in ParamSpec default -11 | def t[T: (x := 1)](): ... # named expr in TypeVar bound +11 | def r[*Ts = (yield 1)](): ... # yield in TypeVarTuple default +12 | def s[**Ts = (yield 1)](): ... # yield in ParamSpec default +13 | def t[T: (x := 1)](): ... # named expr in TypeVar bound | ^^^^^^ Syntax Error: named expression cannot be used within a TypeVar bound -12 | def u[T = (x := 1)](): ... # named expr in TypeVar default -13 | def v[*Ts = (x := 1)](): ... # named expr in TypeVarTuple default +14 | def u[T = (x := 1)](): ... # named expr in TypeVar default +15 | def v[*Ts = (x := 1)](): ... # named expr in TypeVarTuple default | | -10 | def s[**Ts = (yield 1)](): ... # yield in ParamSpec default -11 | def t[T: (x := 1)](): ... # named expr in TypeVar bound -12 | def u[T = (x := 1)](): ... # named expr in TypeVar default +12 | def s[**Ts = (yield 1)](): ... # yield in ParamSpec default +13 | def t[T: (x := 1)](): ... # named expr in TypeVar bound +14 | def u[T = (x := 1)](): ... # named expr in TypeVar default | ^^^^^^ Syntax Error: named expression cannot be used within a TypeVar default -13 | def v[*Ts = (x := 1)](): ... # named expr in TypeVarTuple default -14 | def w[**Ts = (x := 1)](): ... # named expr in ParamSpec default +15 | def v[*Ts = (x := 1)](): ... # named expr in TypeVarTuple default +16 | def w[**Ts = (x := 1)](): ... # named expr in ParamSpec default | | -11 | def t[T: (x := 1)](): ... # named expr in TypeVar bound -12 | def u[T = (x := 1)](): ... # named expr in TypeVar default -13 | def v[*Ts = (x := 1)](): ... # named expr in TypeVarTuple default +13 | def t[T: (x := 1)](): ... # named expr in TypeVar bound +14 | def u[T = (x := 1)](): ... # named expr in TypeVar default +15 | def v[*Ts = (x := 1)](): ... # named expr in TypeVarTuple default | ^^^^^^ Syntax Error: named expression cannot be used within a TypeVarTuple default -14 | def w[**Ts = (x := 1)](): ... # named expr in ParamSpec default +16 | def w[**Ts = (x := 1)](): ... # named expr in ParamSpec default +17 | def t[T: (await 1)](): ... # await in TypeVar bound | | -12 | def u[T = (x := 1)](): ... # named expr in TypeVar default -13 | def v[*Ts = (x := 1)](): ... # named expr in TypeVarTuple default -14 | def w[**Ts = (x := 1)](): ... # named expr in ParamSpec default +14 | def u[T = (x := 1)](): ... # named expr in TypeVar default +15 | def v[*Ts = (x := 1)](): ... # named expr in TypeVarTuple default +16 | def w[**Ts = (x := 1)](): ... # named expr in ParamSpec default | ^^^^^^ Syntax Error: named expression cannot be used within a ParamSpec default +17 | def t[T: (await 1)](): ... # await in TypeVar bound +18 | def u[T = (await 1)](): ... # await in TypeVar default + | + + + | +15 | def v[*Ts = (x := 1)](): ... # named expr in TypeVarTuple default +16 | def w[**Ts = (x := 1)](): ... # named expr in ParamSpec default +17 | def t[T: (await 1)](): ... # await in TypeVar bound + | ^^^^^^^ Syntax Error: await expression cannot be used within a TypeVar bound +18 | def u[T = (await 1)](): ... # await in TypeVar default +19 | def v[*Ts = (await 1)](): ... # await in TypeVarTuple default + | + + + | +16 | def w[**Ts = (x := 1)](): ... # named expr in ParamSpec default +17 | def t[T: (await 1)](): ... # await in TypeVar bound +18 | def u[T = (await 1)](): ... # await in TypeVar default + | ^^^^^^^ Syntax Error: await expression cannot be used within a TypeVar default +19 | def v[*Ts = (await 1)](): ... # await in TypeVarTuple default +20 | def w[**Ts = (await 1)](): ... # await in ParamSpec default + | + + + | +17 | def t[T: (await 1)](): ... # await in TypeVar bound +18 | def u[T = (await 1)](): ... # await in TypeVar default +19 | def v[*Ts = (await 1)](): ... # await in TypeVarTuple default + | ^^^^^^^ Syntax Error: await expression cannot be used within a TypeVarTuple default +20 | def w[**Ts = (await 1)](): ... # await in ParamSpec default + | + + + | +18 | def u[T = (await 1)](): ... # await in TypeVar default +19 | def v[*Ts = (await 1)](): ... # await in TypeVarTuple default +20 | def w[**Ts = (await 1)](): ... # await in ParamSpec default + | ^^^^^^^ Syntax Error: await expression cannot be used within a ParamSpec default | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_annotation_function_py314.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_annotation_function_py314.py.snap index 90d1a25695..49533fb33f 100644 --- a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_annotation_function_py314.py.snap +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_annotation_function_py314.py.snap @@ -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 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_annotation_type_alias.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_annotation_type_alias.py.snap index e820cf9864..8b4e9638b5 100644 --- a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_annotation_type_alias.py.snap +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_annotation_type_alias.py.snap @@ -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 | diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@valid_annotation_class.py.snap b/crates/ruff_python_parser/tests/snapshots/valid_syntax@valid_annotation_class.py.snap index 384272df70..feeea973fb 100644 --- a/crates/ruff_python_parser/tests/snapshots/valid_syntax@valid_annotation_class.py.snap +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@valid_annotation_class.py.snap @@ -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, + }, + ), + }, + ), + ], + }, + ), + ], + }, + ), ], }, ) diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@valid_annotation_function.py.snap b/crates/ruff_python_parser/tests/snapshots/valid_syntax@valid_annotation_function_py313.py.snap similarity index 61% rename from crates/ruff_python_parser/tests/snapshots/valid_syntax@valid_annotation_function.py.snap rename to crates/ruff_python_parser/tests/snapshots/valid_syntax@valid_annotation_function_py313.py.snap index 249e0dce7b..1a58dffba2 100644 --- a/crates/ruff_python_parser/tests/snapshots/valid_syntax@valid_annotation_function.py.snap +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@valid_annotation_function_py313.py.snap @@ -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, }, ), },