Skip to content

(🐞) false error: undefined-loop-variable when for loop has terminating else branch #5971

Closed
@KotlinIsland

Description

@KotlinIsland

Bug description

def foo(l):
    for i in l:
        break
    else:
        return
    print(i)

Configuration

No response

Command used

pylint test.py

Pylint output

************* Module test
test.py:6:10: W0631: Using possibly undefined loop variable 'i' (undefined-loop-variable)

Expected behavior

no error

Pylint version

pylint 2.13.0
astroid 2.11.1
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)]

OS / Environment

No response

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Control 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