Bug Report
The type of var is not narrowed in match statements including a walrus assignment, such as
match (var := fun()):
case type():
...
To Reproduce
def get_int_or_float() -> int | float:
return int()
def print_int(i: int) -> None:
print(i)
# mypy works correctly:
first_int_or_float = get_int_or_float()
match first_int_or_float:
case int():
# this use is correct; mypy is correct
print_int(first_int_or_float)
case float():
# this use is incorrect; mypy is correct
print_int(first_int_or_float)
# mypy fails:
match (second_int_or_float := get_int_or_float()):
# mypy incorrectly does not narrow type of second_int_or_float
case int():
print_int(second_int_or_float)
case float():
print_int(second_int_or_float)
https://mypy-play.net/?mypy=master&python=3.10&gist=1a1347df9d076755e9dd6a947c97839a
Expected Behavior
main.py:15: error: Argument 1 to "print_int" has incompatible type "float"; expected "int" [arg-type]
main.py:23: error: Argument 1 to "print_int" has incompatible type "float"; expected "int" [arg-type]
Found 2 errors in 1 file (checked 1 source file)
Actual Behavior
main.py:15: error: Argument 1 to "print_int" has incompatible type "float"; expected "int" [arg-type]
main.py:21: error: Argument 1 to "print_int" has incompatible type "Union[int, float]"; expected "int" [arg-type]
main.py:23: error: Argument 1 to "print_int" has incompatible type "Union[int, float]"; expected "int" [arg-type]
main.py:23: error: Name "second_int_or_float" is used before definition [used-before-def]
Found 4 errors in 1 file (checked 1 source file)
Your Environment
mypy master on Python 3.10, see Playground
Bug Report
The type of
varis not narrowed inmatchstatements including a walrus assignment, such asTo Reproduce
https://mypy-play.net/?mypy=master&python=3.10&gist=1a1347df9d076755e9dd6a947c97839a
Expected Behavior
Actual Behavior
Your Environment
mypy master on Python 3.10, see Playground