possibly-used-before-assignment / E0606ΒΆ

Message emitted:

Possibly using variable %r before assignment

Description:

Emitted when a local variable is accessed before its assignment took place in both branches of an if/else switch.

Problematic code:

def check_lunchbox(items: list[str]):
    if not items:
        empty = True
    print(empty)  # [possibly-used-before-assignment]

Correct code:

def check_lunchbox(items: list[str]):
    empty = False
    if not items:
        empty = True
    print(empty)

Additional details:

If you rely on a pattern like:

if guarded():
    var = 1

if guarded():
    print(var)  # emits possibly-used-before-assignment

you may be concerned that possibly-used-before-assignment is not totally useful in this instance. However, consider that pylint, as a static analysis tool, does not know if guarded() is deterministic or talks to a database. (Likewise, for guarded instead of guarded(), any other part of your program may have changed its value in the meantime.)

Created by the variables checker.