Comment

Comment by karlicoss

I also find type narrowing extremely useful for error handling: you can use Union[Exception, T] as the result type, and ‘pattern match’ with isinstance, which works in runtime and statically checkable by mypy.

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...

parent karlicoss 5 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton