Example
Type Hints
Learn Type Hints
Type Checking Tools

Python is a dynamically typed language. This makes it easy to write code, but harder to verify that the code is correct.

Programming tools (and programmers!) have difficulty checking that methods are being called with the correct type of values, and that the returned values are used correctly. Mistakes are discovered only when the code is executed, and sometimes not discovered at all – but the results are wrong.

Example

scorecard.py contains 3 syntax or semantic errors. If you open this file in an IDE does it detect all three errors? Probably not. In a statically typed language like Java, any IDE would immediately detect all the errors.

We can find the errors in Python by adding type hints.

Type Hints

Python allows you to write type hints in code to indicate data types. The type hints are optional and ignored by the Python interpretter, but IDEs and tools like mypy use type hints to detect errors.

Hint 1: In scorecard.py, you can add a hint to the add_score method to indicate that the score parameter must be a float (or int) and the method does not return anything:

def add_score(self, score: float) -> None:

After you add this type hint, your IDE may detect that the “main” block is calling add_score incorrectly.

Hint 2: Add a hint to the average(self) method to indicate that it returns a float:

def average(self) -> float:

Your IDE should then detect another error in main.

Hint 3: Add a hint that the scores attribute is a list of float:

from typing import List

    def __init__(self):
        self.scores: List[float] = []

Your IDE should then detect another error in main.

Learn Type Hints

Mai Norapong’s Introduction to Type Hints

Python Type Checking Guide on RealPython

typing package doc page

To understand the type hints for collections and “interface” behavior on classes, you should read the collections.abc doc page. Each of the types in collections.abc has a corresponding type with the same name in the typing package – use typing in your type hints, not collections.abc.

For example, if the Scorecard class is a collection of scores with a length and you can iterate over the scores, then you would write:

from typing import Iterable, Sized

class Scorecard(Iterable[float], Sized):

    def __len__(self):   # method required by Sized type
        return len(self.scores)

    def __iter__(self):  # method required by Iterable type
        return iter(self.scores)

Type Checking Tools

mypy is the most popular type checking tool for Python. It can be used by itself or integrated into some IDE.

  • Getting Started page contains many simple examples you can learn from.
  • For details on how to use specific type hints, see the index (left hand side) on the docs page.

Pycharm uses both type inference and type hints to check type usage. It’s enabled by default but you can customize it, just like everything in Pycharm.

Pydev uses type hints to warn of type misuse.

The VS Code Pyright and Pylance extensions perform static type checking for VS Code.
Pyright checks usage across all files in a project, which should provide better type checking than mypy. So, you shouldn’t need mypy with VS Code. Pylance is a proprietary, closed source extension (Pyright is open source).

Pylint

Pylint checks for many kinds of errors in code, including semantic and syntax errors, and coding style violations. Pylint checks if “interfaces” are truly implemented. So if you write class Foo(Sized) it will check that the class contains a __len__(self) method.

Pylint give your code a score of 0 - 10, that adds some incentive and fun to correcting problems in code.

Pylint partially overlaps with mypy, but mypy does more type checking.