DEV Community

Oyekunle Oloyede
Oyekunle Oloyede

Posted on

Annotations and Type Checking in Python

Python is a dynamically typed programming language. This means that type checking is done at run-time. PEP484 introduced type hints, which makes it possible to add type hints to variables and function signatures.

Imagine a function say_hello that takes and returns a string. Without hints, this function would look like this:

def say_hello(name):
    return "Hello " + name

It can be annotated as follows:

def say_hello(name: str) -> str:
    return "Hello " + name

Type Aliases

To define a function itemize_names that takes a list of names and returns a new list with names numbered from 1 to n. An implementation of the function can be found below:

def itemize_names(names):
    return [f"{pos + 1}. {name}" for pos, name in enumerate(names)]

To annotate the function an alias can be created for names:

from typing import List

NameList = List[str]

def itemize_names(names: NameList) -> NameList:
    return [f"{pos + 1}. {name}" for pos, name in enumerate(names)]

The typing module provides support for so much more, such as:

  • Creating a new type
  • Annotating a function that takes a callback
  • Generics

You can find the documentation here to learn how to add type hints to your Python code.

Type Checking

While you can add type annotations to your code, the Python runtime does not enforce type annotations. To demonstrate, the say_hello function defined earlier can be re-written like this without producing any change in functionality.

def say_hello(name: str) -> int:
    return "Hello " + name

You will also not get a warning from Python.

To enforce type checks, there are a number of static type checkers available. I would be using mypy.

Let's install mypy to get those type checks we need. You can install mypy with this command:

$ python3 -m pip install mypy

To type check our say_hello function, let's say it is contained in the say_hello.py file:

$ mypy say_hello.py 

say_hello.py:2: error: Incompatible return value type (got "str", expected "int")
Found 1 error in 1 file (checked 1 source file)

Let's fix the error by changing expected return type to str and type check our file again:

$ mypy say_hello.py

Success: no issues found in 1 source file

The mypy documentation provides all the information required on how to use mypy in your projects.

Thanks for reading.

Top comments (0)