Skip to content

Conversation

@danparizher
Copy link
Contributor

Summary

Fixes a false positive in RUF065 when oct() or hex() are used with conversion specifiers that have complex flags or precision.

Fixes #21458.

Problem

The rule incorrectly flagged oct() and hex() calls as unnecessary when conversion specifiers had flags (0, , +) or precision, which change behavior between %s and %#o/%#x.

Approach

Added has_complex_conversion_specifier() helper to detect complex specifiers (zero-pad with width, blank sign, sign char, or precision) and updated oct() and hex() checks to skip reporting when the specifier is complex.

Test Plan

Added test cases to RUF065_0.py covering %06s, % s, %+s, and %.3s with oct() and hex().

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 14, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@amyreese amyreese added the fixes Related to suggested fixes for violations label Nov 18, 2025
@ntBre ntBre requested a review from amyreese November 18, 2025 21:42
@amyreese
Copy link
Member

amyreese commented Nov 18, 2025

Pushed some changes to reduce verbosity (and duplication) of comments, and to condense the logic in has_complex_conversion_specifier. Also moved the "OK" cases into RUF065_1.py alongside similar cases, and included a link to the github issue.

@amyreese amyreese requested a review from ntBre November 18, 2025 21:45
@MichaReiser MichaReiser added bug Something isn't working rule Implementing or modifying a lint rule and removed fixes Related to suggested fixes for violations labels Nov 19, 2025
@MichaReiser MichaReiser merged commit c796a70 into astral-sh:main Nov 19, 2025
37 checks passed
dcreager added a commit that referenced this pull request Nov 19, 2025
* origin/main:
  [ty] Fix flaky tests on macos (#21524)
  [ty] Add tests for generic implicit type aliases (#21522)
  [ty] Semantic tokens: consistently add the `DEFINITION` modifier (#21521)
  Only render hyperlinks for terminals known to support them (#21519)
  [ty] Keep colorizing `mypy_primer` output (#21515)
  [ty] Exit with `2` if there's any IO error (#21508)
  [`ruff`] Fix false positive for complex conversion specifiers in `logging-eager-conversion` (`RUF065`) (#21464)
  [ty] tighten up handling of subscripts in type expressions (#21503)
dcreager added a commit that referenced this pull request Nov 19, 2025
* origin/main:
  [ty] Fix flaky tests on macos (#21524)
  [ty] Add tests for generic implicit type aliases (#21522)
  [ty] Semantic tokens: consistently add the `DEFINITION` modifier (#21521)
  Only render hyperlinks for terminals known to support them (#21519)
  [ty] Keep colorizing `mypy_primer` output (#21515)
  [ty] Exit with `2` if there's any IO error (#21508)
  [`ruff`] Fix false positive for complex conversion specifiers in `logging-eager-conversion` (`RUF065`) (#21464)
  [ty] tighten up handling of subscripts in type expressions (#21503)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants