这是indexloc提供的服务,不要输入任何密码
Skip to content

RUF065: Complex conversion specifiers can make oct and hex not unnecessary #21458

@dscorbett

Description

@dscorbett

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 0 is 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:0x07b

Version

ruff 0.14.5 (87dafb8 2025-11-13)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpreviewRelated to preview mode featuresruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions