Python Language Type Hints

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Syntax

  • typing.Callable[[int, str], None] -> def func(a: int, b: str) -> None
  • typing.Mapping[str, int] -> {"a": 1, "b": 2, "c": 3}
  • typing.List[int] -> [1, 2, 3]
  • typing.Set[int] -> {1, 2, 3}
  • typing.Optional[int] -> None or int
  • typing.Sequence[int] -> [1, 2, 3] or (1, 2, 3)
  • typing.Any -> Any type
  • typing.Union[int, str] -> 1 or "1"
  • T = typing.TypeVar('T') -> Generic type

Remarks

Type Hinting, as specified in PEP 484, is a formalized solution to statically indicate the type of a value for Python Code. By appearing alongside the typing module, type-hints offer Python users the capability to annotate their code thereby assisting type checkers while, indirectly, documenting their code with more information.



Got any Python Language Question?