astral-sh/ruff
在 GitHub 查看[`Pylint`] `PLE2510` fix causes syntax error inside pre-3.12 f-strings
Open
#18,816 创建于 2025年6月20日
bugfixeshelp wanted
描述
Summary
If a backspace is inside an f-string replacement while a pre-3.12 version is targeted, the fix for invalid-character-backspace (PLE2510) will introduce a syntax error. This happens both inside a string inside the replacement, and inside the format specs. playground
PS D:\python_projects> Get-Content issue.py
f"{'␈'}"
^ note: I'm cheating here, PowerShell does not actually display the backspace character as ␈, it actually gets displayed as f"{'}"
PS D:\python_projects> uvx ruff check issue.py --isolated --select PLE --target-version py39
issue.py:1:5: PLE2510 [*] Invalid unescaped character backspace, use "\b" instead
|
1 | f"{'␈'}"
| ^ PLE2510
|
= help: Replace with escape sequence
Found 1 error.
[*] 1 fixable with the `--fix` option.
PS D:\python_projects> uvx ruff check issue.py --isolated --select PLE --target-version py39 --fix
issue.py:1:5: SyntaxError: Cannot use an escape sequence (backslash) in f-strings on Python 3.9 (syntax was added in Python 3.12)
|
1 | f"{'\b'}"
| ^
|
Found 2 errors (1 fixed, 1 remaining).
This also applies to PLE2512, PLE2513, and PLE2515 playground, as well as PLE2514, but the playground does not support null chars.
Version
ruff 0.12.0 (87f0feb 2025-06-17) + playground