-
Notifications
You must be signed in to change notification settings - Fork 767
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
Exhaustive match statement treated as inexhaustive #1380
Comments
This is intended behavior, at least currently. It will handle the case where you are employing an "irrefutable" pattern, as in the It is theoretically possible for us to add support for this case, but it would be significant complexity and potentially very costly at analysis time. We implemented a similar feature called "implied else narrowing" for chains of if/elif statements with no "else". It took a long time to get this right, and we needed to apply some significant limitations to make it performant. Since the For now, you can work around this limitation by adding an irrefutable case at the end of your match statement. def test(x: int) -> str:
"""
Function with declared type of "str" must return value
Type "None" cannot be assigned to type "str"
"""
match x:
case int():
return ""
case _:
return "" or def test(x: int) -> str:
"""
Function with declared type of "str" must return value
Type "None" cannot be assigned to type "str"
"""
match x:
case int():
return ""
raise Exception('Should never get here') |
Alright, I'll probably end up making an Exhaustive exception for these cases and using it to indicate exhaustiveness. |
After thinking about this more, I came up with a way to handle this in a way that will will have a minimal performance impact. With this new approach in place, the type checker is now able to detect match exhaustion and handle the case above. This will be in the next release of pylance. |
This issue has been fixed in version 2021.6.1, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202161-9-june-2021 |
Environment data
Expected behaviour
Test function shouldn't have any errors
Actual behaviour
Pylance warns that test can return None despite the match statement being exhaustive
Additionally, on adding an additional case, pylance correctly identifies the new variable as Never, showing that the match statement is exhaustive
Code Snippet / Additional information
The text was updated successfully, but these errors were encountered: