Skip to content

[possibly-undefined] Track conditional assignment #14310

@cdce8p

Description

@cdce8p

Feature

A common false-positive pattern I've seen for possibly-undefined is separating the conditional assignment and usage. It would be awesome if mypy could track the condition and thus recognize that the usage happens with the same condition.

Not sure if it's possible to implement. Even pyright hasn't done it (yet).

/CC: @ilinum

def some_func() -> bool:
    return True

var = some_func()
other_conditional: bool = True

if var:
    description = "Hallo"

... # some other code, which doesn't modify `var`

if var:  # maybe a bit easier
    print(description)  # Name "description" may be undefined

if var and other_conditional:
    print(description)  # Name "description" may be undefined

This will likely only work for local variables as we can't easily be sure instance attributes aren't changed by the code block in between the definition and usage.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions