[flake8-type-checking] Add test cases for TC004#3
[flake8-type-checking] Add test cases for TC004#3gpilikin wants to merge 10 commits intofix/unused_importsfrom
Conversation
Fix error when importing modules without submodules. Add attribute check for similar imports with submodule.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I haven't dug deep into this yet - just added some test cases and ran cargo insta review.
There was a problem hiding this comment.
You'd need to run cargo insta test once to get updated snapshots. cargo insta review doesn't really do anything on its own without the temporary diff snapshot files.
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)
There was a problem hiding this comment.
Thanks, I'll try to do it later.
There was a problem hiding this comment.
Hello. Ah, I see. I was doing it by creating snapshots via cargo test and then running cargo insta review. I didn’t know you could just run cargo insta test to create snapshots.
There was a problem hiding this comment.
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.
Also cargo test does work, yes. But cargo insta test is more convenient, especially if there are multiple snapshots that need to be updated.
There was a problem hiding this comment.
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.
From what I understand, the imports are shadowing each other, making them harder to access.
Check the last two commits.
If I understand correctly, is this like in your PR?
There was a problem hiding this comment.
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:
import a.b.c
TYPE_CHECKING = False
if TYPE_CHECKING:
import a.b
class Foo(a.b.c.Bar):
def baz() -> a.b.x: ...
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.
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 for binding_id in scope.binding_ids() with for _, binding_id in scope.all_bindings(), that should be faster than going from each binding back to its shadowed bindings.
2f238e1 to
8e09a96
Compare
| let binding_name = top_binding | ||
| .name(checker.source()) | ||
| .split('.') | ||
| .next() | ||
| .unwrap_or(""); | ||
|
|
||
| // NOTE: It’s necessary to go through all bindings, including shadowed ones, | ||
| // using the module name. | ||
| for binding in scope | ||
| .get_all(&binding_name.nfkc().collect::<String>()) | ||
| .map(|binding_id| checker.semantic().binding(binding_id)) |
There was a problem hiding this comment.
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):
There was a problem hiding this comment.
Or rather as pointed out above, you might be able to revert everything and only have to replace scope.binding_ids() with scope.all_bindings().
Add test cases for astral-sh#13965 (comment)