Exhaustiveness Checking with Mypy
hakibenita.com
hakibenita.com
In addition, covariant Union type allows using it with generators (i.e. yield Exception("some error"), and consuming without extra boilerplate. I write more about it here: https://beepb00p.xyz/mypy-error-handling.html#coderef-simple...
I've read the issue, and it seems that it's more about the lack of overloading/dependent types, e.g. their example with pow function. In c++ you'd have two overloads:
float pow(float, float)
int pow(int, int)
Whereas in python, even though you can achieve the equivalent behaviour in runtime, mypy (at least until recently) couldn't express this, so you had to define something like def pow(base: Union[int, float], power: Union[int, float]) -> Union[int, float]
Which might be annoying on call sites when you do expect the return type to be int (but there is no way for mypy to know that with such signature).In my case I do want to return a variant type unconditionally, so this warning doesn't really apply. It's more similar to an Optional type (just instead of None I am using a more meaningful Exception): while it's not great if your code is riddled with optional return types, sometimes you don't have other choice but using it.
from typing import TypeVar
T = TypeVar('T', int, float)
def pow(base: T, power: T) -> T: ...As evidenced by your comment, most people forget about negative exponents. The vast majority of usage is positive exponents; people expect an int back when they do `pow(2, 2)` and returning a Union or float would result in annoying false positives.
https://www.python.org/dev/peps/pep-0622/#exhaustiveness-che...
https://www.python.org/dev/peps/pep-0634/
[0] Trivia: Python is older than Linux!
Cramming in new statements just to check Enums types properly sounds... well wrong... The types don't do anything except make code harder to read. But I know that unpopular opinion here... I guess I just like old simple Python more.
However I also think that how you look at the language greatly depends on what actual projects you have been working on and with whom plus personal taste. I've been mostly in finance and accounting and personally love code that is very easy to "read" (definition of readable code seems to vary per person).
Example: C++ has so many things crammed into it that I don't see the value of it for doing stuff that are not high frequency trading. It is like using a sledgehammer to drive a nail in the wood.
I've spent 25 years discovering languages and concepts in computer science. I've even played with Smalltalk, Ada and some Algol variant. Even wrote COBOL for short while. What's your point?
If the issue is to help you ensure that you check your edge cases properly, and ensure you are acting within specification, then presumably you should be addressing these issues in your tests, not in production code.
Therefore the whole exercise of writing code to catch errors of this kind 'at compile-time vs runtime' is somewhat of a red herring, if not dangerous.
Having said that, I'm also guilty of frequently writing
else: error("In theory this should never happen")What you may be missing is that this code will be guaranteed by MyPy to never actually run in production. In fact what it's effectively saying is "if this code can run, there's a type error", and therefore MyPy forces you to ensure that the code can never run.
https://hakibenita.com/python-mypy-exhaustive-checking#the-f...