mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:25:17 +00:00
[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:
parent
d8538d8c98
commit
f8c20258ae
3 changed files with 9 additions and 2 deletions
|
@ -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__`.
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
msg = "hello"
|
||||
|
||||
f"{msg!r:>{10+10}}"
|
|
@ -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),
|
||||
];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue