News for hackers. Stuff that matters.
Re:Statically-typed error handling in Python using Mypy by staticassertion on Sunday December 08, 2019 @02:16PM (#21737612)
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.

Re:Statically-typed error handling in Python using Mypy by karlicoss on Sunday December 08, 2019 @02:31PM (#21737700)
* 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

Re:Statically-typed error handling in Python using Mypy by staticassertion on Sunday December 08, 2019 @02:33PM (#21737716)
The '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!

Re:Statically-typed error handling in Python using Mypy by Caligatio on Sunday December 08, 2019 @02:55PM (#21737870)
* Still weak support for JSON (use Dict[str, Any])

Python 3.8 added TypedDict which finally helps this particular shortcoming.

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
Re:Statically-typed error handling in Python using Mypy by ledauphin on Sunday December 08, 2019 @08:26PM (#21739763)
in my experience, TypedDict has its own shortcomings, most of which boil down to the fact that it's not truly structural subtyping, because 'extra' keys are treated as errors.