Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# parse_options: {"target-version": "3.12"}
type X[T = int] = int
def f[T = int](): ...
class C[T = int](): ...
class D[S, T = int, U = uint](): ...
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# parse_options: {"target-version": "3.13"}
type X[T = int] = int
def f[T = int](): ...
class C[T = int](): ...
13 changes: 9 additions & 4 deletions crates/ruff_python_parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,18 +449,22 @@ pub enum UnsupportedSyntaxErrorKind {
Match,
Walrus,
ExceptStar,
TypeParamDefault,
}

impl Display for UnsupportedSyntaxError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let kind = match self.kind {
UnsupportedSyntaxErrorKind::Match => "`match` statement",
UnsupportedSyntaxErrorKind::Walrus => "named assignment expression (`:=`)",
UnsupportedSyntaxErrorKind::ExceptStar => "`except*`",
UnsupportedSyntaxErrorKind::Match => "Cannot use `match` statement",
UnsupportedSyntaxErrorKind::Walrus => "Cannot use named assignment expression (`:=`)",
UnsupportedSyntaxErrorKind::ExceptStar => "Cannot use `except*`",
UnsupportedSyntaxErrorKind::TypeParamDefault => {
"Cannot set default type for a type parameter"
}
};
write!(
f,
"Cannot use {kind} on Python {} (syntax was added in Python {})",
"{kind} on Python {} (syntax was added in Python {})",
self.target_version,
self.kind.minimum_version(),
)
Expand All @@ -474,6 +478,7 @@ impl UnsupportedSyntaxErrorKind {
UnsupportedSyntaxErrorKind::Match => PythonVersion::PY310,
UnsupportedSyntaxErrorKind::Walrus => PythonVersion::PY38,
UnsupportedSyntaxErrorKind::ExceptStar => PythonVersion::PY311,
UnsupportedSyntaxErrorKind::TypeParamDefault => PythonVersion::PY313,
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions crates/ruff_python_parser/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3242,6 +3242,7 @@ impl<'src> Parser<'src> {
None
};

let default_start = self.node_start();
let default = if self.eat(TokenKind::Equal) {
if self.at_expr() {
// test_err type_param_type_var_invalid_default_expr
Expand All @@ -3267,6 +3268,26 @@ impl<'src> Parser<'src> {
None
};

// test_ok type_param_default_py313
// # parse_options: {"target-version": "3.13"}
// type X[T = int] = int
// def f[T = int](): ...
// class C[T = int](): ...

// test_err type_param_default_py312
// # parse_options: {"target-version": "3.12"}
// type X[T = int] = int
// def f[T = int](): ...
// class C[T = int](): ...
// class D[S, T = int, U = uint](): ...

if default.is_some() {
self.add_unsupported_syntax_error(
UnsupportedSyntaxErrorKind::TypeParamDefault,
self.node_range(default_start),
);
}

ast::TypeParam::TypeVar(ast::TypeParamTypeVar {
range: self.node_range(start),
name,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/type_param_default_py312.py
---
## AST

```
Module(
ModModule {
range: 0..149,
body: [
TypeAlias(
StmtTypeAlias {
range: 44..65,
name: Name(
ExprName {
range: 49..50,
id: Name("X"),
ctx: Store,
},
),
type_params: Some(
TypeParams {
range: 50..59,
type_params: [
TypeVar(
TypeParamTypeVar {
range: 51..58,
name: Identifier {
id: Name("T"),
range: 51..52,
},
bound: None,
default: Some(
Name(
ExprName {
range: 55..58,
id: Name("int"),
ctx: Load,
},
),
),
},
),
],
},
),
value: Name(
ExprName {
range: 62..65,
id: Name("int"),
ctx: Load,
},
),
},
),
FunctionDef(
StmtFunctionDef {
range: 66..87,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("f"),
range: 70..71,
},
type_params: Some(
TypeParams {
range: 71..80,
type_params: [
TypeVar(
TypeParamTypeVar {
range: 72..79,
name: Identifier {
id: Name("T"),
range: 72..73,
},
bound: None,
default: Some(
Name(
ExprName {
range: 76..79,
id: Name("int"),
ctx: Load,
},
),
),
},
),
],
},
),
parameters: Parameters {
range: 80..82,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
Expr(
StmtExpr {
range: 84..87,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 84..87,
},
),
},
),
],
},
),
ClassDef(
StmtClassDef {
range: 88..111,
decorator_list: [],
name: Identifier {
id: Name("C"),
range: 94..95,
},
type_params: Some(
TypeParams {
range: 95..104,
type_params: [
TypeVar(
TypeParamTypeVar {
range: 96..103,
name: Identifier {
id: Name("T"),
range: 96..97,
},
bound: None,
default: Some(
Name(
ExprName {
range: 100..103,
id: Name("int"),
ctx: Load,
},
),
),
},
),
],
},
),
arguments: Some(
Arguments {
range: 104..106,
args: [],
keywords: [],
},
),
body: [
Expr(
StmtExpr {
range: 108..111,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 108..111,
},
),
},
),
],
},
),
ClassDef(
StmtClassDef {
range: 112..148,
decorator_list: [],
name: Identifier {
id: Name("D"),
range: 118..119,
},
type_params: Some(
TypeParams {
range: 119..141,
type_params: [
TypeVar(
TypeParamTypeVar {
range: 120..121,
name: Identifier {
id: Name("S"),
range: 120..121,
},
bound: None,
default: None,
},
),
TypeVar(
TypeParamTypeVar {
range: 123..130,
name: Identifier {
id: Name("T"),
range: 123..124,
},
bound: None,
default: Some(
Name(
ExprName {
range: 127..130,
id: Name("int"),
ctx: Load,
},
),
),
},
),
TypeVar(
TypeParamTypeVar {
range: 132..140,
name: Identifier {
id: Name("U"),
range: 132..133,
},
bound: None,
default: Some(
Name(
ExprName {
range: 136..140,
id: Name("uint"),
ctx: Load,
},
),
),
},
),
],
},
),
arguments: Some(
Arguments {
range: 141..143,
args: [],
keywords: [],
},
),
body: [
Expr(
StmtExpr {
range: 145..148,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 145..148,
},
),
},
),
],
},
),
],
},
)
```
## Unsupported Syntax Errors

|
1 | # parse_options: {"target-version": "3.12"}
2 | type X[T = int] = int
| ^^^^^ Syntax Error: Cannot set default type for a type parameter on Python 3.12 (syntax was added in Python 3.13)
3 | def f[T = int](): ...
4 | class C[T = int](): ...
|


|
1 | # parse_options: {"target-version": "3.12"}
2 | type X[T = int] = int
3 | def f[T = int](): ...
| ^^^^^ Syntax Error: Cannot set default type for a type parameter on Python 3.12 (syntax was added in Python 3.13)
4 | class C[T = int](): ...
5 | class D[S, T = int, U = uint](): ...
|


|
2 | type X[T = int] = int
3 | def f[T = int](): ...
4 | class C[T = int](): ...
| ^^^^^ Syntax Error: Cannot set default type for a type parameter on Python 3.12 (syntax was added in Python 3.13)
5 | class D[S, T = int, U = uint](): ...
|


|
3 | def f[T = int](): ...
4 | class C[T = int](): ...
5 | class D[S, T = int, U = uint](): ...
| ^^^^^ Syntax Error: Cannot set default type for a type parameter on Python 3.12 (syntax was added in Python 3.13)
|


|
3 | def f[T = int](): ...
4 | class C[T = int](): ...
5 | class D[S, T = int, U = uint](): ...
| ^^^^^^ Syntax Error: Cannot set default type for a type parameter on Python 3.12 (syntax was added in Python 3.13)
|
Loading
Loading