ruff/crates/ruff_python_parser/src/parser
Brent Westbrook 38c074e67d
Catch syntax errors in nested interpolations before Python 3.12 (#20949)
Summary
--

This PR fixes the issue I added in #20867 and noticed in #20930. Cases
like this
cause an error on any Python version:

```py
f"{1:""}"
```

which gave me a false sense of security before. Cases like this are
still
invalid only before 3.12 and weren't flagged after the changes in
#20867:

```py
f'{1: abcd "{'aa'}" }'
           # ^  reused quote
f'{1: abcd "{"\n"}" }'
            # ^  backslash
```

I didn't recognize these as nested interpolations that also need to be
checked
for invalid expressions, so filtering out the whole format spec wasn't
quite
right. And `elements.interpolations()` only iterates over the outermost 
interpolations, not the nested ones.

There's basically no code change in this PR, I just moved the existing
check
from `parse_interpolated_string`, which parses the entire string, to
`parse_interpolated_element`. This kind of seems more natural anyway and
avoids
having to try to recursively visit nested elements after the fact in
`parse_interpolated_string`. So viewing the diff with something like

```
git diff --color-moved --ignore-space-change --color-moved-ws=allow-indentation-change main
```

should make this more clear.

Test Plan
--

New tests
2025-10-20 09:03:13 -04:00
..
snapshots Improved error recovery for unclosed strings (including f- and t-strings) (#20848) 2025-10-15 09:50:56 +02:00
expression.rs Catch syntax errors in nested interpolations before Python 3.12 (#20949) 2025-10-20 09:03:13 -04:00
helpers.rs Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
mod.rs [ty] Shrink size of `AstNodeRef` (#20028) 2025-08-22 17:03:22 -04:00
options.rs [syntax-errors] Store to or delete `__debug__` (#16984) 2025-03-29 12:07:20 -04:00
pattern.rs [ty] Shrink size of `AstNodeRef` (#20028) 2025-08-22 17:03:22 -04:00
progress.rs Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
recovery.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
statement.rs Fix syntax error false positives on parenthesized context managers (#20846) 2025-10-13 14:13:27 -04:00
tests.rs Fix `unreachable` panic in parser (#19183) 2025-07-20 22:04:14 +00:00