Re:Statically-typed error handling in Python using Mypy
by Recursing on Sunday December 08, 2019 @05:07PM
(#21738714)
Do you mean something like this?
[ Parent
| on HN ]
Re:Statically-typed error handling in Python using Mypy
by macca321 on Monday December 09, 2019 @07:19AM
(#21741995)
Yes I can't quite remember why I couldn't get it to work. I want to make something where this is valid, but couldn't manage it:
def foo() -> OneOf[Bar, Baz]:
if blah():
return Bar()
return Baz()
def bar_to_blah(bar:Bar) -> Blah :
...
def baz_to_blah(baz:Baz) -> Blah :
...
one_of_bar_or_baz = foo()
blah = one_of_bar_or_baz.match(bar_to_blah, baz_to_blah)
but the critical thing is to get a type error if you add another case to foo's return type, or change the generics to different types.