Skip to content

Commit

Permalink
[red-knot] Fix panic related to f-strings in annotations (#14613)
Browse files Browse the repository at this point in the history
## Summary

Fix panics related to expressions without inferred types in invalid
syntax examples like:
```py
x: f"Literal[{1 + 2}]" = 3
```
where the `1 + 2` expression (and its sub-expressions) inside the
annotation did not have an inferred type.

## Test Plan

Added new corpus test.
  • Loading branch information
sharkdp authored Nov 26, 2024
1 parent cd0c972 commit b94d6cf
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4229,6 +4229,7 @@ impl<'db> TypeInferenceBuilder<'db> {
"annotation-f-string",
format_args!("Type expressions cannot use f-strings"),
);
self.infer_fstring_expression(fstring);
Type::Unknown
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x: f"Literal[{1 + 2}]" = 3
2 changes: 1 addition & 1 deletion crates/red_knot_workspace/tests/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ const KNOWN_FAILURES: &[(&str, bool, bool)] = &[
("crates/ruff_linter/resources/test/fixtures/pyupgrade/UP039.py", true, false),
// related to circular references in type aliases (salsa cycle panic):
("crates/ruff_python_parser/resources/inline/err/type_alias_invalid_value_expr.py", true, true),
// related to f-strings in annotations (invalid syntax)
// related to circular references in f-string annotations (invalid syntax)
("crates/ruff_linter/resources/test/fixtures/pyflakes/F821_15.py", true, true),
("crates/ruff_linter/resources/test/fixtures/pyflakes/F821_14.py", false, true),
];

0 comments on commit b94d6cf

Please sign in to comment.