DEV Community

Discussion on: Why I stay away from Python type annotations

Collapse
 
arnaudparan profile image
Arnaud Paran

Okay, you should check out prototypes and PEP 544

It is the way to implement duck typing in type annotations, so anything which can be cast to a str could be defined by

class CastableToStr(typing.Protocol):
def str(self) -> str:
pass

Now all the other points are overly personal, and the comparison with rust is funny because rust is one of those languages which proove that a good compiler with static typing etc prevents mistakes.

And the final question why choose python? Well, I never understood why people would chose python, using that language because "python is easier to use" is just downright shooting yourself in the foot. An economy of one month of coaching for your new developpers and ending up with more bugs and issues does not seem like a great choice to me anyways

Collapse
 
danieleniero profile image
daniele-niero

the comparison with rust is funny because rust is one of those languages which proove that a good compiler with static typing etc prevents mistakes.

But that is exactly the point, Rust has a compiler, it is a statically typed language, but python is interpreted and the interpreter doesn't understand these type annotations, they are just hints for other tools, like mypy, but they are not taken into consideration during the program execution.
The annotations may say something and at run time you can pass something else and python will run anyway, or at least it will run the function until it find another problem, it it finds it.
In this sense they are a "false security mechanism".

There are other languages with an easy, expressive syntax similar to Python, but statically typed and compiled. Nim, Swift, Crystal, Julia and more. If there is the need for static types without loosing the simplicity of a language, there are alternatives. I really don't understand why it has to be forced into Python, in a hacky way, making Python look like something that it is not and losing sight of the dynamic nature of the language, which can be, given the right circumstances, a huge benefit.

The right tool for the right job.
Type hinting in Python feels like using a screwdriver as if it was a hammer.