Skip to content

False positive used-before-assignment when TYPE_CHECKING is used with if/elif/else blocks #7574

Closed
@yilei

Description

@yilei

Bug description

Example

# pylint: disable=import-error,missing-function-docstring,invalid-name
"""."""

from typing import TYPE_CHECKING, Optional
from package import runtime


if TYPE_CHECKING:
    from package import module
elif runtime.is_local:
    from package import module
else:
    module = None


def diagnose() -> Optional[module.Result]:
    if module is not None:
        return module.diagnose_result()
    return None

Configuration

No response

Command used

pylint a.py

Pylint output

************* Module a
a.py:16:27: E0601: Using variable 'module' before assignment (used-before-assignment)

------------------------------------------------------------------
Your code has been rated at 5.45/10 (previous run: 5.45/10, +0.00)

Expected behavior

No used-before-assignment should be raised.

Pylint version

pylint 2.15.3
astroid 2.12.10
Python 3.8.10 (default, Jul  6 2022, 10:42:32)
[Clang 13.0.0 (clang-1300.0.29.30)]

OS / Environment

No response

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: used-before-assignmentIssues related to 'used-before-assignment' checkControl flowRequires control flow understandingFalse 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