Static type checking provides code-level safety.

  • It analyzes the code without running it (before code execution)
  • It catches developer mistakes ahead of time (during compile time)
  • In Python, it uses Type Hints for Typing

Let’s consider the following User Pydantic model and Python function:

from pydantic import BaseModel
 
class User(BaseModel):
    age: int
def process_user(user: User) -> int:
    return user.ag + 10  # Typo

In the function, we have a typo: user.ag should have been user.age.

  • If we don’t perform static type checking, the Python code will run (because it is dynamically typed) and crash at runtime by throwing an AttributeError.
  • With Static Analysis, we can find this typo bug before executing the code. It helps with catching code mistakes early.

Python

Following are some of the popular static type checking tools for Python.