Python Annotation System Inspired by Java Annotations
Project description
annotate
Python Annotation System Inspired by Java Annotations
About
Annotations are tags that store object metadata (e.g. for functions/classes)
Installation
pip install git+https://github.com/tombulled/annotate
Usage
Marker
An annotation with a fixed value
import annotate
@annotate.marker
def deprecated() -> bool:
return True
@deprecated
def foo():
pass
>>> annotate.get_annotations(foo)
{'deprecated': True}
Annotation
An annotation with a configurable value
import annotate
@annotate.annotation
def metadata(*, author: str, version: str) -> dict:
return dict(
author = author,
version = version,
)
@metadata(author='sam', version='1.0.1')
def foo():
pass
>>> annotate.get_annotations(foo)
{'metadata': {'author': 'sam', 'version': '1.0.1'}}
Repeatable Annotation
An annotation that can be used to annotate the same object multiple times
import annotate
@annotate.annotation(repeatable=True)
def tag(tag: str, /) -> str:
return tag
@tag('awesome')
@tag('cool')
@tag('funky')
def foo():
pass
>>> annotate.get_annotations(foo)
{'tag': ['funky', 'cool', 'awesome']}
Inherited Annotation
An annotation that gets added to subclasses of an annotated class
import annotate
@annotate.annotation(inherited=True)
def identifier(identifier: str, /) -> str:
return identifier
@identifier('abc')
class Class:
pass
class Subclass(Class):
pass
>>> annotate.get_annotations(Class)
{'identifier': 'abc'}
>>> annotate.get_annotations(Subclass)
{'identifier': 'abc'}
Targetted Annotation
An annotation that targets objects of specific types
import annotate
import types
@annotate.annotation(targets=(types.FunctionType,))
def description(description: str, /) -> str:
return description
@description('A really cool function')
def foo():
pass
>>> annotate.get_annotations(foo)
{'description': 'A really cool function'}
Non-Stored Annotation
import annotate
@annotate.annotation(stored=False)
def author(name: str, /) -> None:
pass
@author('Tim')
def foo():
pass
>>> annotate.get_annotations(foo)
{}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for tombulled-annotate-0.1.11.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 083af4ec06a87daee464736b53ce390bcc6ac9f9a1e1b7c68cec8e137dfab1a7 |
|
MD5 | 551cd381758874e81d2d78b296d43d3f |
|
BLAKE2b-256 | ce61d92cf8da81d4380b5f9d57ff6c49aa49bead156bed0b22b35e692f0871ff |
Close
Hashes for tombulled_annotate-0.1.11-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bdb9f921a133d71786b1132255ee4118ae2aabece53f3ec56736c204434cb68c |
|
MD5 | fde8bb2f4fddf6fe4b055376f237c4e1 |
|
BLAKE2b-256 | efd214d56c254bedbe530d2ac8b439cbc5691917f4382a854424235ac8b98734 |