-
Notifications
You must be signed in to change notification settings - Fork 0
[flake8-type-checking] Add test cases for TC004 #3
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
base: fix/unused_imports
Are you sure you want to change the base?
Changes from all commits
2698972
60281d3
9c27ead
4f20c21
1c443cb
8d0d50c
2f238e1
ed03bb8
9233817
996c5d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you getting a false negative with your approach here as well or did you just copy over the snapshots from my PR? I was kind of hoping your approach wouldn't lead to source order dependent false negatives.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't dug deep into this yet - just added some test cases and ran There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'd need to run But you can also just merge this and let the CI tell you if some of the snapshots need to be updated. (Or enable GitHub actions on your fork and trigger the CI using a force push)
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I'll try to do it later.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello. Ah, I see. I was doing it by creating snapshots via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a shame, but it's still better than without the fix, so I'll take what I can get. Maybe the source order dependency will be easier to fix as a follow-up with the additional contextual information your fix provides. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lol) I actually ran into this when fixing a rule related to unused imports. The solution was to search for all bindings by explicitly using the module name. Check the last two commits. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I used a similar approach in one of my attempts to fix this. I'm worried there are still other corner-cases that aren't handled correctly though, like the following example: Or the reverse or if you add more imports with different levels. Making each reference prioritize binding to the correct import for the purposes of this check is challenging. It looks like a good start towards further improving results though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, if what your fix does, is ensure that references point to the correct import, then you can probably make a much smaller patch and just iterate over all the bindings in the outermost loop, i.e. replace |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| --- | ||
| source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs | ||
| --- | ||
| <filename>:5:12: TC004 [*] Move import `importlib.abc` out of type-checking block. Import is used for more than type hinting. | ||
| | | ||
| 3 | from typing import TYPE_CHECKING | ||
| 4 | if TYPE_CHECKING: | ||
| 5 | import importlib.abc | ||
| | ^^^^^^^^^^^^^ TC004 | ||
| 6 | import importlib.machinery | ||
| 7 | class Foo(importlib.abc.MetaPathFinder): | ||
| | | ||
| = help: Move out of type-checking block | ||
|
|
||
| ℹ Unsafe fix | ||
| 1 1 | | ||
| 2 2 | from __future__ import annotations | ||
| 3 3 | from typing import TYPE_CHECKING | ||
| 4 |+import importlib.abc | ||
| 4 5 | if TYPE_CHECKING: | ||
| 5 |- import importlib.abc | ||
| 6 6 | import importlib.machinery | ||
| 7 7 | class Foo(importlib.abc.MetaPathFinder): | ||
| 8 8 | def bar(self) -> importlib.machinery.ModuleSpec: ... |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| --- | ||
| source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs | ||
| --- | ||
| <filename>:6:12: TC004 [*] Move import `importlib.abc` out of type-checking block. Import is used for more than type hinting. | ||
| | | ||
| 4 | if TYPE_CHECKING: | ||
| 5 | import importlib.machinery | ||
| 6 | import importlib.abc | ||
| | ^^^^^^^^^^^^^ TC004 | ||
| 7 | class Foo(importlib.abc.MetaPathFinder): | ||
| 8 | def bar(self) -> importlib.machinery.ModuleSpec: ... | ||
| | | ||
| = help: Move out of type-checking block | ||
|
|
||
| ℹ Unsafe fix | ||
| 1 1 | | ||
| 2 2 | from __future__ import annotations | ||
| 3 3 | from typing import TYPE_CHECKING | ||
| 4 |+import importlib.abc | ||
| 4 5 | if TYPE_CHECKING: | ||
| 5 6 | import importlib.machinery | ||
| 6 |- import importlib.abc | ||
| 7 7 | class Foo(importlib.abc.MetaPathFinder): | ||
| 8 8 | def bar(self) -> importlib.machinery.ModuleSpec: ... |
| 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 | ||
| --- | ||
|
|
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.
It might be more efficient to use the existing method on the semantic model to iterate through the shadowed bindings (unless your change makes those bindings not always shadow each other):
https://github.com/astral-sh/ruff/blob/91b7a570c2bd1c9e1cab894ded866e885f28946a/crates/ruff_python_semantic/src/model.rs#L2038-L2075
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.
Or rather as pointed out above, you might be able to revert everything and only have to replace
scope.binding_ids()withscope.all_bindings().