-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathannotated_doc.py
More file actions
45 lines (34 loc) · 1.29 KB
/
annotated_doc.py
File metadata and controls
45 lines (34 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Annotated Doc compatibility module.
Provides the Doc annotation for FastAPI documentation.
Uses typing_extensions.Doc when available (Python 3.9+ with typing_extensions >= 4.9.0),
otherwise provides a simple fallback implementation.
"""
try:
from typing_extensions import Doc
except ImportError:
class Doc:
"""
Documentation annotation for Annotated types.
Used to provide documentation for type annotations in FastAPI endpoints.
Falls back to a simple implementation if typing_extensions is not available.
Example:
from typing import Annotated
from annotated_doc import Doc
def endpoint(
user_id: Annotated[str, Doc("The user's unique identifier")]
):
pass
"""
__slots__ = ('documentation',)
def __init__(self, documentation: str) -> None:
self.documentation = documentation
def __repr__(self) -> str:
return f"Doc({self.documentation!r})"
def __hash__(self) -> int:
return hash(self.documentation)
def __eq__(self, other: object) -> bool:
if isinstance(other, Doc):
return self.documentation == other.documentation
return NotImplemented
__all__ = ['Doc']