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
When using if-statements, the type is being properly narrowed depending on the conditions checked. mypy seems unable to do the same type of narrowing in a match statement
Nice finding. match being a relatively new feature, i.e., in 3.10 Python, might be one reason for mypy to fail or not being mature enough to handle this. Also, it can be relatively complex to handle this, although you have come up with a very straightforward example above but I can imagine it should be quite a task to achieve
I encountered a similar bug. Do not know if current PR #17600 solves it.
Below example shows errors from mypy 1.14.0
importtypingvalue: tuple[int, str] |None=1, 'some_str'matchvalue:
caseitem_1, item_2:
typing.assert_type(item_1, int) # error: Expression is of type "int | str", not "int" [assert-type]typing.assert_type(item_2, str) # error: Expression is of type "int | str", not "str" [assert-type]
Bug Report
When using if-statements, the type is being properly narrowed depending on the conditions checked. mypy seems unable to do the same type of narrowing in a match statement
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=eb4da8b4996d740af15df17b231e0527
Here I get an error:
While the following works fine:
https://mypy-play.net/?mypy=latest&python=3.12&gist=ab7f28a259a5bff450413ce2f2924e74
Expected Behavior
Both examples should work fine without any typechecking errors
Actual Behavior
Mypy is not able to narrow the type in the match statement case like it is able to when using if-conditions
Your Environment
The mypy playground links (mypy version 1.11.0, Python version 3.12).
Related:
The text was updated successfully, but these errors were encountered: