[red-knot] Do not panic when encountering string annotations (#14091)

## Summary

Encountered this while running red-knot benchmarks on the `black`
codebase.

Fixes two of the issues in #13478.

## Test Plan

Added a regression test.
This commit is contained in:
David Peter 2024-11-04 15:06:54 +01:00 committed by GitHub
parent df45a0e3f9
commit 6dabf045c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 1 deletions

View file

@ -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
```

View file

@ -3725,7 +3725,10 @@ impl<'db> TypeInferenceBuilder<'db> {
// TODO: parse the expression and check whether it is a string annotation, since they // TODO: parse the expression and check whether it is a string annotation, since they
// can be annotation expressions distinct from type expressions. // can be annotation expressions distinct from type expressions.
// https://typing.readthedocs.io/en/latest/spec/annotations.html#string-annotations // 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`. // Annotation expressions also get special handling for `*args` and `**kwargs`.
ast::Expr::Starred(starred) => self.infer_starred_expression(starred), ast::Expr::Starred(starred) => self.infer_starred_expression(starred),