#python #programming #doc #typing #datamodel
- Types are defined by supported operations
-
It is a documentation which can be checked by
type checkers
likeMyPy
- There is no impact on the execution of the code
-
How is:
var_name: some_type
(PEP 484) - It is not possible to make hints ever on the code, sometimes is quite impossible
- PEP 484
- Duck Typing vs Nominal Typing
-
Some options that can be important for
mypy
tools- disallow-untyped-defs: not allow any function without type hint
- disallow-incomplete-defs: not allow any function with incomplete type hint
-
mypy
can have global configuration or havemypy.ini
for each module -
Give no space between
:
and variable and one space between the:
and type -
=
space on both sides -
You can use
default
valueNone
with two syntax andOptional
-
def show_count(count: int, singular: str, plural: Optional[str] = None)
-
def show_count(count: int, singular: str, plural: str | None = None)
-
-
Any
is a magic type which allow any operations and subclasses, therefore accept anything -
Simply type annotation:
str, float, int, bytes or classes
-
Union[A, B, Union[C, D, E]] = Union[A, B, C, D, E]
- Generic Collections example:
-
NoReturn
usually used for function that raises exception
def tokenize(text: str) -> list[str]:
return text.upper().split()
- Code above return a list of string
- Typing Tuple
- Typing Maps
-
Sequence
orIterable
for parameters andlist
for return type - Typing Callable
- Parameterized Generics and TypeVar
- Annotating Positional Only and Variadic Parameters
Warning
Avoid Optional
and Union
on return types, because the user need to test which type of variable was returned by the function
Collection Type Parameters
Use one of them int
, float
, complex
Use Union[float, Decimal, Fraction]
Use Iterable
instead of Sequence
Use Iterator
for return type on generator
from collections.abc import Iterable
FromTo = tuple[str, str]
def zip_replace(text: str, changes: Iterable[FromTo]) -> str:
for from_, to in changes:
text = text.replace(from_, to)
return text
References
- Ramalho, 2022, p172-179
- Ramalho, 2022, p253-260
- Ramalho, 2022, p260-265
- Ramalho, 2022, p294