Whenever I try using mypy in my projects, it’s taking me more to do annotate the typing info than to write the actual code. And yes, I’ve read its documentation beforehand.
I don’t use MyPy but I use 3.7 and the type annotations and PyCharm and I am very happy. I’d assume PyCharm uses MyPy or a derivative in the background to do the static analysis but whatever it is PyCharm has got it right. (PyCharm used to be slow ca 2014, it’s much faster now, but does use a ton of memory.)
One of pages I found great when trying to figure out type annotations is: https://mypy.readthedocs.io/en/latest/cheat_sheet_py3.html
FWIW, I didn’t find MyPy to be worth it at first either. Only after nearly 3 years working on Oil did I find it to be worthwhile.
I had a very good experience with it recently – I was able to statically type the whole OSH front end in about 8 days, with the help of pyannotate, which the MyPy team also developed.
The front end is about 9K lines of hand-written code and 7K lines of generated code. And it’s about half the codebase overall.
The code has to “settle down” before it can be typed, IMO. It doesn’t make sense to start with types, at least for the kinds of projects that Python is good for. I’m now a fan of gradual typing.
I also have several thousand lines of supporting tools written in Python, but I wouldn’t bother typing those. It does make sense for the main codebase though.
I imagine it depends on the kind of code you write, and experience with type annotations in general.
It’s important to know certain patterns to encode business logic in the right way so types can work in your favor
Mypys massive slowness doesn’t help with this but even before this release it’s gotten a lot better at incremental builds
I’ve largely avoided using annotations and third-party static checkers for Python because the original design was at odds with the language.
Annotations + the typing module + checkers like mypy have been fundamentally built around nominal typing, but the way Python is almost always used in the real world is built around something much closer to structural typing. I know people are working on ways to express “I don’t care about the class hierarchy of this object, I care it has these methods”, but that work has started awfully late, and I’m not sure it’s possible, now, to overcome the inertia of typing as originally designed.
Those are pretty legit criticisms. I wish I could just take Typescript (structural typing) and slap it into Python. It actually should be possible to do this but it’s a bit of work
mypy introduced protocols, and you can use those to get pretty far in structural land. In the kind of code I write, I end up with pretty monomorphic code so I don’t need structural typing as much. But if you’re writing more general code then declaring protocols should work well (but is definitely work)
Thanks for taking the time to reply to this
Yeah, every time I use typescript, I can’t help but feel it would be amazing if python had something as polished and cool.
give pytype a try - it’s meant to work with unannotated code and be quick and easy to get started with. (disclaimer - I work on it, and would be happy to help you get set up)