Python Function Annotation

Starting with Python 3.0 (and PEP 3107 — Function Annotations) it is possible to annotate the arguments and return values of functions.

A function with an annotation might look like this.

def split_into_first_and_last_name(name: str) -> list:
    return name.split()

The syntax is for function arguments is argument[: type][="default_value"] where type can be either a Python Data Type or a String describing the argument. There is no meaning in these annotations on default and no type checking is done. You can access them programmatically via split_into_first_and_last_name.__annotations__ or use the help() function for the

Why use Function Annotations?

  • You can use them for Documentation Purposes and Type Hinting. Therfore using function Annotations will help you to write clean code.
  • You can use mypy, Pyright (Microsoft), pyre-check (Facebook) or pytype (Google) to optionaly turn on static type checking (According to PEP 484 — Type Hints). To do so:
    • Install mypy with pip install mypy
    • Try out your type annotated file with mypy namesplitter.py
    • If you are using the function wrong (e.g. split_into_first_and_last_name(["Simon","Klug"])) you get an error message error: Argument 1 to "split_into_first_and_last_name" has incompatible type "List[str]"; expected "str"
  • Get better linting with an integration for your favourite editor or linter
#Code #PEP #Programming #Python #Tips