astral-sh/ruff

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

Chiusa

#18.816 aperta il 20 giu 2025

 (4 commenti) (0 reazioni) (0 assegnatari)Rust (2088 fork)batch import
bugfixeshelp wanted

Metriche repository

Star
 (47.527 stelle)
Metriche merge PR
 (Merge medio 3g 8h) (573 PR mergiate in 30 g)

Descrizione

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

Guida contributor