From 6dabf045c39daa2f80c56a78d50fbdf90362f311 Mon Sep 17 00:00:00 2001 From: David Peter Date: Mon, 4 Nov 2024 15:06:54 +0100 Subject: [PATCH] [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. --- .../resources/mdtest/annotations/string_annotations.md | 9 +++++++++ crates/red_knot_python_semantic/src/types/infer.rs | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 crates/red_knot_python_semantic/resources/mdtest/annotations/string_annotations.md diff --git a/crates/red_knot_python_semantic/resources/mdtest/annotations/string_annotations.md b/crates/red_knot_python_semantic/resources/mdtest/annotations/string_annotations.md new file mode 100644 index 0000000000..0d6fe841bd --- /dev/null +++ b/crates/red_knot_python_semantic/resources/mdtest/annotations/string_annotations.md @@ -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 +``` diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index c7838114e1..f817e39e00 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -3725,7 +3725,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),