Repository navigation
Fix F401 infinite loop when adding duplicate symbols to __all__ - #23052
Closed
jonathan-fulton wants to merge 1 commit into
Closed
jonathan-fulton wants to merge 1 commit into
jonathan-fulton wants to merge 1 commit into
Conversation
Adds dedup() call after sorting imports in fix_by_reexporting to prevent infinite loops when the same symbol appears multiple times due to star imports combined with explicit imports. Without deduplication, ruff would repeatedly add the same symbol to __all__, creating an infinite fix loop. Fixes astral-sh#22221
Collaborator
|
Thanks for your interest in contributing to Ruff! Unfortunately this does not seem to fix the linked issue: ❯ gh pr checkout 23052
❯ mkdir foo && echo 'import foo.bar.module
import foo.baz.module
__all__ = ["foo"]' > foo/__init__.py
❯ cargo run -p ruff -- check --no-cache --isolated --select F401 --preview foo/__init__.py
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s
Running `target/debug/ruff check --no-cache --isolated --select F401 --preview foo/__init__.py`
F401 [*] `foo.baz.module` imported but unused; consider removing, adding to `__all__`, or using a redundant alias
--> foo/__init__.py:2:8
|
1 | import foo.bar.module
2 | import foo.baz.module
| ^^^^^^^^^^^^^^
3 |
4 | __all__ = ["foo"]
|
help: Add `foo` to __all__
1 | import foo.bar.module
2 | import foo.baz.module
3 |
- __all__ = ["foo"]
4 + __all__ = ["foo", "foo"]
Found 1 error.
[*] 1 fixable with the `--fix` option.Moreover, even if it did, we need to do more than just change the autofix - the help text is also incorrect in this situation. So I think we need something closer to what I started working on in this draft #23038 Finally, I'm not sure I understand the test case you tried. The autofix would add |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #22221
Running
ruff check --fixcould enter an infinite loop when fixing F401 (unused imports) in__init__.pyfiles that combine star imports with explicit imports of the same symbol.Problem
When adding symbols to
__all__infix_by_reexporting, duplicate symbols could be collected (e.g., when a module has bothfrom foo import *andfrom foo import bar). Without deduplication, ruff would repeatedly add the same symbol to__all__, creating an infinite fix loop.Changes
Added a
dedup()call aftersort_unstable()infix_by_reexportingto remove duplicate symbols before adding them to__all__.Test Plan
Tested with:
Before this fix,
ruff check --fix --unsafe-fixeswould loop infinitely. After this fix, it correctly adds each symbol once.