* Regarding errors: there are few quite recent flags: ' --pretty', '-show-error-context' and '--show-error-codes', they make it a bit more pleasant. You can see me using them in this section: https://beepb00p.xyz/mypy-error-handling.html#container
Thank you for letting me know about those flags, I had not heard of them before!
Python 3.8 added TypedDict which finally helps this particular shortcoming.
Optional keys are also a nuisance.
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.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...
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.
from mypy_extensions import TypedDict
https://github.com/python/mypy_extensions
* Third party libraries are still not typed, which sucks
* Inference is weak. Sometimes an 'if' statement narrows accurately, sometimes it doesn't.
* Generics are extremely confusing, moreso than any other typed language I have used. Any Generics are even more confusing.
* Fundamentally, Python is structured such that the concept of interfaces are coupled tightly to implementation of classes, which is severely limiting. As an example, a module A can declare a class, a module B can declare an interface (Protocol), but module B can not implement that interface for A (without modifying A's code).
* Errors from mypy are still very very light. There are very few "Try XYZ to fix it", and it's just a single line of read text that amounts to an assertion.
* Still weak support for JSON (use Dict[str, Any])
* Slowly improving but still not amazing support for Self types
I could go on.
Mypy is awesome but it still feels very immature, and as it should since it's pre 1.0. It does not come close to matching the experience of other languages with static types.