-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[flake8-type-checking] Avoid TC004 false positives for submodules
#16186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Daverball
wants to merge
3
commits into
astral-sh:main
Choose a base branch
from
Daverball:bugfix/tc004-overlapping-import-bindings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
4 changes: 4 additions & 0 deletions
4
...s/ruff_linter__rules__flake8_type_checking__tests__github_issue_15723_false_negative.snap
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs | ||
| --- | ||
|
|
26 changes: 26 additions & 0 deletions
26
...ff_linter__rules__flake8_type_checking__tests__github_issue_15723_ideal_import_order.snap
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| --- | ||
| source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs | ||
| --- | ||
| <filename>:8:12: TC004 [*] Move import `importlib.abc` out of type-checking block. Import is used for more than type hinting. | ||
| | | ||
| 6 | if TYPE_CHECKING: | ||
| 7 | import importlib.machinery | ||
| 8 | import importlib.abc | ||
| | ^^^^^^^^^^^^^ TC004 | ||
| 9 | | ||
| 10 | class Foo(importlib.abc.MetaPathFinder): | ||
| | | ||
| = help: Move out of type-checking block | ||
|
|
||
| ℹ Unsafe fix | ||
| 2 2 | from __future__ import annotations | ||
| 3 3 | | ||
| 4 4 | from typing import TYPE_CHECKING | ||
| 5 |+import importlib.abc | ||
| 5 6 | | ||
| 6 7 | if TYPE_CHECKING: | ||
| 7 8 | import importlib.machinery | ||
| 8 |- import importlib.abc | ||
| 9 9 | | ||
| 10 10 | class Foo(importlib.abc.MetaPathFinder): | ||
| 11 11 | def bar(self) -> importlib.machinery.ModuleSpec: ... |
4 changes: 4 additions & 0 deletions
4
.../ruff_linter__rules__flake8_type_checking__tests__github_issue_15723_regression_test.snap
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs | ||
| --- | ||
|
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually it's still quite a bit more complex than that, since we can have partial overlaps and complete overlaps of bindings, so whether or not we can look at the references of a shadowing import depends on how it overlaps with our import and whether or not those overlapping imports are in a runtime context or not.
We also don't properly take into account the fact that the parent module will be fully imported by a submodule import. So for the parent module we would need to check if there are any bindings in runtime context at all in order to avoid false negatives, e.g. for a runtime use of
os.sepwith a typing only import ofos.path.os.pathshould be flagged if there isn't also a runtimeosimport.So for something like
For reference
xwe would need to detect that the shadowed bindingaforimport adoesn't includea.b, but for referenceywe would need to detect thatimport a.b.cfully covers the shadowedimport a.b, so we don't have to worry about it. But forzimport a.b.cdoesn't fully coverimport a.b.d, so we do need to look at the references toimport a.b.dfor checking whether or notimport a.b.dhas any runtime references. So the combinatorics quickly get out of hand here.The approach chosen in this PR still seems like a reasonable compromise however, since it will catch some things with submodule imports, while incorrectly flagging fewer imports that shouldn't be flagged. I.e. we raise the false negative rate slightly, but we get rid of the false positives we're most likely to encounter in the wild.
Anything more accurate would probably need multi-file analysis and some degree of type inference, so we actually know which attributes are currently available at runtime in the current scope and through which binding.
Another approach we could take is to just never emit
TC004if we're shadowing another binding. This would completely get rid of any false positives. But I feel the approach this PR has taken is more balanced.