Skip to content

UP037 fix causes syntax error on string with newline escape and comment #19835

@MeGaGiGaGon

Description

@MeGaGiGaGon

Summary

If a quoted annotation contains a newline escape (\n) before a comment (#), the fix for quoted-annotation (UP037) will cause a syntax error playground:

PS ~>echo @'
from __future__ import annotations
def foo(bar: "A\n#"): ...
'@ | uvx ruff check --select UP037 - --fix 2>&1 | rg "syntax error"
error: Fix introduced a syntax error. Reverting all changes.

The fix works fine if the newline is instead added using a multiline string, so (completely guessing) this might be fixable by making these cases also take that code path.

PS ~>echo @'
from __future__ import annotations
def foo(bar: """A
#"""): ...
'@ | uvx ruff check --select UP037 - --fix
from __future__ import annotations
def foo(bar: (A
#
)): ...
Found 1 error (1 fixed, 0 remaining).

Version

ruff 0.12.8 (f51a228 2025-08-07)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions