Skip to content
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

"unused import" false positives #192

Closed
chris-rands opened this issue Mar 2, 2020 · 9 comments
Closed

"unused import" false positives #192

chris-rands opened this issue Mar 2, 2020 · 9 comments

Comments

@chris-rands
Copy link

Thanks for the nice tool. I've encountered a few "unused import" false positives recently of 2 types. 1st case is where the import is merely tested, e.g.:

try:
    import a
except ImportError:
    print("a not available")

2nd case is in __init__.py files, where it is common to import modules that are not directly used in the file

@jendrikseipp
Copy link
Owner

I agree that we should fix these two cases. Would you like to write a pull request for either of the two cases?

@RJ722
Copy link
Contributor

RJ722 commented Mar 31, 2020

Let's fix the first case here. I'd raise a PR.

@jendrikseipp
Copy link
Owner

Thinking about the first case again, I'm not sure whether we should do anything about it. In the example above, a should be reported as unused, I think. Or am I missing something? Do you see an example with a real false-positive unused import?

@chris-rands
Copy link
Author

sorry i've not had time to help with any PR, but to clarify the first example:

$ cat a.py 
var = True
$ cat b.py 
try:
    import a
    from a import var
    print(var)
except ImportError:
    print("a not available")
$ python b.py 
True
$ vulture b.py 
b.py:2: unused import 'a' (90% confidence)

@jendrikseipp
Copy link
Owner

Thanks for the example! However, a is still unused, since you import var directly as well.

@chris-rands
Copy link
Author

true, the actual context i found this is in unit tests where we raise a custom exception/handling when the import is not found. in this case importing a is useful without using the import, i guess probably better to white list it though

@jendrikseipp
Copy link
Owner

I agree. If you find an example where we should adapt Vulture, feel free to post it here.

@jendrikseipp
Copy link
Owner

I think in the first example, you could simply just do from scipy.linalg import expm instead of import scipy and in the second example, you could remove the imports that are unused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants