DEV Community

Cover image for Why I use a static type checker for Python code
icncsx
icncsx

Posted on

Why I use a static type checker for Python code

Speedy Comprehension

When you're working with code bases in the tens of thousands of lines, speedy comprehension is often necessary. Two things really boost comprehension: input type and output type. What am I getting, and what do I need to produce. Not really obvious when you're working in a dynamically typed language such as Python.

Proven Documentation

Sure type annotations and doc strings exist and they can work, but they are not enforced nor are they verified. When you introduce a static type checker such as mypy, what you get is proven documentation. You know what you're getting and what you need to produce - input and output - isn't that the focal point of most programs!

So what does that look like?

Something like this:

def greeting(name: str) -> str:
    return 'Hello ' + name
Enter fullscreen mode Exit fullscreen mode

This is a trivial use case because the code is only two lines long. But imagine you are scanning through hundreds if not thousands of lines. Trust me, types help.

Top comments (0)