Skip to content

RUF027 creates invalid f-string interpolations with comments before Python 3.12 #23460

@dscorbett

Description

@dscorbett

Summary

The fix for missing-f-string-syntax (RUF027) should be suppressed in Python 3.11 and earlier if it would create an f-string with an embedded comment, which only became valid in Python 3.12. Example:

$ cat >ruf027.py <<'# EOF'
x = "!"
print("""{x  # }
}""")
# EOF

$ ruff --isolated check --select RUF027 ruf027.py --preview --target-version py311 --unsafe-fixes --fix --output-format concise
ruf027.py:2:15: invalid-syntax: Cannot use comments in f-strings on Python 3.11 (syntax was added in Python 3.12)
Found 1 error.

Version

ruff 0.15.2 (9d18ee9 2026-02-19)

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