Skip to content

pyflakes: fix F401 infinite loop via improved submodule re-export logic - #22359

Closed
Jkhall81 wants to merge 1 commit into
astral-sh:mainfrom
Jkhall81:bugfix/22221-f401-infinite-loop
Closed

Jkhall81 wants to merge 1 commit into
astral-sh:mainfrom
Jkhall81:bugfix/22221-f401-infinite-loop

Conversation

@Jkhall81

@Jkhall81 Jkhall81 commented Jan 3, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR resolves a convergence failure (infinite loop) in rule F401 occurring in __init__.py files.

The fix is two-fold:

  1. Linter Logic: Updated unused_import.rs to recognize that a parent package listed in __all__ correctly re-exports its submodule imports (e.g., ["foo"] now covers import foo.bar).
  2. Fixer Idempotency: Hardened add_to_dunder_all in edits.rs to check for existing entries before inserting new ones. This prevents duplicate strings from being appended and ensures the fixer terminates even if the linter re-flags an entry.

Fixes #22221

Test Plan

  • Regression Tests: Added new test cases to crates/ruff_linter/src/fix/edits.rs covering duplicate detection in both lists and tuples.
  • Snapshot Updates: Updated existing pyflakes snapshots to reflect the improved submodule matching logic.
  • Manual Verification: Verified against the reproduction case in the linked issue using ruff check --fix --preview.
  • Suite Verification: Ran the full linter test suite (cargo test -p ruff_linter) with 2625 tests passing.

@amyreese
amyreese requested a review from ntBre January 8, 2026 18:03
@astral-sh-bot

astral-sh-bot Bot commented Jan 8, 2026 •

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser
MichaReiser requested review from dylwil3 and removed request for ntBre January 8, 2026 18:31
@amyreese

amyreese commented Jan 8, 2026

Copy link
Copy Markdown
Contributor

Looks like this needs a rebase to clean up ecosystem results.

@Jkhall81
Jkhall81 force-pushed the bugfix/22221-f401-infinite-loop branch from cb22d52 to e57982a Compare January 8, 2026 19:30
@MichaReiser MichaReiser added bug An issue describing something that isn't working, or a PR that fixes a bug rule Implementing or modifying a lint rule fixes Related to suggested fixes for violations and removed rule Implementing or modifying a lint rule labels Jan 9, 2026
@dylwil3

dylwil3 commented Feb 9, 2026

Copy link
Copy Markdown
Collaborator

I don't think this is quite the fix we're after, sorry - here you've just skipped the whole rule in the case where a reference is found in __all__.

Instead, we want to change the variant of the lint that we report so that we don't suggest adding anything to __all__ - this is the approach in-progress at #23038.

@dylwil3 dylwil3 closed this Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An issue describing something that isn't working, or a PR that fixes a bug fixes Related to suggested fixes for violations rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

F401 fix causes infinite loop adding duplicates to __all__

4 participants