๐ Edit page
โ Add page
Kinds of types
See Kinds of types in the docs.
Based on Type hints cheat sheet (Python 3).
Note from Python 3.9, you can use dict
and list
instead of Dict
and List
, avoiding imports.
Built-in types
x: int = 1
x: float = 1.0
x: bool = True
x: str = "test"
x: bytes = b"test"
x: List[int] = [1]
x: Set[int] = {6, 7}
x = [1] # type: List[int]
A dictionary.
x: Dict[str, float] = {
'field': 2.0
}
See also the Typed Dictionary section.
Fixed size tuple
. More details in Tuple section below.
x: Tuple[int, str, float] = (3, "yes", 7.5)
For a variable size tuple
- use ellipsis.
x: Tuple[int, ...] = (1, 2, 3)
Allow a value to be None
. More details in Optional section below.
x: Optional[str] = some_function()
Mypy understands a value canโt be None
in used an if-statement.
if x is not None:
print(x.upper())
If a value can never be None
due to some invariants, use an assert
.
assert x is not None
print(x.upper())