Skip to main content

Library to structure annotations in your code

Project description

Pyannotating

Allows you to structure your annotations and put more information into them.
Templates created with this library should preferably be placed in the annotations.py file.

Installation

pip install pyannotating

Features

You can create a template of your annotations

from pyannotating import *
from typing import Callable, Any, Optional, Iterable

handler_of = AnnotationTemplate(Callable, [[input_annotation], Any])

and then create an annotation by this template

handler_of[int | float]

what is equivalent

Callable[[int | float], Any]

Also you can nest templates inside each other

optional_handler_of = AnnotationTemplate(
    Callable,
    [[input_annotation], AnnotationTemplate(Optional, [input_annotation])]
)

optional_handler_of[int]

what results in

Callable[[int], Optional[int]]

and use input_annotation in conjunction with something else

summator_of = AnnotationTemplate(Callable, [[input_annotation | int, input_annotation], int])
summator_of[float]

to get

Callable[[float | int, float], int]

In addition, you can integrate comments with your annotations

even = FormalAnnotation("Formal annotation of even numbers.")

number: even[int | float] = 42

or annotate downcasts

def transform(collection: Special[range, Iterable]) -> Any:
    ...

or just use some pre-made templates

many_or_one[Callable]
method_of[int]

for getting

Callable | Iterable[Callable]
Callable[[int, ...], Any]

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.2.1.tar.gz (16.2 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