Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
radoering opened this issue Jul 20, 2024 · 2 comments · Fixed by #12534
Closed

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

radoering opened this issue Jul 20, 2024 · 2 comments · Fixed by #12534
Assignees
Labels
bug Something isn't working fixes Related to suggested fixes for violations

Comments

@radoering
Copy link

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

@charliermarsh charliermarsh added bug Something isn't working fixes Related to suggested fixes for violations labels Jul 20, 2024
@charliermarsh
Copy link
Member

Agreed.

@charliermarsh
Copy link
Member

So basically: use :0 rather than . for integer-only presentation types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants