|
1 | 1 | ## Pyannotating |
2 | | -Allows you to create similar annotations without copying them all the time.<br> |
3 | | -It is advisable to place annotations created using this library in annotations.py file. |
| 2 | +Allows you to structure your annotations and put more information into them.<br> |
| 3 | +Templates created with this library should preferably be placed in the annotations.py file. |
4 | 4 |
|
5 | 5 | ### Installation |
6 | 6 | `pip install pyannotating` |
7 | 7 |
|
8 | | -### Examples |
9 | | -Creating a factory of your annotations |
| 8 | +### Features |
| 9 | +You can create a template of your annotations |
10 | 10 | ```python |
11 | | -from pyannotating import CustomAnnotationFactory, input_annotation |
12 | | -from typing import Callable |
| 11 | +from pyannotating import * |
| 12 | +from typing import Callable, Any, Optional, Iterable |
13 | 13 |
|
14 | | -handler_of = CustomAnnotationFactory(Callable, [[input_annotation], any]) |
| 14 | +handler_of = AnnotationTemplate(Callable, [[input_annotation], Any]) |
15 | 15 | ``` |
16 | | -Now you can create an annotation by this factory |
| 16 | +and then create an annotation by this template |
17 | 17 | ```python |
18 | 18 | handler_of[int | float] |
19 | 19 | ``` |
20 | 20 |
|
21 | | -What is equivalent |
| 21 | +what is equivalent |
22 | 22 | ```python |
23 | | -Callable[[int | float], any] |
| 23 | +Callable[[int | float], Any] |
24 | 24 | ``` |
25 | 25 |
|
26 | | -Also you can use Union with input_annotation |
| 26 | +Also you can nest templates inside each other |
27 | 27 | ```python |
28 | | -summator_of = CustomAnnotationFactory(Callable, [[input_annotation | int, input_annotation], int]) |
29 | | -summator_of[SomeCustomNumber] |
| 28 | +optional_handler_of = AnnotationTemplate( |
| 29 | + Callable, |
| 30 | + [[input_annotation], AnnotationTemplate(Optional, [input_annotation])] |
| 31 | +) |
| 32 | + |
| 33 | +optional_handler_of[int] |
| 34 | +``` |
| 35 | + |
| 36 | +what results in |
| 37 | +```python |
| 38 | +Callable[[int], Optional[int]] |
| 39 | +``` |
| 40 | + |
| 41 | +and use input_annotation in conjunction with something else |
| 42 | +```python |
| 43 | +summator_of = AnnotationTemplate(Callable, [[input_annotation | int, input_annotation], int]) |
| 44 | +summator_of[float] |
30 | 45 | ``` |
31 | 46 |
|
32 | | -What results in |
| 47 | +to get |
33 | 48 | ```python |
34 | | -Callable[[SomeCustomNumber | int, SomeCustomNumber], int] |
| 49 | +Callable[[float | int, float], int] |
35 | 50 | ``` |
36 | 51 |
|
37 | | -In addition, you can also annotate something regardless of its type |
| 52 | +In addition, you can integrate comments with your annotations |
38 | 53 | ```python |
39 | 54 | even = FormalAnnotation("Formal annotation of even numbers.") |
40 | 55 |
|
41 | 56 | number: even[int | float] = 42 |
42 | 57 | ``` |
43 | 58 |
|
44 | | -Full example |
| 59 | +or annotate downcasts |
45 | 60 | ```python |
46 | | -def some_operation_by( |
47 | | - handler: handler_of[int | float], |
48 | | - number: even[float], |
49 | | - *middleware_handlers: summator_of[SomeCustomNumber] |
50 | | -) -> handler_of[int | float]: |
| 61 | +def transform(collection: Special[range, Iterable]) -> Any: |
51 | 62 | ... |
52 | 63 | ``` |
| 64 | + |
| 65 | +or just use some pre-made templates |
| 66 | +```python |
| 67 | +many_or_one[Callable] |
| 68 | +method_of[int] |
| 69 | +``` |
| 70 | + |
| 71 | +for getting |
| 72 | +```python |
| 73 | +Callable | Iterable[Callable] |
| 74 | +Callable[[int, ...], Any] |
| 75 | +``` |
0 commit comments