mirror of
https://github.com/astral-sh/ruff
synced 2026-01-11 00:24:13 -05:00
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:
@@ -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"}" }'
|
||||
Reference in New Issue
Block a user