5 ms·
Here's my opinion, having written many thousands of lines of mypy code. * Third party libraries are still not typed, which sucks * Inference is weak. Sometime
by staticassertion 7y ago
Here's my opinion, having written many thousands of lines of mypy code.
* 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.
- karlicoss 7y ago* Third party libraries: I sometimes see them annotated, but what people don't suspect, is that you need to include 'py.typed' file with your package in order for it to be discoverable. Perhaps that's something setuptools could warn the developer about.. * 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 https://beepb00p.xyz/mypy-error-handling.html#container
- staticassertion 7y agoThe 'py.typed' thing is extremely unintuitive, I package up python libs and I'm still not sure I've done it right. Thank you for letting me know about those flags, I had not heard of them before!
- Caligatio 7y ago* Still weak support for JSON (use Dict[str, Any]) Python 3.8 added TypedDict which finally helps this particular shortcoming.
- staticassertion 7y agoWonderful, 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.
- emptysea 7y agoBtw, TypedDict has been available in older versions of python via `mypy_extensions` from mypy_extensions import TypedDict https://github.com/python/mypy_extensions https://github.com/python/mypy_extensions
- avremel 7y agoI 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 https://github.com/python/mypy/issues/7178). Optional keys are also a nuisance.
- dharmab 7y agoHow 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.
- avremel 7y agoI 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-required-and-non-required-items https://mypy.readthedocs.io/en/latest/more_types.html#mixing...