typing.List

class typing.List(list, MutableSequence[T]) Generic version of list. Useful for annotating return types. To annotate arguments it is preferred to use abstract collection types such as Mapping, Sequence, or AbstractSet. This type may be used as follows: T = TypeVar('T', int, float) def vec2(x: T, y: T) -> List[T]: return [x, y] def keep_positives(vector: Sequence[T]) -> List[T]: return [item for item in vector if item > 0]

typing.Mapping

class typing.Mapping(Sized, Iterable[KT], Container[KT], Generic[VT_co]) A generic version of collections.abc.Mapping.

typing.Container

class typing.Container(Generic[T_co]) A generic version of collections.abc.Container.

typing.cast()

typing.cast(typ, val) Cast a value to a type. This returns the value unchanged. To the type checker this signals that the return value has the designated type, but at runtime we intentionally don’t check anything (we want this to be as fast as possible).

typing.io

class typing.io Wrapper namespace for I/O stream types. This defines the generic type IO[AnyStr] and aliases TextIO and BinaryIO for respectively IO[str] and IO[bytes]. These representing the types of I/O streams such as returned by open().

typing.Dict

class typing.Dict(dict, MutableMapping[KT, VT]) A generic version of dict. The usage of this type is as follows: def get_position_in_index(word_list: Dict[str, int], word: str) -> int: return word_list[word]

typing.get_type_hints()

typing.get_type_hints(obj) Return type hints for a function or method object. This is often the same as obj.__annotations__, but it handles forward references encoded as string literals, and if necessary adds Optional[t] if a default value equal to None is set.

typing.Generator

class typing.Generator(Iterator[T_co], Generic[T_co, T_contra, V_co]) A generator can be annotated by the generic type Generator[YieldType, SendType, ReturnType]. For example: def echo_round() -> Generator[int, float, str]: sent = yield 0 while sent >= 0: sent = yield round(sent) return 'Done' Note that unlike many other generics in the typing module, the SendType of Generator behaves contravariantly, not covariantly or invariantly. If your generator will only yiel

typing.Generic

class typing.Generic Abstract base class for generic types. A generic type is typically declared by inheriting from an instantiation of this class with one or more type variables. For example, a generic mapping type might be defined as: class Mapping(Generic[KT, VT]): def __getitem__(self, key: KT) -> VT: ... # Etc. This class can then be used as follows: X = TypeVar('X') Y = TypeVar('Y') def lookup_name(mapping: Mapping[X, Y], key: X, default: Y) -> Y: try:

typing.Callable

class typing.Callable Callable type; Callable[[int], str] is a function of (int) -> str. The subscription syntax must always be used with exactly two values: the argument list and the return type. The argument list must be a list of types; the return type must be a single type. There is no syntax to indicate optional or keyword arguments, such function types are rarely used as callback types. Callable[..., ReturnType] could be used to type hint a callable taking any number of arguments an