[syntax-errors] Allow yield in base classes and annotations (#17206)

Summary
--

This PR fixes the issue pointed out by @JelleZijlstra in
https://github.com/astral-sh/ruff/pull/17101#issuecomment-2777480204.
Namely, I conflated two very different errors from CPython:

```pycon
>>> def m[T](x: (yield from 1)): ...
  File "<python-input-310>", line 1
    def m[T](x: (yield from 1)): ...
                 ^^^^^^^^^^^^
SyntaxError: yield expression cannot be used within the definition of a generic
>>> def m(x: (yield from 1)): ...
  File "<python-input-311>", line 1
    def m(x: (yield from 1)): ...
              ^^^^^^^^^^^^
SyntaxError: 'yield from' outside function
>>> def outer():
...     def m(x: (yield from 1)): ...
...
>>>
```

I thought the second error was the same as the first, but `yield` (and
`yield from`) is actually valid in this position when inside a function
scope. The same is true for base classes, as pointed out in the original
comment.

We don't currently raise an error for `yield` outside of a function, but
that should be handled separately.

On the upside, this had the benefit of removing the
`InvalidExpressionPosition::BaseClass` variant and the
`allow_named_expr` field from the visitor because they were both no
longer used.

Test Plan
--

Updated inline tests.
This commit is contained in:
Brent Westbrook 2025-04-04 13:48:28 -04:00 committed by GitHub
parent 33a56f198b
commit acc5662e8b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 1081 additions and 686 deletions

View file

@ -1,6 +1,4 @@
class F[T](y := list): ...
class G((yield 1)): ...
class H((yield from 1)): ...
class I[T]((yield 1)): ...
class J[T]((yield from 1)): ...
class K[T: (yield 1)]: ... # yield in TypeVar

View file

@ -1,13 +1,9 @@
def f[T]() -> (y := 3): ...
def g[T](arg: (x := 1)): ...
def h[T](x: (yield 1)): ...
def i(x: (yield 1)): ...
def j[T]() -> (yield 1): ...
def k() -> (yield 1): ...
def l[T](x: (yield from 1)): ...
def m(x: (yield from 1)): ...
def n[T]() -> (yield from 1): ...
def o() -> (yield from 1): ...
def p[T: (yield 1)](): ... # yield in TypeVar bound
def q[T = (yield 1)](): ... # yield in TypeVar default
def r[*Ts = (yield 1)](): ... # yield in TypeVarTuple default

View file

@ -0,0 +1,8 @@
# parse_options: {"target-version": "3.14"}
def f() -> (y := 3): ...
def g(arg: (x := 1)): ...
def outer():
def i(x: (yield 1)): ...
def k() -> (yield 1): ...
def m(x: (yield from 1)): ...
def o() -> (yield from 1): ...

View file

@ -1 +1,4 @@
class F(y := list): ...
def f():
class G((yield 1)): ...
class H((yield from 1)): ...

View file

@ -1,2 +1,7 @@
def f() -> (y := 3): ...
def g(arg: (x := 1)): ...
def outer():
def i(x: (yield 1)): ...
def k() -> (yield 1): ...
def m(x: (yield from 1)): ...
def o() -> (yield from 1): ...

View file

@ -128,18 +128,29 @@ impl SemanticSyntaxChecker {
// test_ok valid_annotation_function
// def f() -> (y := 3): ...
// def g(arg: (x := 1)): ...
// def outer():
// def i(x: (yield 1)): ...
// def k() -> (yield 1): ...
// def m(x: (yield from 1)): ...
// def o() -> (yield from 1): ...
// test_err invalid_annotation_function_py314
// # parse_options: {"target-version": "3.14"}
// def f() -> (y := 3): ...
// def g(arg: (x := 1)): ...
// def outer():
// def i(x: (yield 1)): ...
// def k() -> (yield 1): ...
// def m(x: (yield from 1)): ...
// def o() -> (yield from 1): ...
// test_err invalid_annotation_function
// def f[T]() -> (y := 3): ...
// def g[T](arg: (x := 1)): ...
// def h[T](x: (yield 1)): ...
// def i(x: (yield 1)): ...
// def j[T]() -> (yield 1): ...
// def k() -> (yield 1): ...
// def l[T](x: (yield from 1)): ...
// def m(x: (yield from 1)): ...
// def n[T]() -> (yield from 1): ...
// def o() -> (yield from 1): ...
// def p[T: (yield 1)](): ... # yield in TypeVar bound
// def q[T = (yield 1)](): ... # yield in TypeVar default
// def r[*Ts = (yield 1)](): ... # yield in TypeVarTuple default
@ -148,19 +159,20 @@ 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
let is_generic = type_params.is_some();
let mut visitor = InvalidExpressionVisitor {
allow_named_expr: !is_generic,
position: InvalidExpressionPosition::TypeAnnotation,
ctx,
};
if let Some(type_params) = type_params {
visitor.visit_type_params(type_params);
}
if is_generic {
// the __future__ annotation error takes precedence over the generic error
if ctx.future_annotations_or_stub() || ctx.python_version() > PythonVersion::PY313 {
visitor.position = InvalidExpressionPosition::TypeAnnotation;
} else if type_params.is_some() {
visitor.position = InvalidExpressionPosition::GenericDefinition;
} else {
visitor.position = InvalidExpressionPosition::TypeAnnotation;
return;
}
for param in parameters
.iter()
@ -173,36 +185,29 @@ impl SemanticSyntaxChecker {
}
}
Stmt::ClassDef(ast::StmtClassDef {
type_params,
type_params: Some(type_params),
arguments,
..
}) => {
// test_ok valid_annotation_class
// class F(y := list): ...
// def f():
// class G((yield 1)): ...
// class H((yield from 1)): ...
// test_err invalid_annotation_class
// class F[T](y := list): ...
// class G((yield 1)): ...
// class H((yield from 1)): ...
// 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
let is_generic = type_params.is_some();
let mut visitor = InvalidExpressionVisitor {
allow_named_expr: !is_generic,
position: InvalidExpressionPosition::TypeAnnotation,
ctx,
};
if let Some(type_params) = type_params {
visitor.visit_type_params(type_params);
}
if is_generic {
visitor.position = InvalidExpressionPosition::GenericDefinition;
} else {
visitor.position = InvalidExpressionPosition::BaseClass;
}
visitor.visit_type_params(type_params);
if let Some(arguments) = arguments {
visitor.position = InvalidExpressionPosition::GenericDefinition;
visitor.visit_arguments(arguments);
}
}
@ -217,7 +222,6 @@ impl SemanticSyntaxChecker {
// type Y = (yield 1) # yield in value
// type Y = (x := 1) # named expr in value
let mut visitor = InvalidExpressionVisitor {
allow_named_expr: false,
position: InvalidExpressionPosition::TypeAlias,
ctx,
};
@ -625,12 +629,6 @@ impl Display for SemanticSyntaxError {
write!(f, "cannot delete `__debug__` on Python {python_version} (syntax was removed in 3.9)")
}
},
SemanticSyntaxErrorKind::InvalidExpression(
kind,
InvalidExpressionPosition::BaseClass,
) => {
write!(f, "{kind} cannot be used as a base class")
}
SemanticSyntaxErrorKind::InvalidExpression(kind, position) => {
write!(f, "{kind} cannot be used within a {position}")
}
@ -858,7 +856,6 @@ pub enum InvalidExpressionPosition {
TypeVarTupleDefault,
ParamSpecDefault,
TypeAnnotation,
BaseClass,
GenericDefinition,
TypeAlias,
}
@ -872,7 +869,6 @@ impl Display for InvalidExpressionPosition {
InvalidExpressionPosition::ParamSpecDefault => "ParamSpec default",
InvalidExpressionPosition::TypeAnnotation => "type annotation",
InvalidExpressionPosition::GenericDefinition => "generic definition",
InvalidExpressionPosition::BaseClass => "base class",
InvalidExpressionPosition::TypeAlias => "type alias",
})
}
@ -1086,16 +1082,6 @@ impl<'a, Ctx: SemanticSyntaxContext> MatchPatternVisitor<'a, Ctx> {
}
struct InvalidExpressionVisitor<'a, Ctx> {
/// Allow named expressions (`x := ...`) to appear in annotations.
///
/// These are allowed in non-generic functions, for example:
///
/// ```python
/// def foo(arg: (x := int)): ... # ok
/// def foo[T](arg: (x := int)): ... # syntax error
/// ```
allow_named_expr: bool,
/// Context used for emitting errors.
ctx: &'a Ctx,
@ -1108,7 +1094,7 @@ where
{
fn visit_expr(&mut self, expr: &Expr) {
match expr {
Expr::Named(ast::ExprNamed { range, .. }) if !self.allow_named_expr => {
Expr::Named(ast::ExprNamed { range, .. }) => {
SemanticSyntaxChecker::add_error(
self.ctx,
SemanticSyntaxErrorKind::InvalidExpression(
@ -1166,6 +1152,9 @@ pub trait SemanticSyntaxContext {
/// Returns `true` if a module's docstring boundary has been passed.
fn seen_docstring_boundary(&self) -> bool;
/// Returns `true` if `__future__`-style type annotations are enabled.
fn future_annotations_or_stub(&self) -> bool;
/// The target Python version for detecting backwards-incompatible syntax changes.
fn python_version(&self) -> PythonVersion;

View file

@ -490,6 +490,10 @@ impl SemanticSyntaxContext for TestContext<'_> {
false
}
fn future_annotations_or_stub(&self) -> bool {
false
}
fn python_version(&self) -> PythonVersion {
self.python_version
}

View file

@ -179,13 +179,3 @@ Module(
3 | def foo() -> yield x: ...
| ^^^^^^^ Syntax Error: Yield expression cannot be used here
|
## Semantic Syntax Errors
|
1 | def foo() -> *int: ...
2 | def foo() -> (*int): ...
3 | def foo() -> yield x: ...
| ^^^^^^^ Syntax Error: yield 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_cl
```
Module(
ModModule {
range: 0..246,
range: 0..193,
body: [
ClassDef(
StmtClassDef {
@ -78,112 +78,22 @@ Module(
),
ClassDef(
StmtClassDef {
range: 27..50,
decorator_list: [],
name: Identifier {
id: Name("G"),
range: 33..34,
},
type_params: None,
arguments: Some(
Arguments {
range: 34..45,
args: [
Yield(
ExprYield {
range: 36..43,
value: Some(
NumberLiteral(
ExprNumberLiteral {
range: 42..43,
value: Int(
1,
),
},
),
),
},
),
],
keywords: [],
},
),
body: [
Expr(
StmtExpr {
range: 47..50,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 47..50,
},
),
},
),
],
},
),
ClassDef(
StmtClassDef {
range: 51..79,
decorator_list: [],
name: Identifier {
id: Name("H"),
range: 57..58,
},
type_params: None,
arguments: Some(
Arguments {
range: 58..74,
args: [
YieldFrom(
ExprYieldFrom {
range: 60..72,
value: NumberLiteral(
ExprNumberLiteral {
range: 71..72,
value: Int(
1,
),
},
),
},
),
],
keywords: [],
},
),
body: [
Expr(
StmtExpr {
range: 76..79,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 76..79,
},
),
},
),
],
},
),
ClassDef(
StmtClassDef {
range: 80..106,
range: 27..53,
decorator_list: [],
name: Identifier {
id: Name("I"),
range: 86..87,
range: 33..34,
},
type_params: Some(
TypeParams {
range: 87..90,
range: 34..37,
type_params: [
TypeVar(
TypeParamTypeVar {
range: 88..89,
range: 35..36,
name: Identifier {
id: Name("T"),
range: 88..89,
range: 35..36,
},
bound: None,
default: None,
@ -194,15 +104,15 @@ Module(
),
arguments: Some(
Arguments {
range: 90..101,
range: 37..48,
args: [
Yield(
ExprYield {
range: 92..99,
range: 39..46,
value: Some(
NumberLiteral(
ExprNumberLiteral {
range: 98..99,
range: 45..46,
value: Int(
1,
),
@ -218,10 +128,10 @@ Module(
body: [
Expr(
StmtExpr {
range: 103..106,
range: 50..53,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 103..106,
range: 50..53,
},
),
},
@ -231,22 +141,22 @@ Module(
),
ClassDef(
StmtClassDef {
range: 107..138,
range: 54..85,
decorator_list: [],
name: Identifier {
id: Name("J"),
range: 113..114,
range: 60..61,
},
type_params: Some(
TypeParams {
range: 114..117,
range: 61..64,
type_params: [
TypeVar(
TypeParamTypeVar {
range: 115..116,
range: 62..63,
name: Identifier {
id: Name("T"),
range: 115..116,
range: 62..63,
},
bound: None,
default: None,
@ -257,14 +167,14 @@ Module(
),
arguments: Some(
Arguments {
range: 117..133,
range: 64..80,
args: [
YieldFrom(
ExprYieldFrom {
range: 119..131,
range: 66..78,
value: NumberLiteral(
ExprNumberLiteral {
range: 130..131,
range: 77..78,
value: Int(
1,
),
@ -279,10 +189,10 @@ Module(
body: [
Expr(
StmtExpr {
range: 135..138,
range: 82..85,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 135..138,
range: 82..85,
},
),
},
@ -292,31 +202,31 @@ Module(
),
ClassDef(
StmtClassDef {
range: 139..165,
range: 86..112,
decorator_list: [],
name: Identifier {
id: Name("K"),
range: 145..146,
range: 92..93,
},
type_params: Some(
TypeParams {
range: 146..160,
range: 93..107,
type_params: [
TypeVar(
TypeParamTypeVar {
range: 147..159,
range: 94..106,
name: Identifier {
id: Name("T"),
range: 147..148,
range: 94..95,
},
bound: Some(
Yield(
ExprYield {
range: 151..158,
range: 98..105,
value: Some(
NumberLiteral(
ExprNumberLiteral {
range: 157..158,
range: 104..105,
value: Int(
1,
),
@ -336,10 +246,10 @@ Module(
body: [
Expr(
StmtExpr {
range: 162..165,
range: 109..112,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 162..165,
range: 109..112,
},
),
},
@ -349,37 +259,37 @@ Module(
),
ClassDef(
StmtClassDef {
range: 190..215,
range: 137..162,
decorator_list: [],
name: Identifier {
id: Name("L"),
range: 196..197,
range: 143..144,
},
type_params: Some(
TypeParams {
range: 197..210,
range: 144..157,
type_params: [
TypeVar(
TypeParamTypeVar {
range: 198..209,
range: 145..156,
name: Identifier {
id: Name("T"),
range: 198..199,
range: 145..146,
},
bound: Some(
Named(
ExprNamed {
range: 202..208,
range: 149..155,
target: Name(
ExprName {
range: 202..203,
range: 149..150,
id: Name("x"),
ctx: Store,
},
),
value: NumberLiteral(
ExprNumberLiteral {
range: 207..208,
range: 154..155,
value: Int(
1,
),
@ -398,10 +308,10 @@ Module(
body: [
Expr(
StmtExpr {
range: 212..215,
range: 159..162,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 212..215,
range: 159..162,
},
),
},
@ -418,62 +328,42 @@ Module(
|
1 | class F[T](y := list): ...
| ^^^^^^^^^ Syntax Error: named expression cannot be used within a generic definition
2 | class G((yield 1)): ...
3 | class H((yield from 1)): ...
2 | class I[T]((yield 1)): ...
3 | class J[T]((yield from 1)): ...
|
|
1 | class F[T](y := list): ...
2 | class G((yield 1)): ...
| ^^^^^^^ Syntax Error: yield expression cannot be used as a base class
3 | class H((yield from 1)): ...
4 | class I[T]((yield 1)): ...
|
|
1 | class F[T](y := list): ...
2 | class G((yield 1)): ...
3 | class H((yield from 1)): ...
| ^^^^^^^^^^^^ Syntax Error: yield expression cannot be used as a base class
4 | class I[T]((yield 1)): ...
5 | class J[T]((yield from 1)): ...
|
|
2 | class G((yield 1)): ...
3 | class H((yield from 1)): ...
4 | class I[T]((yield 1)): ...
2 | class I[T]((yield 1)): ...
| ^^^^^^^ Syntax Error: yield expression cannot be used within a generic definition
5 | class J[T]((yield from 1)): ...
6 | class K[T: (yield 1)]: ... # yield in TypeVar
3 | class J[T]((yield from 1)): ...
4 | class K[T: (yield 1)]: ... # yield in TypeVar
|
|
3 | class H((yield from 1)): ...
4 | class I[T]((yield 1)): ...
5 | class J[T]((yield from 1)): ...
1 | class F[T](y := list): ...
2 | class I[T]((yield 1)): ...
3 | class J[T]((yield from 1)): ...
| ^^^^^^^^^^^^ Syntax Error: yield expression cannot be used within a generic definition
6 | class K[T: (yield 1)]: ... # yield in TypeVar
7 | class L[T: (x := 1)]: ... # named expr in TypeVar
4 | class K[T: (yield 1)]: ... # yield in TypeVar
5 | class L[T: (x := 1)]: ... # named expr in TypeVar
|
|
4 | class I[T]((yield 1)): ...
5 | class J[T]((yield from 1)): ...
6 | class K[T: (yield 1)]: ... # yield in TypeVar
2 | class I[T]((yield 1)): ...
3 | class J[T]((yield from 1)): ...
4 | class K[T: (yield 1)]: ... # yield in TypeVar
| ^^^^^^^ Syntax Error: yield expression cannot be used within a TypeVar bound
7 | class L[T: (x := 1)]: ... # named expr in TypeVar
5 | class L[T: (x := 1)]: ... # named expr in TypeVar
|
|
5 | class J[T]((yield from 1)): ...
6 | class K[T: (yield 1)]: ... # yield in TypeVar
7 | class L[T: (x := 1)]: ... # named expr in TypeVar
3 | class J[T]((yield from 1)): ...
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
|

View file

@ -0,0 +1,433 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/invalid_annotation_function_py314.py
---
## AST
```
Module(
ModModule {
range: 0..236,
body: [
FunctionDef(
StmtFunctionDef {
range: 44..68,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("f"),
range: 48..49,
},
type_params: None,
parameters: Parameters {
range: 49..51,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: Some(
Named(
ExprNamed {
range: 56..62,
target: Name(
ExprName {
range: 56..57,
id: Name("y"),
ctx: Store,
},
),
value: NumberLiteral(
ExprNumberLiteral {
range: 61..62,
value: Int(
3,
),
},
),
},
),
),
body: [
Expr(
StmtExpr {
range: 65..68,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 65..68,
},
),
},
),
],
},
),
FunctionDef(
StmtFunctionDef {
range: 69..94,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("g"),
range: 73..74,
},
type_params: None,
parameters: Parameters {
range: 74..89,
posonlyargs: [],
args: [
ParameterWithDefault {
range: 75..88,
parameter: Parameter {
range: 75..88,
name: Identifier {
id: Name("arg"),
range: 75..78,
},
annotation: Some(
Named(
ExprNamed {
range: 81..87,
target: Name(
ExprName {
range: 81..82,
id: Name("x"),
ctx: Store,
},
),
value: NumberLiteral(
ExprNumberLiteral {
range: 86..87,
value: Int(
1,
),
},
),
},
),
),
},
default: None,
},
],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
Expr(
StmtExpr {
range: 91..94,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 91..94,
},
),
},
),
],
},
),
FunctionDef(
StmtFunctionDef {
range: 95..235,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("outer"),
range: 99..104,
},
type_params: None,
parameters: Parameters {
range: 104..106,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
FunctionDef(
StmtFunctionDef {
range: 112..136,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("i"),
range: 116..117,
},
type_params: None,
parameters: Parameters {
range: 117..131,
posonlyargs: [],
args: [
ParameterWithDefault {
range: 118..130,
parameter: Parameter {
range: 118..130,
name: Identifier {
id: Name("x"),
range: 118..119,
},
annotation: Some(
Yield(
ExprYield {
range: 122..129,
value: Some(
NumberLiteral(
ExprNumberLiteral {
range: 128..129,
value: Int(
1,
),
},
),
),
},
),
),
},
default: None,
},
],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
Expr(
StmtExpr {
range: 133..136,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 133..136,
},
),
},
),
],
},
),
FunctionDef(
StmtFunctionDef {
range: 141..166,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("k"),
range: 145..146,
},
type_params: None,
parameters: Parameters {
range: 146..148,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: Some(
Yield(
ExprYield {
range: 153..160,
value: Some(
NumberLiteral(
ExprNumberLiteral {
range: 159..160,
value: Int(
1,
),
},
),
),
},
),
),
body: [
Expr(
StmtExpr {
range: 163..166,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 163..166,
},
),
},
),
],
},
),
FunctionDef(
StmtFunctionDef {
range: 171..200,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("m"),
range: 175..176,
},
type_params: None,
parameters: Parameters {
range: 176..195,
posonlyargs: [],
args: [
ParameterWithDefault {
range: 177..194,
parameter: Parameter {
range: 177..194,
name: Identifier {
id: Name("x"),
range: 177..178,
},
annotation: Some(
YieldFrom(
ExprYieldFrom {
range: 181..193,
value: NumberLiteral(
ExprNumberLiteral {
range: 192..193,
value: Int(
1,
),
},
),
},
),
),
},
default: None,
},
],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
Expr(
StmtExpr {
range: 197..200,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 197..200,
},
),
},
),
],
},
),
FunctionDef(
StmtFunctionDef {
range: 205..235,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("o"),
range: 209..210,
},
type_params: None,
parameters: Parameters {
range: 210..212,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: Some(
YieldFrom(
ExprYieldFrom {
range: 217..229,
value: NumberLiteral(
ExprNumberLiteral {
range: 228..229,
value: Int(
1,
),
},
),
},
),
),
body: [
Expr(
StmtExpr {
range: 232..235,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 232..235,
},
),
},
),
],
},
),
],
},
),
],
},
)
```
## Semantic Syntax Errors
|
1 | # parse_options: {"target-version": "3.14"}
2 | def f() -> (y := 3): ...
| ^^^^^^ Syntax Error: named expression cannot be used within a type annotation
3 | def g(arg: (x := 1)): ...
4 | def outer():
|
|
1 | # parse_options: {"target-version": "3.14"}
2 | def f() -> (y := 3): ...
3 | def g(arg: (x := 1)): ...
| ^^^^^^ Syntax Error: named expression cannot be used within a type annotation
4 | def outer():
5 | def i(x: (yield 1)): ...
|
|
3 | def g(arg: (x := 1)): ...
4 | def outer():
5 | def i(x: (yield 1)): ...
| ^^^^^^^ Syntax Error: yield expression cannot be used within a type annotation
6 | def k() -> (yield 1): ...
7 | def m(x: (yield from 1)): ...
|
|
4 | def outer():
5 | def i(x: (yield 1)): ...
6 | def k() -> (yield 1): ...
| ^^^^^^^ Syntax Error: yield expression cannot be used within a type annotation
7 | def m(x: (yield from 1)): ...
8 | def o() -> (yield from 1): ...
|
|
5 | def i(x: (yield 1)): ...
6 | def k() -> (yield 1): ...
7 | def m(x: (yield from 1)): ...
| ^^^^^^^^^^^^ Syntax Error: yield expression cannot be used within a type annotation
8 | def o() -> (yield from 1): ...
|
|
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
|

View file

@ -224,13 +224,3 @@ Module(
3 | def foo(arg: x := int): ...
| ^^ Syntax Error: Expected ',', found ':='
|
## Semantic Syntax Errors
|
1 | def foo(arg: *int): ...
2 | def foo(arg: yield int): ...
| ^^^^^^^^^ Syntax Error: yield expression cannot be used within a type annotation
3 | def foo(arg: x := int): ...
|

View file

@ -308,14 +308,3 @@ Module(
| ^^^^^^^ Syntax Error: Yield expression cannot be used here
5 | # def foo(*args: **int): ...
|
## Semantic Syntax Errors
|
2 | def foo(*args: (*tuple[int])): ...
3 | def foo(*args: *int or str): ...
4 | def foo(*args: *yield x): ...
| ^^^^^^^ Syntax Error: yield expression cannot be used within a type annotation
5 | # def foo(*args: **int): ...
|

View file

@ -7,7 +7,7 @@ input_file: crates/ruff_python_parser/resources/inline/ok/valid_annotation_class
```
Module(
ModModule {
range: 0..24,
range: 0..94,
body: [
ClassDef(
StmtClassDef {
@ -59,6 +59,119 @@ Module(
],
},
),
FunctionDef(
StmtFunctionDef {
range: 24..93,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("f"),
range: 28..29,
},
type_params: None,
parameters: Parameters {
range: 29..31,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
ClassDef(
StmtClassDef {
range: 37..60,
decorator_list: [],
name: Identifier {
id: Name("G"),
range: 43..44,
},
type_params: None,
arguments: Some(
Arguments {
range: 44..55,
args: [
Yield(
ExprYield {
range: 46..53,
value: Some(
NumberLiteral(
ExprNumberLiteral {
range: 52..53,
value: Int(
1,
),
},
),
),
},
),
],
keywords: [],
},
),
body: [
Expr(
StmtExpr {
range: 57..60,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 57..60,
},
),
},
),
],
},
),
ClassDef(
StmtClassDef {
range: 65..93,
decorator_list: [],
name: Identifier {
id: Name("H"),
range: 71..72,
},
type_params: None,
arguments: Some(
Arguments {
range: 72..88,
args: [
YieldFrom(
ExprYieldFrom {
range: 74..86,
value: NumberLiteral(
ExprNumberLiteral {
range: 85..86,
value: Int(
1,
),
},
),
},
),
],
keywords: [],
},
),
body: [
Expr(
StmtExpr {
range: 90..93,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 90..93,
},
),
},
),
],
},
),
],
},
),
],
},
)

View file

@ -7,7 +7,7 @@ input_file: crates/ruff_python_parser/resources/inline/ok/valid_annotation_funct
```
Module(
ModModule {
range: 0..51,
range: 0..192,
body: [
FunctionDef(
StmtFunctionDef {
@ -130,6 +130,247 @@ Module(
],
},
),
FunctionDef(
StmtFunctionDef {
range: 51..191,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("outer"),
range: 55..60,
},
type_params: None,
parameters: Parameters {
range: 60..62,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
FunctionDef(
StmtFunctionDef {
range: 68..92,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("i"),
range: 72..73,
},
type_params: None,
parameters: Parameters {
range: 73..87,
posonlyargs: [],
args: [
ParameterWithDefault {
range: 74..86,
parameter: Parameter {
range: 74..86,
name: Identifier {
id: Name("x"),
range: 74..75,
},
annotation: Some(
Yield(
ExprYield {
range: 78..85,
value: Some(
NumberLiteral(
ExprNumberLiteral {
range: 84..85,
value: Int(
1,
),
},
),
),
},
),
),
},
default: None,
},
],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
Expr(
StmtExpr {
range: 89..92,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 89..92,
},
),
},
),
],
},
),
FunctionDef(
StmtFunctionDef {
range: 97..122,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("k"),
range: 101..102,
},
type_params: None,
parameters: Parameters {
range: 102..104,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: Some(
Yield(
ExprYield {
range: 109..116,
value: Some(
NumberLiteral(
ExprNumberLiteral {
range: 115..116,
value: Int(
1,
),
},
),
),
},
),
),
body: [
Expr(
StmtExpr {
range: 119..122,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 119..122,
},
),
},
),
],
},
),
FunctionDef(
StmtFunctionDef {
range: 127..156,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("m"),
range: 131..132,
},
type_params: None,
parameters: Parameters {
range: 132..151,
posonlyargs: [],
args: [
ParameterWithDefault {
range: 133..150,
parameter: Parameter {
range: 133..150,
name: Identifier {
id: Name("x"),
range: 133..134,
},
annotation: Some(
YieldFrom(
ExprYieldFrom {
range: 137..149,
value: NumberLiteral(
ExprNumberLiteral {
range: 148..149,
value: Int(
1,
),
},
),
},
),
),
},
default: None,
},
],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
Expr(
StmtExpr {
range: 153..156,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 153..156,
},
),
},
),
],
},
),
FunctionDef(
StmtFunctionDef {
range: 161..191,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("o"),
range: 165..166,
},
type_params: None,
parameters: Parameters {
range: 166..168,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: Some(
YieldFrom(
ExprYieldFrom {
range: 173..185,
value: NumberLiteral(
ExprNumberLiteral {
range: 184..185,
value: Int(
1,
),
},
),
},
),
),
body: [
Expr(
StmtExpr {
range: 188..191,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 188..191,
},
),
},
),
],
},
),
],
},
),
],
},
)