Type Annotation Examples

import mkapi

[1] 2020-08-03 20:47:45 (3.00ms) python3 (384ms)

Builtin Types

def builtin(i: int, f: float, s: str, l: list, d: dict, t: tuple, e: set) -> bool:
    """Function with buitin type annotation.

    Args:
        i: Integer.
        f: Float.
        s: String.
        l: List.
        d: Dictionary.
        t: Tuple.
        e: Set.
    """
    return True

mkapi.display(builtin)

[2] 2020-08-03 20:47:45 (9.00ms) python3 (393ms)

function
builtin(i, f, s, l, d, t, e) → bool

Function with buitin type annotation.

Parameters
  • i (int) Integer.
  • f (float) Float.
  • s (str) String.
  • l (list) List.
  • d (dict) Dictionary.
  • t (tuple) Tuple.
  • e (set) Set.

Builtin Types with Default

def builtin_default(i: int = 1, f: float = 1.0, s: str = "abc", t: tuple = (1, 2)):
    """Function with buitin type annotation and default.

    Args:
        i: Integer. Default={default}.
        f: Float. Default={default}.
        s: String. Default={default}.
        t: Tuple. Default={default}.
    """

mkapi.display(builtin_default)

[3] 2020-08-03 20:47:45 (8.00ms) python3 (401ms)

function
builtin_default(i=1, f=1.0, s='abc', t=(1, 2))

Function with buitin type annotation and default.

Parameters
  • i (int, optional) Integer. Default=1.
  • f (float, optional) Float. Default=1.0.
  • s (str, optional) String. Default='abc'.
  • t (tuple, optional) Tuple. Default=(1, 2).

Basic Collection Types

from typing import Dict, List, Set, Tuple

def basic(l: List[int], t: Tuple[str, int, float], d: Dict[str, int], s: Set[int]):
    """Function with basic collection type annotation.

    Args:
        l: List of integer.
        t: Tuple of (string, string).
        d: Dictionary from string to integer.
        s: Set of integer.
    """

mkapi.display(basic)

[4] 2020-08-03 20:47:45 (10.0ms) python3 (411ms)

function
basic(l, t, d, s)

Function with basic collection type annotation.

Parameters
  • l (list of int) List of integer.
  • t (str, int, float) Tuple of (string, string).
  • d (dict(str: int)) Dictionary from string to integer.
  • s (set of int) Set of integer.

Iterator and Iterable

from typing import Iterator, Iterable

def function(x: Iterable[str]) -> Iterator[str]:
    """Function that returns an iterator.

    Args:
        x: Iterable of string
    """
    return iter(x)

mkapi.display(function)

[5] 2020-08-03 20:47:45 (7.00ms) python3 (418ms)

function
function(x) → iterator of str

Function that returns an iterator.

Parameters
  • x (iterable of str) Iterable of string
def iterator(x: Iterable[str]) -> Iterator[str]:
    """Iterator that yields string."""
    yield from x

mkapi.display(iterator)

[6] 2020-08-03 20:47:45 (7.00ms) python3 (425ms)

generator
iterator(x) → str

Iterator that yields string.

Union and Optional

from typing import Optional, Union

def optional(x: Optional[List[int]]):
    """Function with optional list.

    Args:
        x: List of integer or None.
    """

mkapi.display(optional)

[7] 2020-08-03 20:47:45 (7.00ms) python3 (432ms)

function
optional(x)

Function with optional list.

Parameters
  • x (list of int, optional) List of integer or None.
def optional_default(x: Optional[List[int]] = None):
    """Function with optional list and default.

    Args:
        x: List of integer or None
    """

mkapi.display(optional_default)

[8] 2020-08-03 20:47:45 (8.00ms) python3 (440ms)

function
optional_default(x=None)

Function with optional list and default.

Parameters
  • x (list of int, optional) List of integer or None
def union(x: Union[int, float], y: Union[int, str, dict]):
    """Function with union of builtin.

    Args:
        x: Integer or float.
        y: Integer, string, or dictionary.
    """

mkapi.display(union)

[9] 2020-08-03 20:47:45 (8.00ms) python3 (448ms)

function
union(x, y)

Function with union of builtin.

Parameters
  • x (int or float) Integer or float.
  • y (int, str, or dict) Integer, string, or dictionary.
def union_collection(x: Union[List[int], Tuple[str, str]]):
    """Function with union of collection.

    Args:
        x: List of integer or tuple of (string, string).
    """

mkapi.display(union_collection)

[10] 2020-08-03 20:47:45 (8.00ms) python3 (456ms)

function
union_collection(x)

Function with union of collection.

Parameters
  • x (Union(list of int, (str, str))) List of integer or tuple of (string, string).

Callable

from typing import Callable

def callable(
    x: Callable,
    y: Callable[[int], str],
    z: Callable[[List[str], Tuple[float]], Dict[str, str]],
) -> Callable[..., int]:
    """Callable.

    Args:
        x: Without arguments.
        y: Builtins.
        z: Using `typing` module.


    """

mkapi.display(callable)

[11] 2020-08-03 20:47:45 (11.0ms) python3 (467ms)

function
callable(x, y, z) → callable(...: int)

Callable.

Parameters
  • x (callable) Without arguments.
  • y (callable(int: str)) Builtins.
  • z (callable(list of str, (float): dict(str: str))) Using typing module.

Generator

from typing import Generator, AsyncGenerator

def generator(
    x: Generator[int, float, str],
    y: Generator[int, float, None],
    z: Generator[int, None, List[str]],
) -> Generator[int, None, None]:
    """Generator.

    Args:
        x: Yield type, send type, and return type.
        y: Yield type and send type.
        z: Yield type and return type.
    """

mkapi.display(generator)

[12] 2020-08-03 20:47:45 (9.00ms) python3 (476ms)

function
generator(x, y, z) → generator(int)

Generator.

Parameters
  • x (generator(int, float, str)) Yield type, send type, and return type.
  • y (generator(int, float)) Yield type and send type.
  • z (generator(int,, list of str)) Yield type and return type.
def async_generator(x: AsyncGenerator[int, float], y: AsyncGenerator[int, None]):
    """AsyncGenerator.

    Args:
        x: Yield type and send type.
        y: Yield type.
    """

mkapi.display(async_generator)

[13] 2020-08-03 20:47:45 (8.00ms) python3 (484ms)

function
async_generator(x, y)

AsyncGenerator.

Parameters
  • x (asyncgenerator(int, float)) Yield type and send type.
  • y (asyncgenerator(int)) Yield type.