-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
F401 - Recommend adding unused import bindings to
__all__
(#11314)
Followup on #11168 and resolve #10391 # User facing changes * F401 now recommends a fix to add unused import bindings to to `__all__` if a single `__all__` list or tuple is found in `__init__.py`. * If there are no `__all__` found in the file, fall back to recommending redundant-aliases. * If there are multiple `__all__` or only one but of the wrong type (non list or tuple) then diagnostics are generated without fixes. * `fix_title` is updated to reflect what the fix/recommendation is. Subtlety: For a renamed import such as `import foo as bees`, we can generate a fix to add `bees` to `__all__` but cannot generate a fix to produce a redundant import (because that would break uses of the binding `bees`). # Implementation changes * Add `name` field to `ImportBinding` to contain the name of the _binding_ we want to add to `__all__` (important for the `import foo as bees` case). It previously only contained the `AnyImport` which can give us information about the import but not the binding. * Add `binding` field to `UnusedImport` to contain the same. (Naming note: the field `name` field already existed on `UnusedImport` and contains the qualified name of the imported symbol/module) * Change `fix_by_reexporting` to branch on the size of `dunder_all: Vec<&Expr>` * For length 0 call the edit-producing function `make_redundant_alias`. * For length 1 call edit-producing function `add_to_dunder_all`. * Otherwise, produce no fix. * Implement the edit-producing function `add_to_dunder_all` and add unit tests. * Implement several fixture tests: empty `__all__ = []`, nonempty `__all__ = ["foo"]`, mis-typed `__all__ = None`, plus-eq `__all__ += ["foo"]` * `UnusedImportContext::Init` variant now has two fields: whether the fix is in `__init__.py` and how many `__all__` were found. # Other changes * Remove a spurious pattern match and instead use field lookups b/c the addition of a field would have required changing the unrelated pattern. * Tweak input type of `make_redundant_alias` --------- Co-authored-by: Alex Waygood <[email protected]>
- Loading branch information
1 parent
96f6288
commit da882b6
Showing
30 changed files
with
414 additions
and
67 deletions.
There are no files selected for viewing
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions
11
crates/ruff_linter/resources/test/fixtures/pyflakes/F401_26__all_empty/__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,11 @@ | ||
"""__init__.py with empty __all__ | ||
""" | ||
|
||
|
||
from . import unused # F401: add to __all__ | ||
|
||
|
||
from . import renamed as bees # F401: add to __all__ | ||
|
||
|
||
__all__ = [] |
1 change: 1 addition & 0 deletions
1
crates/ruff_linter/resources/test/fixtures/pyflakes/F401_26__all_empty/renamed.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 @@ | ||
# empty module imported by __init__.py for test fixture |
1 change: 1 addition & 0 deletions
1
crates/ruff_linter/resources/test/fixtures/pyflakes/F401_26__all_empty/unused.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 @@ | ||
# empty module imported by __init__.py for test fixture |
11 changes: 11 additions & 0 deletions
11
crates/ruff_linter/resources/test/fixtures/pyflakes/F401_27__all_mistyped/__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,11 @@ | ||
"""__init__.py with mis-typed __all__ | ||
""" | ||
|
||
|
||
from . import unused # F401: recommend add to all w/o fix | ||
|
||
|
||
from . import renamed as bees # F401: recommend add to all w/o fix | ||
|
||
|
||
__all__ = None |
1 change: 1 addition & 0 deletions
1
crates/ruff_linter/resources/test/fixtures/pyflakes/F401_27__all_mistyped/renamed.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 @@ | ||
# empty module imported by __init__.py for test fixture |
1 change: 1 addition & 0 deletions
1
crates/ruff_linter/resources/test/fixtures/pyflakes/F401_27__all_mistyped/unused.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 @@ | ||
# empty module imported by __init__.py for test fixture |
8 changes: 8 additions & 0 deletions
8
crates/ruff_linter/resources/test/fixtures/pyflakes/F401_28__all_multiple/__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 @@ | ||
"""__init__.py with multiple imports added to all in one edit | ||
""" | ||
|
||
|
||
from . import unused, renamed as bees # F401: add to __all__ | ||
|
||
|
||
__all__ = []; |
1 change: 1 addition & 0 deletions
1
crates/ruff_linter/resources/test/fixtures/pyflakes/F401_28__all_multiple/renamed.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 @@ | ||
# empty module imported by __init__.py for test fixture |
1 change: 1 addition & 0 deletions
1
crates/ruff_linter/resources/test/fixtures/pyflakes/F401_28__all_multiple/unused.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 @@ | ||
# empty module imported by __init__.py for test fixture |
16 changes: 16 additions & 0 deletions
16
crates/ruff_linter/resources/test/fixtures/pyflakes/F401_29__all_conditional/__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,16 @@ | ||
"""__init__.py with __all__ populated by conditional plus-eq | ||
multiple __all__ so cannot offer a fix to add to them | ||
""" | ||
|
||
import sys | ||
|
||
from . import unused, exported, renamed as bees | ||
|
||
if sys.version_info > (3, 9): | ||
from . import also_exported | ||
|
||
__all__ = ["exported"] | ||
|
||
if sys.version_info >= (3, 9): | ||
__all__ += ["also_exported"] |
1 change: 1 addition & 0 deletions
1
...es/ruff_linter/resources/test/fixtures/pyflakes/F401_29__all_conditional/also_exported.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 @@ | ||
# empty module imported by __init__.py for test fixture |
1 change: 1 addition & 0 deletions
1
crates/ruff_linter/resources/test/fixtures/pyflakes/F401_29__all_conditional/exported.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 @@ | ||
# empty module imported by __init__.py for test fixture |
1 change: 1 addition & 0 deletions
1
crates/ruff_linter/resources/test/fixtures/pyflakes/F401_29__all_conditional/renamed.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 @@ | ||
# empty module imported by __init__.py for test fixture |
1 change: 1 addition & 0 deletions
1
crates/ruff_linter/resources/test/fixtures/pyflakes/F401_29__all_conditional/unused.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 @@ | ||
# empty module imported by __init__.py for test fixture |
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
Oops, something went wrong.