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

Inconsistency with a variable defined inside an exclusive if. #3817

Closed
marmistrz opened this issue Aug 9, 2017 · 3 comments
Closed

Inconsistency with a variable defined inside an exclusive if. #3817

marmistrz opened this issue Aug 9, 2017 · 3 comments

Comments

@marmistrz
Copy link

mypy 0.521

Let's take this snippet. This one is perfectly fine and mypy doesn't complain

x = 7
if x == 7:
    v = 42
else:
    pass

Now, let's add another branch:

x = 7
if x == 7:
    v = 42
elif x == 8:
    v = "42"
else:
    pass

This triggers an error:

/tmp/test.py:5: error: Incompatible types in assignment (expression has type "str", variable has type "int")

It shouldn't, since the variable is not used outside the if...elif...else

@Daenyth
Copy link

Daenyth commented Aug 9, 2017

Python doesn't have block level scoping though, so v can be referenced outside the if/elif block once it's completed

@refi64
Copy link
Contributor

refi64 commented Aug 9, 2017

I think the inconsistency here is that v will be undefined if the condition is false?

@ilevkivskyi
Copy link
Member

Thank you for reporting! I dont think this is a new bug/feature request. We already have a proposal to infer Union across an if ... else ... (see #2611) so I am closing this issue.

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

4 participants