Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[syntax-errors] type statements before Python 3.12 #16478

Merged
merged 7 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all 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,2 @@
# parse_options: {"target-version": "3.11"}
type x = int
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# parse_options: {"target-version": "3.12"}
type x = int
3 changes: 3 additions & 0 deletions crates/ruff_python_parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ pub enum UnsupportedSyntaxErrorKind {
Match,
Walrus,
ExceptStar,
TypeAliasStatement,
TypeParamDefault,
}

Expand All @@ -458,6 +459,7 @@ impl Display for UnsupportedSyntaxError {
UnsupportedSyntaxErrorKind::Match => "Cannot use `match` statement",
UnsupportedSyntaxErrorKind::Walrus => "Cannot use named assignment expression (`:=`)",
UnsupportedSyntaxErrorKind::ExceptStar => "Cannot use `except*`",
UnsupportedSyntaxErrorKind::TypeAliasStatement => "Cannot use `type` alias statement",
UnsupportedSyntaxErrorKind::TypeParamDefault => {
"Cannot set default type for a type parameter"
}
Expand All @@ -478,6 +480,7 @@ impl UnsupportedSyntaxErrorKind {
UnsupportedSyntaxErrorKind::Match => PythonVersion::PY310,
UnsupportedSyntaxErrorKind::Walrus => PythonVersion::PY38,
UnsupportedSyntaxErrorKind::ExceptStar => PythonVersion::PY311,
UnsupportedSyntaxErrorKind::TypeAliasStatement => PythonVersion::PY312,
UnsupportedSyntaxErrorKind::TypeParamDefault => PythonVersion::PY313,
}
}
Expand Down
14 changes: 14 additions & 0 deletions crates/ruff_python_parser/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,22 @@ impl<'src> Parser<'src> {
/// See: <https://docs.python.org/3/reference/simple_stmts.html#the-type-statement>
fn parse_type_alias_statement(&mut self) -> ast::StmtTypeAlias {
let start = self.node_start();
let type_range = self.current_token_range();
self.bump(TokenKind::Type);

// test_ok type_stmt_py312
// # parse_options: {"target-version": "3.12"}
// type x = int

// test_err type_stmt_py311
// # parse_options: {"target-version": "3.11"}
// type x = int

self.add_unsupported_syntax_error(
UnsupportedSyntaxErrorKind::TypeAliasStatement,
type_range,
);

let mut name = Expr::Name(self.parse_name());
helpers::set_expr_ctx(&mut name, ExprContext::Store);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/type_stmt_py311.py
---
## AST

```
Module(
ModModule {
range: 0..57,
body: [
TypeAlias(
StmtTypeAlias {
range: 44..56,
name: Name(
ExprName {
range: 49..50,
id: Name("x"),
ctx: Store,
},
),
type_params: None,
value: Name(
ExprName {
range: 53..56,
id: Name("int"),
ctx: Load,
},
),
},
),
],
},
)
```
## Unsupported Syntax Errors

|
1 | # parse_options: {"target-version": "3.11"}
2 | type x = int
| ^^^^ Syntax Error: Cannot use `type` alias statement on Python 3.11 (syntax was added in Python 3.12)
|
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/ok/type_stmt_py312.py
---
## AST

```
Module(
ModModule {
range: 0..57,
body: [
TypeAlias(
StmtTypeAlias {
range: 44..56,
name: Name(
ExprName {
range: 49..50,
id: Name("x"),
ctx: Store,
},
),
type_params: None,
value: Name(
ExprName {
range: 53..56,
id: Name("int"),
ctx: Load,
},
),
},
),
],
},
)
```
Loading