Skip to content

False positive used-before-assignment when assignment can be an import #3127

@mthuurne

Description

@mthuurne

Steps to reproduce

Run pylint on the following code:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from collections import Counter
else:
    Counter = object

def tick(c: Counter, name: str) -> None:
    c[name] += 1

While this example is rather pointless, I use similar constructs a lot when I have to import a name only for the purpose of type checking. It helps avoid import cycles between modules.

Current behavior

pylint reports on the def tick line:

E0601: Using variable 'Counter' before assignment (used-before-assignment)

Expected behavior

No errors are reported, since Counter is guaranteed to be defined.

pylint --version output

pylint 2.4.0
astroid 2.3.0
Python 3.7.2 (default, Feb 26 2019, 13:02:33)
[GCC 7.3.1 20180323 [gcc-7-branch revision 258812]]

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions