-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[red-knot] Emit errors for more AST nodes that are invalid (or only valid in specific contexts) in type expressions #16822
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
[red-knot] Emit errors for more AST nodes that are invalid (or only valid in specific contexts) in type expressions #16822
Conversation
… and added errors for invalid BoolOp and Named type expressions
@@ -6033,16 +6033,19 @@ impl<'db> TypeInferenceBuilder<'db> { | |||
annotation_ty | |||
} | |||
|
|||
fn report_invalid_type_expression( |
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.
@AlexWaygood What do you think of making this a instance method?
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.
I was having issues with mutable references in ast::Expr::BoolOp as infer_boolean_expression requires a mutable reference to self, and i believe the closure does too
|
I havent covered slice type expression yet |
I believe tests failing is mainly due to Callable type inference calls this function with tuple expression instead of the individual arguments, ill look into this more |
The errors mainly stem from "Tuples are not allowed in type expressions" In generic arguments with > 1 generic parameter I'm not really sure the best way to handle this. Perhaps we can create a new function that can call infer_type_expression_no_store if no tuples are expected in the Expr and else it can try to "flatten" the type and / or call infer_type_expression_no_store on each type in the Tuple @AlexWaygood What are your thoughts? |
Yeah, I haven't looked at the test failures but I think those are correct because we fallback to calling ruff/crates/red_knot_python_semantic/src/types/infer.rs Lines 6584 to 6590 in f9ee155
This is a requirement because currently the tests require that every expression has an inferred type even in the case of failure: ruff/crates/red_knot_project/tests/check.rs Lines 165 to 171 in f9ee155
|
fn infer_type_expression_or_tuple_type_expression(
&mut self,
expression: &ast::Expr,
) -> Type<'db> {
match expression {
ast::Expr::Tuple(ast::ExprTuple {
elts: expressions, ..
}) => Type::Tuple(TupleType::new(
self.db(),
expressions
.iter()
.map(|expression| self.infer_type_expression(expression))
.collect::<Box<_>>(),
)),
_ => self.infer_type_expression(expression),
}
} This was sort of what i was thinking, and anywhere we call infer_type_expression and want to allow tuple expressions, we can call this. This is useful for KnownInstanceType::Dict => {
self.infer_type_expression(arguments_slice);
KnownClass::Dict.to_instance(self.db())
} Where if we used infer_type_expression_or_tuple_type_expression, we can actually get the Type of the tuple back, for the future as these generics are currently not supported. But for KnownInstaneType::List, we continue to use infer_type_expression |
I'd be inclined to postpone the complicated ones like |
Okay can revert them to no error message |
|
LGTM, thank you! Will leave it for @AlexWaygood to merge in case he has any review comments. |
I need to test Slice node and ill also improve some error messages |
…test Slice in type expression
FWIW I'm not personally a big fan of providing arbitrary "examples" in the error messages ("like So I would be inclined to remove these from the error messages, but open to what others think. |
That's fair, my thinking was that "Unary operations", and others, could a bit vague for some users. Then again, you could argue, people who find themselves writing these examples in type expressions, probably know what they are called. I'm happy to revert either way |
I think the strongest argument against is that the diagnostic will literally always be pointing to a specific example in the user's code, which seems like it should make things pretty clear. Given Alex's thumbs-up above, yeah I think you should go ahead and remove these from the messages. |
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.
Thank you, this is great! I pushed a couple of nitpick changes, but nothing major.
* main: [playground] Avoid concurrent deployments (#16834) [red-knot] Infer `lambda` return type as `Unknown` (#16695) [red-knot] Move `name` field on parameter kind (#16830) [red-knot] Emit errors for more AST nodes that are invalid (or only valid in specific contexts) in type expressions (#16822) [playground] Use cursor for clickable elements (#16833) [red-knot] Deploy playground on main (#16832) Red Knot Playground (#12681) [syntax-errors] PEP 701 f-strings before Python 3.12 (#16543)
Summary
Add error messages for invalid nodes in type expressions
Fixes #16816
Test Plan
Extend annotations/invalid.md to handle these invalid AST nodes error messages