Skip to content

Request: detecting bad short circuit with walrus operatorΒ #8658

Open
@jamesbraza

Description

@jamesbraza

Current problem

import random

enter_first = True
while enter_first or (some_walrus := random.random()) < 0.9:
    print(some_walrus)
    enter_first = False

This code will fail, because enter_first short circuits, and the variable some_walrus isn't set.

Traceback (most recent call last):
  File "/path/to/a.py", line 5, in <module>
    print(some_walrus)
NameError: name 'some_walrus' is not defined

Desired solution

It would be pretty cool if pylint could flag this vulnerabiltity. Basically, the idea is warning if a short circuit can lead to a local variable not being present.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Assignment expressionRelated to the walrus operator / assignment expressionC: undefined-variableIssues related to 'undefined-variable' checkFalse Negative πŸ¦‹No message is emitted but something is wrong with the codeNeeds 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