Skip to content

F523 fix changes behavior by removing str.format call #15557

@dscorbett

Description

@dscorbett

When the fix for string-dot-format-extra-positional-arguments (F523) removes a string’s format method call, it can change the program’s behavior. If the string contains a double brace like {{, it originally represents an escape sequence for a single brace like {, but with the method call removed it literally represents a double brace. If the string contains an unset field name, removing the method call suppresses the expected KeyError.

$ ruff --version
ruff 0.9.2

$ cat f523.py
print("{{".format("!"))
print("{x}".format("!"))

$ python f523.py
{
Traceback (most recent call last):
  File "f523.py", line 2, in <module>
    print("{x}".format("!"))
          ^^^^^^^^^^^^^^^^^
KeyError: 'x'

$ ruff --isolated check --fix --select F523 f523.py
Found 2 errors (2 fixed, 0 remaining).

$ python f523.py
{{
{x}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violations

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions