Skip to content

Commit

Permalink
Fix PLE251 rules with f-string escaping
Browse files Browse the repository at this point in the history
**Summary** The `value` of the `FStringMiddle` for `f"""}}a�b"""` is `}a�b`, i.e. the curly brace escaping is decoded. If we iterate over string this gives us false indices causing exploding fixes for PLE251 rules (PLE2510, PLE2512, PLE2513, PLE2514, PLE2515). Instead, we now use the source range.

Handles #7455 (comment)
Handles #7455 (comment)

**Test Plan** Minimized fuzzing cases as fixtures.
  • Loading branch information
konstin committed Oct 1, 2023
1 parent 2838f7a commit 091f845
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ pub(crate) fn invalid_string_characters(
) {
let text = match tok {
Tok::String { .. } => locator.slice(range),
Tok::FStringMiddle { value, .. } => value.as_str(),
// The string value has already decoded escaped curly braces, which would gives us wrong
// column information
Tok::FStringMiddle { .. } => &locator.contents()[range],
_ => return,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ invalid_characters.py:55:21: PLE2510 [*] Invalid unescaped character backspace,
54 |
55 | nested_fstrings = f'{f'{f''}'}'
| PLE2510
56 |
57 | # https://github.com/astral-sh/ruff/issues/7455#issuecomment-1741998106
|
= help: Replace with escape sequence

Expand All @@ -57,5 +59,8 @@ invalid_characters.py:55:21: PLE2510 [*] Invalid unescaped character backspace,
54 54 |
55 |-nested_fstrings = f'{f'{f''}'}'
55 |+nested_fstrings = f'\b{f'{f''}'}'
56 56 |
57 57 | # https://github.com/astral-sh/ruff/issues/7455#issuecomment-1741998106
58 58 | x = f"""}}ab"""


Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ invalid_characters.py:55:25: PLE2512 [*] Invalid unescaped character SUB, use "\
54 |
55 | nested_fstrings = f'{f'{f''}'}'
| PLE2512
56 |
57 | # https://github.com/astral-sh/ruff/issues/7455#issuecomment-1741998106
|
= help: Replace with escape sequence

Expand All @@ -56,5 +58,27 @@ invalid_characters.py:55:25: PLE2512 [*] Invalid unescaped character SUB, use "\
54 54 |
55 |-nested_fstrings = f'{f'{f''}'}'
55 |+nested_fstrings = f'{f'\x1A{f''}'}'
56 56 |
57 57 | # https://github.com/astral-sh/ruff/issues/7455#issuecomment-1741998106
58 58 | x = f"""}}ab"""

invalid_characters.py:58:12: PLE2512 [*] Invalid unescaped character SUB, use "\x1A" instead
|
57 | # https://github.com/astral-sh/ruff/issues/7455#issuecomment-1741998106
58 | x = f"""}}ab"""
| PLE2512
59 | # https://github.com/astral-sh/ruff/issues/7455#issuecomment-1741998256
60 | x = f"""}}ab"""
|
= help: Replace with escape sequence

Fix
55 55 | nested_fstrings = f'{f'{f''}'}'
56 56 |
57 57 | # https://github.com/astral-sh/ruff/issues/7455#issuecomment-1741998106
58 |-x = f"""}}ab"""
58 |+x = f"""}}a\x1Ab"""
59 59 | # https://github.com/astral-sh/ruff/issues/7455#issuecomment-1741998256
60 60 | x = f"""}}ab"""


Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ invalid_characters.py:55:29: PLE2513 [*] Invalid unescaped character ESC, use "\
54 |
55 | nested_fstrings = f'{f'{f''}'}'
| PLE2513
56 |
57 | # https://github.com/astral-sh/ruff/issues/7455#issuecomment-1741998106
|
= help: Replace with escape sequence

Expand All @@ -56,5 +58,24 @@ invalid_characters.py:55:29: PLE2513 [*] Invalid unescaped character ESC, use "\
54 54 |
55 |-nested_fstrings = f'{f'{f''}'}'
55 |+nested_fstrings = f'{f'{f'\x1B'}'}'
56 56 |
57 57 | # https://github.com/astral-sh/ruff/issues/7455#issuecomment-1741998106
58 58 | x = f"""}}ab"""

invalid_characters.py:60:12: PLE2513 [*] Invalid unescaped character ESC, use "\x1B" instead
|
58 | x = f"""}}ab"""
59 | # https://github.com/astral-sh/ruff/issues/7455#issuecomment-1741998256
60 | x = f"""}}ab"""
| PLE2513
|
= help: Replace with escape sequence

Fix
57 57 | # https://github.com/astral-sh/ruff/issues/7455#issuecomment-1741998106
58 58 | x = f"""}}ab"""
59 59 | # https://github.com/astral-sh/ruff/issues/7455#issuecomment-1741998256
60 |-x = f"""}}ab"""
60 |+x = f"""}}a\x1Bb"""


Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ invalid_characters.py:53:61: PLE2515 [*] Invalid unescaped character zero-width-
53 |+zwsp_after_multicharacter_grapheme_cluster = f"ಫ್ರಾನ್ಸಿಸ್ಕೊ \u200b​"
54 54 |
55 55 | nested_fstrings = f'{f'{f''}'}'
56 56 |

invalid_characters.py:53:62: PLE2515 [*] Invalid unescaped character zero-width-space, use "\u200B" instead
|
Expand All @@ -161,5 +162,6 @@ invalid_characters.py:53:62: PLE2515 [*] Invalid unescaped character zero-width-
53 |+zwsp_after_multicharacter_grapheme_cluster = f"ಫ್ರಾನ್ಸಿಸ್ಕೊ ​\u200b"
54 54 |
55 55 | nested_fstrings = f'{f'{f''}'}'
56 56 |


0 comments on commit 091f845

Please sign in to comment.