The wonder of Static types in Python

Having a background in Java, I more than often found myself struck by nastiness of running into unspotted TypeErrors in runtime.

But – I find myself loving Python regardless of the dynamic typing – though, it would be useful to check the typing somehow. Then, I read about Mypy, a neat static type checker that have saved me quite some frustration!

Mypy is an experimental optional static type checker for Python that aims to combine the benefits of dynamic (or “duck”) typing and static typing. Mypy combines the expressive power and convenience of Python with a powerful type system and compile-time type checking. Mypy type checks standard Python programs; run them using any Python VM with basically no runtime overhead.

Mypy example

def sum_list(numbers: List[int]) -> str:
return sum(numbers)


Gives (as expected) error: Incompatible return value type (got "int", expected "str")

Sweet! Easy as that. Check out the Mypy syntax cheat sheet. Hack away!


Leave a Reply

Your email address will not be published. Required fields are marked *