5 reasons to love Python type hints
I’m a big fan of type hints, and I honestly don’t understand why some people find it ugly or unpythonic.
- They improve the auto-complete and code navigation in the IDE.
- They protect you against trivial errors and remind you about corner cases.
- Beyond IDEs, more and more tools in the Python ecosystem take advantage of typing information. The FastAPI framework is the first example that comes to my mind.
- They are like documentation, that is testable, both human- and machine-readable, and never outdated.
- They make you think about your code and highlight code smell. Do you need to return Optional, or it’s better to implement a null object pattern? Do you need to return a Union or split your function in two?
Overall, they make explicit many implicit assumptions and connections in your code. Maybe people who complain that type hints are ugly fail to admit that type hints just uncover deficiencies of their own system?