Skip to content

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

Closed
@skeeto

Description

@skeeto

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🪲False Negative 🦋No message is emitted but something is wrong with the codeImport systemNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions