Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spurious assignment error in disjunt condition branches #17688

Open
ZeeD opened this issue Aug 19, 2024 · 2 comments
Open

Spurious assignment error in disjunt condition branches #17688

ZeeD opened this issue Aug 19, 2024 · 2 comments
Labels
bug mypy got something wrong

Comments

@ZeeD
Copy link

ZeeD commented Aug 19, 2024

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 ZeeD added the bug mypy got something wrong label Aug 19, 2024
@DetachHead
Copy link
Contributor

duplicate of #6232?

@ZeeD
Copy link
Author

ZeeD commented Sep 5, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants