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

Mypy doesn't throw when returning a variable that is only defined conditionally #8626

Closed
ckarnell opened this issue Apr 3, 2020 · 2 comments

Comments

@ckarnell
Copy link
Contributor

ckarnell commented Apr 3, 2020

This looks like a bug to me:

def test_func() -> int:
    y: bool = False
    if y:
        x = 5
    return x # No error

When a variable is defined behind a conditional with type bool, then mypy doesn't flag that the variable might not exist outside of the condition. I noticed that mypy can tell that the variable is False, then it will at least say "cannot determine type of x", but not when the variable is just a bool.

This will very often be a runtime error, so I would expect mypy to throw here. Is this behavior intended?

@msullivan
Copy link
Collaborator

Yeah, this is #2400. It's a feature request, technically, but seems important. We'd like to get it done.

@ckarnell
Copy link
Contributor Author

ckarnell commented Apr 3, 2020

Awesome, thanks for the quick response @msullivan

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

No branches or pull requests

2 participants