Skip to main content

Library of annotations for humans

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyannotating-1.3.0.tar.gz (16.6 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page