-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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] Do not panic when encountering string annotations #14091
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# String annotations | ||
|
||
```py | ||
def f() -> "int": | ||
return 1 | ||
|
||
# TODO: We do not support string annotations, but we should not panic if we encounter them | ||
reveal_type(f()) # revealed: @Todo | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3655,7 +3655,10 @@ impl<'db> TypeInferenceBuilder<'db> { | |
// TODO: parse the expression and check whether it is a string annotation, since they | ||
// can be annotation expressions distinct from type expressions. | ||
// https://typing.readthedocs.io/en/latest/spec/annotations.html#string-annotations | ||
ast::Expr::StringLiteral(_literal) => Type::Todo, | ||
ast::Expr::StringLiteral(_literal) => { | ||
self.store_expression_type(expression, Type::Todo); | ||
Type::Todo | ||
} | ||
|
||
// Annotation expressions also get special handling for `*args` and `**kwargs`. | ||
ast::Expr::Starred(starred) => self.infer_starred_expression(starred), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect we have the same bug here, for starred expressions in an annotation. In general the contract is supposed to be that For annotations, the tricky thing is the split between annotation expressions (what is valid in an annotation) and type expressions (which is a subset of annotation expressions). Currently There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I did look at There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It uses |
||
|
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.
nit: If all we're asserting is "make sure we don't panic", then an mdtest is arguably overkill here: this could be better as a snippet added to the corpus directory (we have a test that runs over all the snippets in that directory and checks that we can at least run red-knot over them without panicking)
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 thought we would eventually have a Markdown-based test for string annotations anyway, and this test (with a non-
@Todo
-assertion) could very well be a basic initial test. I agree, so far it does little except document the fact that we don't understand string annotations yet.But I'm happy to turn it into a snippet, if you prefer that.
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.
Yeah, fair enough. I suppose the corpus directory becomes less and less useful as we expand our ambitions beyond "do not panic when checking Python" to "infer the correct types consistently everywhere when checking Python" 😆