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

False positive for Optional with if branches #5902

Closed
dedalusj opened this issue Nov 16, 2018 · 1 comment
Closed

False positive for Optional with if branches #5902

dedalusj opened this issue Nov 16, 2018 · 1 comment

Comments

@dedalusj
Copy link

dedalusj commented Nov 16, 2018

Bug report

  • Repro case
def foo(a: Dict, b: Optional[str] = None):
    if b is None and 'k' not in a:
        b = '1'
    elif 'k' in a:
        b = '2'
    c: str = b  # Incompatible types error reported here
    print(c)
  • What is the actual behavior/output?

Incompatible types error reported on c var assignment

  • What is the behavior/output you expect?

No error reported

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

mypy 0.641. The is present in master as well.

  • What are the mypy flags you are using? (For example --strict-optional)

None

@gvanrossum
Copy link
Member

Looks like you're being too clever here. Mypy doesn't understand that the elif clause rules out b being None. (Honestly I had to do a double take and re-read the code too before I trusted this was correct.)

We don't have plans to add such logic to mypy, so I recommend rewriting your code to be a bit simpler.

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