Re:Statically-typed error handling in Python using Mypy
by avremel on Sunday December 08, 2019 @05:07PM
(#21738711)
I was surprised to find out that MyPy will throw an error if you use a variable to access a key (https://github.com/python/mypy/issues/7178).
Optional keys are also a nuisance.
Re:Statically-typed error handling in Python using Mypy
by dharmab on Sunday December 08, 2019 @07:16PM
(#21739467)
How are optional keys a nuisance? I find that using the patterns:
if "optional_key" in my_dict:
do_something_with(my_dict["optional_key"])
Or: optional_value = my_dict.get("optional_key")
if optional_value:
do_something_with(optional value)
Works as expected. Nested optional keys can be a bit annoying, although: my_dict.get("optional_parent", {}).get("optional_child")
seems to work.
Re:Statically-typed error handling in Python using Mypy
by avremel on Sunday December 08, 2019 @07:22PM
(#21739494)
I was referring to defining optional keys.
If you have a dict with some keys which are optional, you need to create a separate subclass (with `total=False`) just for those optional keys.
With TypeScript, I can just use `key?: type`.
https://mypy.readthedocs.io/en/latest/more_types.html#mixing...
Re:Statically-typed error handling in Python using Mypy
by staticassertion on Sunday December 08, 2019 @03:25PM
(#21738053)
Wonderful, that's great to hear, and I can move to 3.8 quite easily myself.
It's nice to see strong progress in such important areas. My post is probably overly critical, but I write Python almost every day, and maintaining a highly-typed library can be demoralizing at times.
Re:Statically-typed error handling in Python using Mypy
by emptysea on Sunday December 08, 2019 @03:34PM
(#21738107)
Btw, TypedDict has been available in older versions of python via `mypy_extensions`
from mypy_extensions import TypedDict
https://github.com/python/mypy_extensions
Python 3.8 added TypedDict which finally helps this particular shortcoming.