Type Annotation Examples
import mkapi
[1] 2020-12-20 11:36:13 (15.6ms) python3 (1.08s)
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-12-20 11:36:13 (15.6ms) python3 (1.10s)
builtin
(
i
, f
, s
, l
, d
, t
, e
)
→ boolFunction with buitin type annotation.
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-12-20 11:36:13 (15.6ms) python3 (1.12s)
builtin_default
(
i=1
, f=1.0
, s='abc'
, t=(1, 2)
)
Function with buitin type annotation and default.
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-12-20 11:36:13 (15.6ms) python3 (1.13s)
basic
(
l
, t
, d
, s
)
Function with basic collection type annotation.
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-12-20 11:36:14 (15.6ms) python3 (1.15s)
function
(
x
)
→ iterator of strFunction that returns an iterator.
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-12-20 11:36:14 (15.6ms) python3 (1.16s)
iterator
(
x
)
→ strIterator 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-12-20 11:36:14 (15.6ms) python3 (1.18s)
optional
(
x
)
Function with optional list.
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-12-20 11:36:14 (15.6ms) python3 (1.19s)
optional_default
(
x=None
)
Function with optional list and default.
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-12-20 11:36:14 (15.6ms) python3 (1.21s)
union
(
x
, y
)
Function with union of builtin.
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-12-20 11:36:14 (15.6ms) python3 (1.22s)
union_collection
(
x
)
Function with union of collection.
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-12-20 11:36:14 (31.3ms) python3 (1.26s)
callable
(
x
, y
, z
)
→ callable(...: int)Callable.
x
(callable) — Without arguments.y
(callable(int: str)) — Builtins.z
(callable(list of str, (float): dict(str: str))) — Usingtyping
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-12-20 11:36:14 (31.3ms) python3 (1.29s)
generator
(
x
, y
, z
)
→ generator(int)Generator.
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-12-20 11:36:14 (31.3ms) python3 (1.32s)
async_generator
(
x
, y
)
AsyncGenerator.
x
(asyncgenerator(int, float)) — Yield type and send type.y
(asyncgenerator(int)) — Yield type.