Skip to content

UP031: fix for "%.2X" % 1 generates broken code #12421

@radoering

Description

@radoering

I know fixing UP031 is unsafe, but in this case we can do better:

"%.2X" % 1 is converted to "{:.2X}".format(1) but should probably be converted to "{:02X}".format(1)

>>> "%.2X" % 1
'01'
>>> "%02X" % 1
'01'
>>> "{:02X}".format(1)
'01'
>>> "{:.2X}".format(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Precision not allowed in integer format specifier

Command: ruff check --select UP031 --fix --unsafe-fixes test.py
Ruff Version: 0.5.3

Metadata

Metadata

Assignees

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