Skip to content

Catch undefined-variable for variables with only type assignment and NamedExpr calls #5197

Open
@DanielNoord

Description

@DanielNoord

#5163 added the following test to the test suite for undefined-variable:

# pylint: disable-next=fixme
# TODO: With better control-flow inference related to NamedExpr we should see that the
# NamedExpr is never called here and the second call to `var` is thus undefined
def typing_and_incorrect_assignment_expression():
    """The variable gets assigned in an assignment expression which is never called"""
    var: int
    if False:
        if (var := 1 ** 2):
            print(var)
    else:
        print(var) # This should raise

Basically, we should be able to recognise when NamedExprs have been called. In this case it clearly hasn't and thus var remains undefined.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Assignment expressionRelated to the walrus operator / assignment expressionC: undefined-variableIssues related to 'undefined-variable' checkControl flowRequires control flow understandingEnhancement ✨Improvement to a componentNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions