Catch syntax errors in nested interpolations before Python 3.12 #20949
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes the issue I added in #20867 and noticed in #20930. Cases like this
cause an error on any Python version:
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:
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 outermostinterpolations, 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, toparse_interpolated_element. This kind of seems more natural anyway and avoidshaving to try to recursively visit nested elements after the fact in
parse_interpolated_string. So viewing the diff with something likeshould make this more clear.
Test Plan
New tests