def cond1() -> bool:
return True
def cond2() -> bool:
return True
def foo() -> None:
if cond1():
a = ''
return
if cond2():
a = 123
return
a = False
return
in this scenario, while a is a local variable the lifetime of it is determined by the if and return so there is no overlap between the case when a is a str and when is an int or a bool - those are "effectively" different variables.
Expected Behavior
no error triggered by mypy
Actual Behavior
main.py:13: error: Incompatible types in assignment (expression has type "int", variable has type "str") [assignment]
main.py:16: error: Incompatible types in assignment (expression has type "bool", variable has type "str") [assignment]
Found 2 errors in 1 file (checked 1 source file)
it's a bit of a "extended variant" (the concrete usecase is a bit different as is with if / else branches vs early return) but the gist is the same, yep
Playground url: https://mypy-play.net/?mypy=latest&python=3.12&flags=strict%2Cwarn-unused-configs&gist=d2e9766efcc27a2bc27d7226c0f6b444
in this scenario, while
a
is a local variable the lifetime of it is determined by theif
andreturn
so there is no overlap between the case whena
is astr
and when is anint
or abool
- those are "effectively" different variables.Expected Behavior
no error triggered by
mypy
Actual Behavior