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
Inspired by the match statement in modern languages like Rust, Elm, Haskell etc., I would like the if-else blocks in Python to be stricter and make us match all the possibilities either explicitly or by enforcing the else block or by enforcing the usage of > / < to cover all the possibilities.
Pitch
I have attempted to gain this strictness to some degree (only for enums) by creating this library. However, hacks like these aren't static and requires us to run the program to identify the missing possibilities.
Here's my proposal:
For values with finite possibilities (e.g. bool, Enum etc.), the else block isn't required, but it can be used as a wildcard matcher.
For values with infinite possibilities (e.g. str, int etc.), must either use the else block or use > / < to cover all the possibilities.
e.g.
x=10# Validifx==10:
...
else:
...
# Also validifx==10:
...
elifx<10:
...
elifx>10:
...
# Also validifxinrange(10, 20):
...
elifx<10:
...
elifx>=20:
...
# Invalid (Missing possibilities where x != 10 and x != 11)ifx==10:
...
elifx==11:
...
There will be more edge-cases that we'll need to cover, however, I think for this feature, having something is better than nothing. So this is my general proposal as the starting point.
The text was updated successfully, but these errors were encountered:
I'm closing this as a duplicate of #5818 and #6366
It should be possible to get the assert_never trick described in those issues to make your enum examples work. mypy's not going to change to enforce that all if statements are exhaustive by default, since there are many good reasons to have non-exhaustive if statements. The range / dependent type stuff you propose isn't covered by those issues, but it would probably be a lot of work to design and implement.
Finally, Python might soon have a match statement (see PEP 636, 634, 635)
Feature
Inspired by the
match
statement in modern languages like Rust, Elm, Haskell etc., I would like the if-else blocks in Python to be stricter and make us match all the possibilities either explicitly or by enforcing the else block or by enforcing the usage of>
/<
to cover all the possibilities.Pitch
I have attempted to gain this strictness to some degree (only for enums) by creating this library. However, hacks like these aren't static and requires us to run the program to identify the missing possibilities.
Here's my proposal:
bool
,Enum
etc.), theelse
block isn't required, but it can be used as a wildcard matcher.e.g.
str
,int
etc.), must either use the else block or use>
/<
to cover all the possibilities.e.g.
There will be more edge-cases that we'll need to cover, however, I think for this feature, having something is better than nothing. So this is my general proposal as the starting point.
The text was updated successfully, but these errors were encountered: