mirror of https://github.com/astral-sh/ruff
## Summary Fixes https://github.com/astral-sh/ruff/issues/19771 Fixes incorrect parsing of Unicode named escape sequences like `Hey \N{snowman}` in `FormatString`, which were being incorrectly split into separate literal and field parts instead of being treated as a single literal unit. ## Problem The `FormatString` parser incorrectly handles Unicode named escape sequences: - **Current**: `Hey \N{snowman}` is parsed into 2 parts `Literal("Hey \N")` & `Field("snowman")` - **Expected**: `Hey \N{snowman}` should be parsed into 1 part `Literal("Hey \N{snowman}")` This affects f-string conversion rules when fixing `UP032` that rely on proper format string parsing. ## Solution I modified `parse_literal` to detect and handle Unicode named escape sequences before parsing single characters: - Introduced a flag to track when a backslash is "available" to escape something. - When the flag is `true`, and the text starts with `N{`, try to parse the complete Unicode escape sequence as one unit, and set the flag to `false` after parsing successfully. - Set the flag to `false` when the backslash is already consumed. ## Manual Verification `"\N{angle}AOB = {angle}°".format(angle=180)` **Result** ```bash def foo(): - "\N{angle}AOB = {angle}°".format(angle=180) + f"\N{angle}AOB = {180}°" Would fix 1 error. ``` `"\N{snowman} {snowman}".format(snowman=1)` **Result** ```bash def foo(): - "\N{snowman} {snowman}".format(snowman=1) + f"\N{snowman} {1}" Would fix 1 error. ``` `"\\N{snowman} {snowman}".format(snowman=1)` **Result** ```bash def foo(): - "\\N{snowman} {snowman}".format(snowman=1) + f"\\N{1} {1}" Would fix 1 error. ``` ## Test Plan - Added test cases (happy case, invalid case, edge case) for `FormatString` when parsing Unicode escape sequence. - Updated snapshots. |
||
|---|---|---|
| .. | ||
| resources | ||
| src | ||
| Cargo.toml | ||