mirror of
https://github.com/astral-sh/ruff
synced 2026-01-09 15:44:22 -05:00
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.
This commit is contained in:
@@ -99,6 +99,8 @@ pub enum ParseErrorType {
|
||||
EmptyDeleteTargets,
|
||||
/// An empty import names list was found during parsing.
|
||||
EmptyImportNames,
|
||||
/// An empty type parameter list was found during parsing.
|
||||
EmptyTypeParams,
|
||||
|
||||
/// An unparenthesized named expression was found where it is not allowed.
|
||||
UnparenthesizedNamedExpression,
|
||||
@@ -242,6 +244,7 @@ impl std::fmt::Display for ParseErrorType {
|
||||
ParseErrorType::EmptyImportNames => {
|
||||
f.write_str("Expected one or more symbol names after import")
|
||||
}
|
||||
ParseErrorType::EmptyTypeParams => f.write_str("Type parameter list cannot be empty"),
|
||||
ParseErrorType::ParamAfterVarKeywordParam => {
|
||||
f.write_str("Parameter cannot follow var-keyword parameter")
|
||||
}
|
||||
|
||||
@@ -3027,6 +3027,14 @@ impl<'src> Parser<'src> {
|
||||
Parser::parse_type_param,
|
||||
);
|
||||
|
||||
if type_params.is_empty() {
|
||||
// test_err type_params_empty
|
||||
// def foo[]():
|
||||
// pass
|
||||
// type ListOrSet[] = list | set
|
||||
self.add_error(ParseErrorType::EmptyTypeParams, self.current_token_range());
|
||||
}
|
||||
|
||||
self.expect(TokenKind::Rsqb);
|
||||
|
||||
ast::TypeParams {
|
||||
|
||||
Reference in New Issue
Block a user