Skip to content

used-before-assignment when using walrus operator(:=) in dict, generator, some comprehensionswith function #8486

Open
@LCH-1

Description

@LCH-1

Bug description

This issue is similar to #4828 and #8125, but seems to be different

The dictionary is fine when used alone, but when used with some function, the used-before-assignment code occurs.
Like issue #8425

test code

print({
    # used-before-assignment
    "key": test1 if (test1 := "var") else ""
})

dict({
    # used-before-assignment
    "key": test2 if (test2 := "var") else ""
})

{
    # normal
    "key": test3 if (test3 := "var") else ""
}

Configuration

No response

Command used

pylint test.py --disable=W0104,C0114

Pylint output

************* Module test
test.py:3:11: E0601: Using variable 'test1' before assignment (used-before-assignment)
test.py:8:11: E0601: Using variable 'test2' before assignment (used-before-assignment)

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

Expected behavior

no error

Pylint version

pylint 2.17.1
astroid 2.15.0
Python 3.11.1

OS / Environment

No response

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Assignment expressionRelated to the walrus operator / assignment expressionC: used-before-assignmentIssues related to 'used-before-assignment' checkFalse Positive 🦟A message is emitted but nothing is wrong with the code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions