Skip to content

Conversation

@danparizher
Copy link
Contributor

Summary

Fixes false positive where UP029 incorrectly flagged relative imports from local modules named builtins (e.g., from .builtins import next) as unnecessary builtin imports. The rule should only flag imports from Python's standard builtins module, not from local modules.

Fixes #21307

Problem Analysis

The UP029 rule checks for unnecessary imports from Python's builtins module. However, it was incorrectly flagging relative imports like from .builtins import next as unnecessary, even though these are importing from a local module named builtins, not Python's builtins module.

The bug occurred because the rule only checked if the module name matched "builtins" without distinguishing between:

  • Absolute imports: from builtins import next (should be flagged)
  • Relative imports: from .builtins import next (should NOT be flagged)

This caused issues in projects like aioitertools that reimplement builtins as async-compatible variants in a local .builtins module.

Approach

The fix adds a check for relative imports by:

  1. Adding a level: u32 parameter to the unnecessary_builtin_import function to detect import level
  2. Adding an early return when level > 0 (indicating a relative import) before checking the module name
  3. Updating the call site to pass the level parameter from the AST node

This ensures that relative imports are skipped entirely, as they're importing from local modules, not Python's builtins. Absolute imports continue to work correctly and are still flagged as expected.

The fix is minimal and surgical - it only adds the relative import check without modifying any existing logic for absolute imports.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 6, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@amyreese amyreese requested a review from ntBre November 7, 2025 16:00
@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Nov 7, 2025
@MichaReiser MichaReiser merged commit 6185a2a into astral-sh:main Nov 7, 2025
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UP029 false positive on relative imports from local .builtins module

3 participants