Skip to content

Wrong fix for W605 in f-strings #11491

@FreeHarry

Description

@FreeHarry

Python: 3.12.3
Version: ruff 0.4.4
Command: ruff check --select W605 .\test.py --fix

And let's given the following code snippet in test.py:

total = 10
ok = 7
incomplete = 3
s = f"TOTAL: {total}\nOK: {ok}\INCOMPLETE: {incomplete}\n"

It is wrongly fixed as:

total = 10
ok = 7
incomplete = 3
s = rf"TOTAL: {total}\nOK: {ok}\INCOMPLETE: {incomplete}\n"

Now the string is a raw string. The newlines will then interpreted as literals and will not remain newlines.


If I do the same for a normal string:

s = "TOTAL: 10\nOK: 3\INCOMPLETE: 7\n"

It is correctly fixed as:

s = "TOTAL: 10\nOK: 3\\INCOMPLETE: 7\n"

The invalid escape sequence '\I' is correctly fixed as \\, and the newlines are still there.


Even if it is a f-string, but without placeholders, it is done correctly:

s = f"TOTAL: 10\nOK: 3\INCOMPLETE: 7\n"

Result is correct:

s = f"TOTAL: 10\nOK: 3\\INCOMPLETE: 7\n"

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions