Skip to content

False positive unused-variable for TYPE_CHECKING imports #8696

@zenlyj

Description

@zenlyj

Bug description

When allow-global-unused-variables is set to false in the configuration, the unused-variable warning is raised for imports made under TYPE_CHECKING guard clauses. This behavior seems inconsistent with unused-import, which is not raised for such cases.

# test.py
# pylint: disable=missing-module-docstring

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    import math  # [unused-variable]

Configuration

[tool.pylint.variables]
allow-global-unused-variables=false

Command used

pylint test.py

Pylint output

************* Module test
test.py:6:4: W0612: Unused variable 'math' (unused-variable)

Expected behavior

No warnings raised.

Pylint version

pylint 3.0.0b1
astroid 3.0.0a2
Python 3.8.10 (default, Nov 14 2022, 12:59:47)
[GCC 9.4.0]

OS / Environment

WSL2 Ubuntu 20.04 LTS

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: used-before-assignmentIssues related to 'used-before-assignment' checkFalse Positive 🦟A message is emitted but nothing is wrong with the codeNeeds 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