pyannotating

Library of annotations for humans


Keywords
library, annotations, generation, templating, informing, code-readability, annotation
License
GPL-3.0
Install
pip install pyannotating==1.2.1

Documentation

Pyannotating

Structures your annotations and puts more information into them.

Installation

pip install pyannotating

Examples

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] = 42

or subgroups of existing types

natural_numbers = Subgroup(int, lambda number: number > 0)

isinstance(14, natural_numbers)
isinstance(-1.2, natural_numbers)

64 in natural_numbers
True
False
True

or 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]
number
int | float | Iterable[int | float]
int | float | complex