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
This commit is contained in:
Brent Westbrook
2025-10-20 09:03:13 -04:00
committed by GitHub
parent c2ae9c7806
commit 38c074e67d
3 changed files with 335 additions and 96 deletions

View File

@@ -0,0 +1,4 @@
# parse_options: {"target-version": "3.11"}
# nested interpolations also need to be checked
f'{1: abcd "{'aa'}" }'
f'{1: abcd "{"\n"}" }'