Skip to content

Commit aa9bcd8

Browse files
committed
Actual examples
1 parent b508f1f commit aa9bcd8

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

README.md

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,83 @@ Structures your annotations and puts more information into them.
44
### Installation
55
`pip install pyannotating`
66

7-
### Features
8-
You can create a template of your annotations
7+
### Examples
8+
Create annotation templates
99
```python
10-
from pyannotating import *
1110
from typing import Callable, Any, Optional, Iterable
1211

12+
from pyannotating import *
13+
14+
1315
handler_of = AnnotationTemplate(Callable, [[input_annotation], Any])
14-
```
15-
and then create an annotation by this template
16-
```python
1716
handler_of[int | float]
1817
```
19-
20-
what is equivalent
2118
```python
2219
Callable[[int | float], Any]
2320
```
2421

25-
Also you can nest templates inside each other
22+
in a nested way
2623
```python
27-
optional_handler_of = AnnotationTemplate(
24+
optional_reformer_of = AnnotationTemplate(
2825
Callable,
2926
[[input_annotation], AnnotationTemplate(Optional, [input_annotation])]
3027
)
3128

32-
optional_handler_of[int]
29+
optional_reformer_of[int]
3330
```
34-
35-
what results in
3631
```python
3732
Callable[[int], Optional[int]]
3833
```
3934

40-
and use input_annotation in conjunction with something else
35+
with non-strict input annotation
4136
```python
4237
summator_of = AnnotationTemplate(Callable, [[input_annotation | int, input_annotation], int])
4338
summator_of[float]
4439
```
45-
46-
to get
4740
```python
4841
Callable[[float | int, float], int]
4942
```
5043

51-
In addition, you can integrate comments with your annotations
44+
Integrate comments into annotations
5245
```python
5346
even = FormalAnnotation("Formal annotation of even numbers.")
5447

5548
number: even[int | float] = 42
5649
```
5750

58-
or annotate downcasts
51+
or subgroups of existing types
5952
```python
60-
def transform(collection: Special[range, Iterable]) -> Any:
61-
...
53+
natural_numbers = Subgroup(int, lambda number: number > 0)
54+
55+
isinstance(14, natural_numbers)
56+
isinstance(-1.2, natural_numbers)
57+
58+
64 in natural_numbers
59+
```
60+
```python
61+
True
62+
False
63+
True
6264
```
6365

64-
or just use some pre-made templates
66+
or downcasts
6567
```python
66-
many_or_one[Callable]
67-
method_of[int]
68+
def transform(numbers: Special[range, Iterable[int]], additional: Special[None] = None) -> Any:
69+
...
70+
71+
# Equals to
72+
73+
def transform(numbers: Iterable[int], additional: Any = None) -> Any:
74+
...
6875
```
6976

70-
for getting
77+
78+
or just some pre-made templates and annotations
79+
```python
80+
many_or_one[int | float]
81+
number
82+
```
7183
```python
72-
Callable | Iterable[Callable]
73-
Callable[[int, ...], Any]
84+
int | float | Iterable[int | float]
85+
int | float | complex
7486
```

0 commit comments

Comments
 (0)