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

Type hints persist module usage between independent modules (unused-import) #4150

Closed
skeeto opened this issue Feb 26, 2021 · 1 comment · Fixed by #7621
Closed

Type hints persist module usage between independent modules (unused-import) #4150

skeeto opened this issue Feb 26, 2021 · 1 comment · Fixed by #7621
Labels
Bug 🪲 False Negative 🦋 No message is emitted but something is wrong with the code Import system Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@skeeto
Copy link

skeeto commented Feb 26, 2021

When a module is used in a type hint in a certain way it will count as using the module in all source files analyzed afterwards. This leads to a false negative for unused-import depending on the order that sources were scanned.

Steps to reproduce

Given two files, a.py:

"x"
import uuid
from typing import Optional
ID = None  # type: Optional[uuid.UUID]

And b.py:

"x"
import uuid

Current behavior

When analyzing a.py then b.py it misses the unused-import:

$ pylint a.py b.py

----------------------------------------------------------------------
Your code has been rated at 10.00/10

But swap the order and unused-import appears:

$ pylint b.py a.py
************* Module b
b.py:2:0: W0611: Unused import uuid (unused-import)

-----------------------------------
Your code has been rated at 7.50/10

Expected behavior

The outputs should be identical and both report unused-import.

pylint --version output

Result of pylint --version output:

pylint 2.7.1
astroid 2.5
Python 3.7.3 (default, Jul 25 2020, 13:03:44) 
[GCC 8.3.0]
@hippo91
Copy link
Contributor

hippo91 commented Apr 8, 2021

@skeeto thanks for the report.

@hippo91 hippo91 added Bug 🪲 False Negative 🦋 No message is emitted but something is wrong with the code Import system labels Apr 8, 2021
@Pierre-Sassoulas Pierre-Sassoulas added the Needs PR This issue is accepted, sufficiently specified and now needs an implementation label Jul 2, 2022
mbyrnepr2 added a commit to mbyrnepr2/pylint that referenced this issue Oct 14, 2022

Partially verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
…les & one module uses an import in a type annotation & the same import is unused in a subsequent module.

Closes pylint-dev#4150
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.16.0 milestone Oct 15, 2022
Pierre-Sassoulas added a commit that referenced this issue Oct 29, 2022

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
When linting multiple modules & one module uses an import in a type annotation & the same import is unused in a subsequent module.

Closes #4150

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 False Negative 🦋 No message is emitted but something is wrong with the code Import system Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants