You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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 anif value is not None
expression, the corresponding walrus expression does not workExample of valid mypy code without the walrus operator:
With the walrus operator, the following code should not produce a mypy error:
but it does
Apologies if this is a duplicate
The text was updated successfully, but these errors were encountered: