[red-knot] Do not panic on f-string format spec expressions (#14436)

## Summary

Previously, we panicked on expressions like `f"{v:{f'0.2f'}}"` because
we did not infer types for expressions nested inside format spec
elements.

## Test Plan

```
cargo nextest run -p red_knot_workspace -- --ignored linter_af linter_gz
```
This commit is contained in:
David Peter 2024-11-19 10:04:51 +01:00 committed by GitHub
parent d8538d8c98
commit f8c20258ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 2 deletions

View file

@ -2285,6 +2285,12 @@ impl<'db> TypeInferenceBuilder<'db> {
} = expression;
let ty = self.infer_expression(expression);
if let Some(ref format_spec) = format_spec {
for element in format_spec.elements.expressions() {
self.infer_expression(&element.expression);
}
}
// TODO: handle format specifiers by calling a method
// (`Type::format`?) that handles the `__format__` method.
// Conversion flags should be handled before calling `__format__`.

View file

@ -0,0 +1,3 @@
msg = "hello"
f"{msg!r:>{10+10}}"

View file

@ -283,9 +283,7 @@ const KNOWN_FAILURES: &[(&str, bool, bool)] = &[
("crates/ruff_linter/resources/test/fixtures/pyflakes/F821_20.py", true, true),
("crates/ruff_linter/resources/test/fixtures/pyflakes/F821_26.py", true, false),
// Fails for unknown reasons:
("crates/ruff_linter/resources/test/fixtures/pyflakes/F541.py", true, true),
("crates/ruff_linter/resources/test/fixtures/pyflakes/F632.py", true, true),
("crates/ruff_linter/resources/test/fixtures/pyflakes/F811_19.py", true, false),
("crates/ruff_linter/resources/test/fixtures/pyupgrade/UP039.py", true, false),
("crates/ruff_python_parser/resources/valid/expressions/f_string.py", true, true),
];