[Python-ideas] "Sum" Type hinting [was: Type hinting for path-related functions]
greg.ewing at canterbury.ac.nz
Thu May 19 03:00:10 EDT 2016
Guido van Rossum wrote:
> Sometimes you really do want to distinguish between the box and what's
> in it, and then you can use a bunch of simple classes, or maybe a
> bunch of namedtuples, to represent the different kinds of boxes. If
> you declare the unopened box type as a Union you can get mypy to check
> that you've covered all your bases.
Well, sort of. It will check that you don't pass anything
outside of the set of allowed types, but it can't tell
whether you've handled all the possible cases in the
code. That's something Haskell can do because (1) it has
special constructs for case analysis, and (2) its
algebraic types can't be extended or subclassed.
> The one thing that Python doesn't have (and mypy doesn't add) would be
> a match statement. The design of a Pythonic match statement would be
> an interesting exercise;
Yes, if there were dedicated syntax for it, mypy could
check that you covered all the branches of a Union.
I've thought about this off and on a bit. Maybe I'll
write about some ideas in another post.
More information about the Python-ideas