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 doesnt do type inference in "walrus" expressions (i.e. := ) #8147

Closed
samuelbryant opened this issue Dec 14, 2019 · 2 comments
Closed

Mypy doesnt do type inference in "walrus" expressions (i.e. := ) #8147

samuelbryant opened this issue Dec 14, 2019 · 2 comments

Comments

@samuelbryant
Copy link

I'm running python3.8 and mypy 0.750 and I realized that mypy doesn't do automatic type inference in if expressions involving the walrus operator the way it does with ordinary expressions. Specifically, where mypy would infer that a value is not None after passing an if value is not None expression, the corresponding walrus expression does not work

Example of valid mypy code without the walrus operator:

def method(arg: int):
    pass
int_map: Dict[str, int] = {}    
value = int_map.get(key, None)
if value is not None:
    method(value) # okay

With the walrus operator, the following code should not produce a mypy error:

def method(arg: int):
    pass
int_map: Dict[str, int] = {}    
if (value := int_map.get(key, None)) is not None:
    method(value) # triggers "... has incompatible type Optional[int]"; expected "int"

but it does

Apologies if this is a duplicate

@samuelbryant
Copy link
Author

if someone gives me a hint for where in the source code something like this should be implemented, I'd be happy to try and write the patch myself

@JelleZijlstra
Copy link
Member

This is a duplicate of #7316, so I'll close this one. The fix should be in one of the places I touched in #6899, but I'm not sure exactly where.

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