Python: Fix false positives in py/unused-import.
#2042
Merged
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.
Fixes one of the false positives reported in #2014 (in two different ways).
Firstly, we now consider occurrences of a module name inside a string annotation of a parameter to be a "use" of that module.
Secondly, we only report unused modules arising from
importstatements that are likely to be executed, using the existence of points-to information for the module name as a proxy for reachability. This should be fairly safe -- even if we don't correctly identify what the module actually points to, we should infer that it points to some unknown module. On its own, this would also prevent the reported false positive from occurring, since the import in question is guarded by a check oftyping.TYPE_CHECKING, which isFalseexcept during type checking.This still needs careful testing to see how it affects the already existing alerts on LGTM.com.