Python Type Hints: Gradual Typing in Real Projects
I was skeptical of type hints at first. After adding them to a 50,000-line codebase, I am not going back. Here is. When type hints were added in Python 3.5, I dismissed them as unnecessary. Python is dynamically typed, and I liked that flexibility. After spending a week tracking down a bug caused by passing a string where an integer was expected, I decided to try type hints in one module. The experience changed my mind. I now add type hints to every new function I write and gradually add them to old code as I touch it. Basic Type Annotations Function annotations specify the types of parameters and return values. They are optional, do not affect runtime behavior, and are checked by static analysis tools like mypy or pyright, not by Python itself. def greet(name: str, times: int = 1) -> str: return f'Hello {name}!' * times def process(items: list[int]) -> dict[str, int]: return {item: item * 2 for item in items} The lowercase list , dict , and tuple for generic types were introduced in Python 3.9. Before that, you needed List , Dict , and Tuple from the typing…