Structures your annotations and puts more information into them.
pip install pyannotating
Create annotation templates
from typing import Callable, Any, Optional, Iterable
from pyannotating import *
handler_of = AnnotationTemplate(Callable, [[input_annotation], Any])
handler_of[int | float]Callable[[int | float], Any]in a nested way
optional_reformer_of = AnnotationTemplate(
Callable,
[[input_annotation], AnnotationTemplate(Optional, [input_annotation])]
)
optional_reformer_of[int]Callable[[int], Optional[int]]with non-strict input annotation
summator_of = AnnotationTemplate(Callable, [[input_annotation | int, input_annotation], int])
summator_of[float]Callable[[float | int, float], int]Integrate comments into annotations
even = FormalAnnotation("Formal annotation of even numbers.")
number: even[int | float] = 42or subgroups of existing types
natural_numbers = Subgroup(int, lambda number: number > 0)
isinstance(14, natural_numbers)
isinstance(-1.2, natural_numbers)
64 in natural_numbersTrue
False
Trueor downcasts
def transform(numbers: Special[range, Iterable[int]], additional: Special[None] = None) -> Any:
...
# Equals to
def transform(numbers: Iterable[int], additional: Any = None) -> Any:
...or just some pre-made templates and annotations
many_or_one[int | float]
numberint | float | Iterable[int | float]
int | float | complex