-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Open
Labels
Description
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 undefinedThis 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.
Reactions are currently unavailable