https://github.com/tombulled/annotate
Annotation System Inspired by Java Annotations
https://github.com/tombulled/annotate
annotate label python
Last synced: about 1 year ago
JSON representation
Annotation System Inspired by Java Annotations
- Host: GitHub
- URL: https://github.com/tombulled/annotate
- Owner: tombulled
- License: mit
- Created: 2021-11-16T21:30:42.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-03-22T18:38:11.000Z (about 2 years ago)
- Last Synced: 2025-03-07T07:36:27.538Z (about 1 year ago)
- Topics: annotate, label, python
- Language: Python
- Homepage:
- Size: 81.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# annotate
Python Annotation System
## About
Annotations are tags that store object metadata (e.g. for functions/classes)
## Installation
### From PyPI
```console
pip install tombulled-annotate
```
### From GitHub
```console
pip install git+https://github.com/tombulled/annotate@main
```
## Usage
### Marker
An annotation with a fixed value
```python
import annotate
@annotate.marker
def deprecated() -> bool:
return True
@deprecated
def foo():
pass
```
```python
>>> annotate.get_annotations(foo)
{'deprecated': True}
```
### Annotation
An annotation with a configurable value
```python
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
```
```python
>>> 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
```python
import annotate
@annotate.annotation(repeatable=True)
def tag(tag: str, /) -> str:
return tag
@tag('awesome')
@tag('cool')
@tag('funky')
def foo():
pass
```
```python
>>> annotate.get_annotations(foo)
{'tag': ['funky', 'cool', 'awesome']}
```
## Inherited Annotation
An annotation that gets added to subclasses of an annotated class
```python
import annotate
@annotate.annotation(inherited=True)
def identifier(identifier: str, /) -> str:
return identifier
@identifier('abc')
class Class:
pass
class Subclass(Class):
pass
```
```python
>>> annotate.get_annotations(Class)
{'identifier': 'abc'}
>>> annotate.get_annotations(Subclass)
{'identifier': 'abc'}
```
## Targetted Annotation
An annotation that targets objects of specific types
```python
import annotate
import types
@annotate.annotation(targets=(types.FunctionType,))
def description(description: str, /) -> str:
return description
@description('A really cool function')
def foo():
pass
```
```python
>>> annotate.get_annotations(foo)
{'description': 'A really cool function'}
```
### Non-Stored Annotation
```python
import annotate
@annotate.annotation(stored=False)
def author(name: str, /) -> None:
pass
@author('Tim')
def foo():
pass
```
```python
>>> annotate.get_annotations(foo)
{}
```