-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Open
Labels
bugSomething isn't workingSomething isn't workingpreviewRelated to preview mode featuresRelated to preview mode featuresruleImplementing or modifying a lint ruleImplementing or modifying a lint rule
Description
Summary
logging-eager-conversion (RUF065) calls oct and hex calls unnecessary with the s conversion type, but they are necessary when any of these is true of the conversion specifier:
- The conversion flag
0is used, the conversion flag-is not used, and a minimum width is specified. - The conversion flag
is used. - The conversion flag
+is used. - A precision is specified.
The string type s interprets those components differently than the numeric types o and x. RUF065’s recommendation leads to a change in behavior, so the calls are not unnecessary, so RUF065 should be suppressed in those cases. Example:
$ cat >ruf065.py <<'# EOF'
import logging
logging.warning("%06s", oct(123))
logging.warning("% s", oct(123))
logging.warning("%+s", oct(123))
logging.warning("%.3s", hex(123))
# EOF
$ python ruf065.py
WARNING:root: 0o173
WARNING:root:0o173
WARNING:root:0o173
WARNING:root:0x7
$ ruff --isolated check ruf065.py --select RUF065 --preview --output-format concise -q
ruf065.py:2:25: RUF065 Unnecessary `oct()` conversion when formatting with `%s`. Use `%#o` instead of `%s`
ruf065.py:3:24: RUF065 Unnecessary `oct()` conversion when formatting with `%s`. Use `%#o` instead of `%s`
ruf065.py:4:24: RUF065 Unnecessary `oct()` conversion when formatting with `%s`. Use `%#o` instead of `%s`
ruf065.py:5:25: RUF065 Unnecessary `hex()` conversion when formatting with `%s`. Use `%#x` instead of `%s`
$ sed -E 's/%/%#/; /oct/s/s"/o"/; /hex/s/s"/x"/; s/hex|oct//' ruf065.py | tee ruf065_fixed.py
import logging
logging.warning("%#06o", (123))
logging.warning("%# o", (123))
logging.warning("%#+o", (123))
logging.warning("%#.3x", (123))
$ python ruf065_fixed.py
WARNING:root:0o0173
WARNING:root: 0o173
WARNING:root:+0o173
WARNING:root:0x07bVersion
ruff 0.14.5 (87dafb8 2025-11-13)
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingpreviewRelated to preview mode featuresRelated to preview mode featuresruleImplementing or modifying a lint ruleImplementing or modifying a lint rule