mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Spurious assignment error in disjunt condition branches

Open ZeeD opened this issue 1 year ago • 0 comments

Playground url: https://mypy-play.net/?mypy=latest&python=3.12&flags=strict%2Cwarn-unused-configs&gist=d2e9766efcc27a2bc27d7226c0f6b444

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)

ZeeD avatar Aug 19 '24 09:08 ZeeD