Python void return type annotation

Tregoreg picture Tregoreg · Apr 22, 2016 · Viewed 37.6k times · Source

In python 3.x, it is common to use return type annotation of a function, such as:

def foo() -> str:
    return "bar"

What is the correct annotation for the "void" type?

I'm considering 3 options:

  1. def foo() -> None:
    • not logical IMO, because None is not a type,
  2. def foo() -> type(None):
    • using the best syntax I know for obtaining NoneType,
  3. def foo():
    • omit explicit return type information.

Option 2. seems the most logical to me, but I've already seen some instances of 1.

Answer

AKS picture AKS · Apr 22, 2016

This is straight from PEP 484 -- Type Hints documentation:

When used in a type hint, the expression None is considered equivalent to type(None).

And, as you can see most of the examples use None as return type.