-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[syntax-error]: default type parameter followed by non-default type parameter #21657
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
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: 11happy <[email protected]>
|
| Stmt::ClassDef(ast::StmtClassDef { | ||
| type_params: Some(type_params), | ||
| .. | ||
| }) => { | ||
| Self::duplicate_type_parameter_name(type_params, ctx); | ||
| Self::type_parameter_default_order(type_params, ctx); | ||
| } | ||
| Stmt::TypeAlias(ast::StmtTypeAlias { | ||
| type_params: Some(type_params), | ||
| .. | ||
| }) => { | ||
| Self::duplicate_type_parameter_name(type_params, ctx); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check needs to be applied to type aliases as well:
In [1]: type Int[T = int, U] = int
Cell In[1], line 1
type Int[T = int, U] = int
^
SyntaxError: non-default type parameter 'U' follows default type parameterThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
however some additional detection's are there for example -
type X[*Ts = x := int] = int
in type params:
type_params: TypeParams { range: 6..22, node_index: NodeIndex(None), type_params: [TypeVarTuple(TypeParamTypeVarTuple { node_index: NodeIndex(None), range: 7..14, name: Identifier { id: Name("Ts"), range: 8..10, node_index: NodeIndex(None) }, default: Some(Name(ExprName { node_index: NodeIndex(None), range: 13..14, id: Name("x"), ctx: Load })) }), TypeVar(TypeParamTypeVar { node_index: NodeIndex(None), range: 18..21, name: Identifier { id: Name("int"), range: 18..21, node_index: NodeIndex(None) }, bound: None, default: None })]
int is getting detected as seperate non default typevar, whereas I think should it be parsed as single expression ?
| // test_err type_parameter_default_order | ||
| // class C[T = int, U]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add some cases where the default is not at the start of the list and it has multiple non-default type parameter following a type parameter with default like:
class C[T1, T2 = int, T3, T4]: ...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Signed-off-by: 11happy <[email protected]>
Summary
This PR implements syntax error where a default type parameter is followed by a non-default type parameter. #17412 (comment)
Test Plan
I have written inline tests as directed in #17412