astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
32.41k stars 1.08k forks source link

UP031 does not support %d formatting #12461

Open inoa-jboliveira opened 3 months ago

inoa-jboliveira commented 3 months ago

Ruff version 0.5.4

The following conversion codes are currently being ignored by UP031 instead of showing as errors:

print("%d" % 1)
print("%i" % 1)
print("%u" % 1)
print("%c" % 1) 

E.g.:

$ ruff check --select UP --fixable UP --unsafe-fixes --isolated foo.py
All checks passed!

The presence of any of these items such as %d in a string will make it ignored by UP031 even if it has another supported one, such as %s:

print("%d - %s" % (1, 2))

Please support all of the valid conversion codes from this table: https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting

Also, if an invalid code is present (either by you not supporting or being invalid), still it should show the error for valid ones. The only difference is that you don't want to supply a fix in this case

inoa-jboliveira commented 3 months ago

Giving a quick look at the implementation, all this sounds like it is by design. There is nothing in the docs that says it, but I see a test file that have several formatting lines are being ignored.

It does sound that these options are being accepted only because there is no automated fix instead of because they are fine. There is zero reason for someone to write this in 2024 (or even 10 years ago), as the test file suggests:

"%(1)s" % {"1": "bar"}

All the old style string formatting options, except for bytestrings, should be deemed errors under UP031. Having a fix available for those cases or not is what should be considered instead.