-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add syntax error for empty type parameter list (#12030)
## Summary (I'm pretty sure I added this in the parser re-write but must've got lost in the rebase?) This PR raises a syntax error if the type parameter list is empty. As per the grammar, there should be at least one type parameter: ``` type_params: | invalid_type_params | '[' type_param_seq ']' type_param_seq: ','.type_param+ [','] ``` Verified via the builtin `ast` module as well: ```console $ python3.13 -m ast parser/_.py Traceback (most recent call last): [..] File "parser/_.py", line 1 def foo[](): ^ SyntaxError: Type parameter list cannot be empty ``` ## Test Plan Add inline test cases and update the snapshots.
- Loading branch information
1 parent
83fe447
commit 7cb2619
Showing
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
crates/ruff_python_parser/resources/inline/err/type_params_empty.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
def foo[](): | ||
pass | ||
type ListOrSet[] = list | set |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_params_empty.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
--- | ||
source: crates/ruff_python_parser/tests/fixtures.rs | ||
input_file: crates/ruff_python_parser/resources/inline/err/type_params_empty.py | ||
--- | ||
## AST | ||
|
||
``` | ||
Module( | ||
ModModule { | ||
range: 0..52, | ||
body: [ | ||
FunctionDef( | ||
StmtFunctionDef { | ||
range: 0..21, | ||
is_async: false, | ||
decorator_list: [], | ||
name: Identifier { | ||
id: "foo", | ||
range: 4..7, | ||
}, | ||
type_params: Some( | ||
TypeParams { | ||
range: 7..9, | ||
type_params: [], | ||
}, | ||
), | ||
parameters: Parameters { | ||
range: 9..11, | ||
posonlyargs: [], | ||
args: [], | ||
vararg: None, | ||
kwonlyargs: [], | ||
kwarg: None, | ||
}, | ||
returns: None, | ||
body: [ | ||
Pass( | ||
StmtPass { | ||
range: 17..21, | ||
}, | ||
), | ||
], | ||
}, | ||
), | ||
TypeAlias( | ||
StmtTypeAlias { | ||
range: 22..51, | ||
name: Name( | ||
ExprName { | ||
range: 27..36, | ||
id: "ListOrSet", | ||
ctx: Store, | ||
}, | ||
), | ||
type_params: Some( | ||
TypeParams { | ||
range: 36..38, | ||
type_params: [], | ||
}, | ||
), | ||
value: BinOp( | ||
ExprBinOp { | ||
range: 41..51, | ||
left: Name( | ||
ExprName { | ||
range: 41..45, | ||
id: "list", | ||
ctx: Load, | ||
}, | ||
), | ||
op: BitOr, | ||
right: Name( | ||
ExprName { | ||
range: 48..51, | ||
id: "set", | ||
ctx: Load, | ||
}, | ||
), | ||
}, | ||
), | ||
}, | ||
), | ||
], | ||
}, | ||
) | ||
``` | ||
## Errors | ||
|
||
| | ||
1 | def foo[](): | ||
| ^ Syntax Error: Type parameter list cannot be empty | ||
2 | pass | ||
3 | type ListOrSet[] = list | set | ||
| | ||
|
||
|
||
| | ||
1 | def foo[](): | ||
2 | pass | ||
3 | type ListOrSet[] = list | set | ||
| ^ Syntax Error: Type parameter list cannot be empty | ||
| |