Skip to content

False positive used-before-assignment for type annotations that take assignment via nonlocal #5394

Closed
@IRDonch

Description

@IRDonch

Bug description

Create test.py with the following content:

def f():
    x: int

    def g():
        nonlocal x
        x = 5

    g()
    print(x)

f()

Configuration

No response

Command used

pylint test.py

Pylint output

************* Module test
test.py:9:10: E0602: Undefined variable 'x' (undefined-variable)

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

Expected behavior

There should be no errors reported.

Pylint version

pylint 2.12.1
astroid 2.9.0
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0]

OS / Environment

No response

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🪲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 code

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions