astral-sh/ruff

[`Pylint`] `PLE2510` fix causes syntax error inside pre-3.12 f-strings

已关闭

#18,816 创建于 2025年6月20日

 (4 条评论) (0 个反应) (0 位负责人)Rust (2,088 个派生)batch import
bugfixeshelp wanted

仓库指标

星标
 (47,527 个星标)
PR 合并指标
 (平均合并 3天 8小时) (30 天内合并 573 个 PR)

描述

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

贡献者指南