Skip to content

ruff suggests invalid code when fixing UP032 #21000

@dima179-fuse

Description

@dima179-fuse

Summary

Consider the following code:

if __name__ == "__main__":
    number = 0
    string = "{}".format(number := number + 1)
    print(string)

It executes successfully and prints 1.

When I run ruff, it returns

bad_ruff.py:3:14: UP032 [*] Use f-string instead of `format` call
  |
1 | if __name__ == "__main__":
2 |     number = 0
3 |     string = "{}".format(number := number + 1)
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP032
4 |     print(string)
  |
  = help: Convert to f-string

When I run ruff check --fix bad_ruff.py, the resulting code is invalid and results in ValueError:

if __name__ == "__main__":
    number = 0
    string = f"{number := number + 1}"
    print(string)

Version

ruff 0.11.13 (5faf72a 2025-06-05)

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