-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
pyflakes
] Fix infinite loop with unused local import in `__init__.…
…py` (`F401`) (#15517) ## Summary This fixes the infinite loop reported in #12897, where an `unused-import` that is undefined at the scope of `__all__` is "fixed" by adding it to `__all__` repeatedly. These changes make it so that only imports in the global scope will be suggested to add to `__all__` and the unused local import is simply removed. ## Test Plan Added a CLI integration test that sets up the same module structure as the original report Closes #12897 --------- Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
- Loading branch information
1 parent
6f0b662
commit ca3b210
Showing
5 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
crates/ruff_linter/resources/test/fixtures/pyflakes/F401_33/__init__.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"""Regression test for https://github.com/astral-sh/ruff/issues/12897""" | ||
|
||
__all__ = ('Spam',) | ||
|
||
|
||
class Spam: | ||
def __init__(self) -> None: | ||
from F401_33.other import Ham |
1 change: 1 addition & 0 deletions
1
crates/ruff_linter/resources/test/fixtures/pyflakes/F401_33/other.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Ham = 1 |
This file contains 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
This file contains 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
19 changes: 19 additions & 0 deletions
19
...es/snapshots/ruff_linter__rules__pyflakes__tests__preview__F401_F401_33____init__.py.snap
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pyflakes/mod.rs | ||
snapshot_kind: text | ||
--- | ||
__init__.py:8:35: F401 [*] `F401_33.other.Ham` imported but unused | ||
| | ||
6 | class Spam: | ||
7 | def __init__(self) -> None: | ||
8 | from F401_33.other import Ham | ||
| ^^^ F401 | ||
| | ||
= help: Remove unused import: `F401_33.other.Ham` | ||
|
||
ℹ Unsafe fix | ||
5 5 | | ||
6 6 | class Spam: | ||
7 7 | def __init__(self) -> None: | ||
8 |- from F401_33.other import Ham | ||
8 |+ pass |