It’s interesting to see some devs in the community finding that type annotations in Python improves code readability. For me it’s the other way around. I find annotated code much harder to read and unless annotations are used for a specific goal (mypy etc) I find that carefully named classes, methods, functions, arguments and variable are the best way to self-document Python code. So for me, if there’s a need for speed that pushed devs to add type annotations, maybe that’s when they need to switch to a language like Rust. If not, ´regular’ plain Python is just fine imho. #opinion
Meanwhile our 100+ million line Python code base with a significant portion of things statically analyzed by pytype and most recent code authors including annotations by default continues to prevent bugs and make it easier to maintain the code. Data from practical experience disagrees with your opinion.
Would you like an editor feature that hides type annotations if you want it to? I think that could be an interesting feature.
Type annotations are great for things like data classes or models, but I'm not convinced that making Python look like C, TypeScript, Rust or Go is good approach. You get all the added syntax, but without the added performance benefits. For strongly typed Python, it's probably better to write Cython :-)
It pleases the ears (or should say the eyes since it's a post) to hear that, I have seen some context where people enforce type annotations with the argument of "quality", but the truth is, other problems were hidden , like misnaming objects, complicated spaghetti code, not using POO when it was necessary (by the way, classes well named might serve a type like purpose) etc.. etc ..
I have the opposite view. Even in OCaml (which is fully statically typed, but also has full type inference, so doesn't require type annotations), I found that adding type annotations was a considerable boon to documentation. In Python, I find that it decreases ambiguity and makes the invariants more explicit. Of course, I'm also of the opinion that mypy should be much more opinionated and stricter, which I suspect makes me part of another minority :)
I have to say I miss the principled days of yore when python cared about things like, "There should be one, and preferably only one, obvious way to do it", or "Simple is better than complex", or "Complex is better than complicated", or "Readability counts", or etc. The language design is drifting.
I'm part of this choir, too. For me, Python type annotations obfuscate the code with noise. If I wanted the benefits of static typing, I would choose a language that has it properly built in. I suppose that cements my reputation for heresy.
I find it nice to make schemas. But I agree, it makes it less readable. I'm not convinced the mypy benefit outweighs the extra work.
The rubyists at Shopify have similar opinions: the code should explain everything. Comments are all but forbidden. Having been immersed in the typescript world myself, I prefer to understand the whole picture through types. I want to see the shape of everything. Pressing f12 in VSCode answers any question I have about how anything works and I love it. Large systems are complex and messy.
Author, Developer
1yRelated : https://dev.to/etenil/why-i-stay-away-from-python-type-annotations-2041