|
11 | 11 | get_origin, |
12 | 12 | ) |
13 | 13 |
|
14 | | -from .types import FunctionSchema |
15 | | -from .utils import is_py310_atleast |
| 14 | +from .types import FunctionSchema, Doc |
| 15 | +from .utils import unwrap_doc |
16 | 16 |
|
17 | | -if is_py310_atleast(): |
18 | | - from types import UnionType |
19 | | -else: |
20 | | - UnionType = Union # type: ignore |
21 | 17 |
|
22 | 18 | try: |
23 | | - from typing import Doc |
| 19 | + from types import UnionType |
24 | 20 | except ImportError: |
25 | | - try: |
26 | | - from typing_extensions import Doc |
27 | | - except ImportError: |
28 | | - |
29 | | - class Doc: |
30 | | - def __init__(self, documentation: str, /): |
31 | | - self.documentation = documentation |
| 21 | + UnionType = Union # type: ignore |
32 | 22 |
|
33 | 23 |
|
34 | 24 | __all__ = ("get_function_schema", "guess_type", "Doc", "Annotated") |
35 | 25 |
|
36 | 26 |
|
37 | | -def is_doc_meta( |
38 | | - obj: Annotated[Any, Doc("The object to be checked.")], |
39 | | -) -> Annotated[ |
40 | | - bool, Doc("True if the object is a documentation object, False otherwise.") |
41 | | -]: |
42 | | - """ |
43 | | - Check if the given object is a documentation object. |
44 | | -
|
45 | | - Example: |
46 | | - >>> is_doc_meta(Doc("This is a documentation object")) |
47 | | - True |
48 | | - """ |
49 | | - return getattr(obj, "__class__") == Doc and hasattr(obj, "documentation") |
50 | | - |
51 | | - |
52 | | -def unwrap_doc( |
53 | | - obj: Annotated[ |
54 | | - Union[Doc, str], Doc("The object to get the documentation string from.") |
55 | | - ], |
56 | | -) -> Annotated[str, Doc("The documentation string.")]: |
57 | | - """ |
58 | | - Get the documentation string from the given object. |
59 | | -
|
60 | | - Example: |
61 | | - >>> unwrap_doc(Doc("This is a documentation object")) |
62 | | - 'This is a documentation object' |
63 | | - >>> unwrap_doc("This is a documentation string") |
64 | | - 'This is a documentation string' |
65 | | - """ |
66 | | - if is_doc_meta(obj): |
67 | | - return obj.documentation |
68 | | - return str(obj) |
69 | | - |
70 | | - |
71 | 27 | def get_function_schema( |
72 | 28 | func: Annotated[Callable, Doc("The function to get the schema for")], |
73 | 29 | format: Annotated[ |
@@ -175,7 +131,8 @@ def get_function_schema( |
175 | 131 | } |
176 | 132 |
|
177 | 133 | if enum_ is not None: |
178 | | - schema["properties"][name]["enum"] = [t for t in enum_ if t is not None] |
| 134 | + schema["properties"][name]["enum"] = [ |
| 135 | + t for t in enum_ if t is not None] |
179 | 136 |
|
180 | 137 | if default_value is not inspect._empty: |
181 | 138 | schema["properties"][name]["default"] = default_value |
@@ -205,7 +162,8 @@ def get_function_schema( |
205 | 162 | def guess_type( |
206 | 163 | T: Annotated[type, Doc("The type to guess the JSON schema type for")], |
207 | 164 | ) -> Annotated[ |
208 | | - Union[str, list[str]], Doc("str | list of str that representing JSON schema type") |
| 165 | + Union[str, list[str]], Doc( |
| 166 | + "str | list of str that representing JSON schema type") |
209 | 167 | ]: |
210 | 168 | """Guesses the JSON schema type for the given python type.""" |
211 | 169 |
|
|
0 commit comments