c63a5cfe-b235-4fbe-8bbb-82a9e02a482a-python 0.1.0a3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. c63a5cfe_b235_4fbe_8bbb_82a9e02a482a_python-0.1.0a3.dist-info/METADATA +396 -0
  2. c63a5cfe_b235_4fbe_8bbb_82a9e02a482a_python-0.1.0a3.dist-info/RECORD +190 -0
  3. c63a5cfe_b235_4fbe_8bbb_82a9e02a482a_python-0.1.0a3.dist-info/WHEEL +4 -0
  4. c63a5cfe_b235_4fbe_8bbb_82a9e02a482a_python-0.1.0a3.dist-info/licenses/LICENSE +201 -0
  5. digitalocean_genai_sdk/__init__.py +99 -0
  6. digitalocean_genai_sdk/_base_client.py +1949 -0
  7. digitalocean_genai_sdk/_client.py +795 -0
  8. digitalocean_genai_sdk/_compat.py +219 -0
  9. digitalocean_genai_sdk/_constants.py +14 -0
  10. digitalocean_genai_sdk/_exceptions.py +108 -0
  11. digitalocean_genai_sdk/_files.py +123 -0
  12. digitalocean_genai_sdk/_models.py +805 -0
  13. digitalocean_genai_sdk/_qs.py +150 -0
  14. digitalocean_genai_sdk/_resource.py +43 -0
  15. digitalocean_genai_sdk/_response.py +832 -0
  16. digitalocean_genai_sdk/_streaming.py +333 -0
  17. digitalocean_genai_sdk/_types.py +219 -0
  18. digitalocean_genai_sdk/_utils/__init__.py +57 -0
  19. digitalocean_genai_sdk/_utils/_logs.py +25 -0
  20. digitalocean_genai_sdk/_utils/_proxy.py +65 -0
  21. digitalocean_genai_sdk/_utils/_reflection.py +42 -0
  22. digitalocean_genai_sdk/_utils/_resources_proxy.py +24 -0
  23. digitalocean_genai_sdk/_utils/_streams.py +12 -0
  24. digitalocean_genai_sdk/_utils/_sync.py +86 -0
  25. digitalocean_genai_sdk/_utils/_transform.py +447 -0
  26. digitalocean_genai_sdk/_utils/_typing.py +151 -0
  27. digitalocean_genai_sdk/_utils/_utils.py +422 -0
  28. digitalocean_genai_sdk/_version.py +4 -0
  29. digitalocean_genai_sdk/lib/.keep +4 -0
  30. digitalocean_genai_sdk/py.typed +0 -0
  31. digitalocean_genai_sdk/resources/__init__.py +145 -0
  32. digitalocean_genai_sdk/resources/agents/__init__.py +89 -0
  33. digitalocean_genai_sdk/resources/agents/agents.py +965 -0
  34. digitalocean_genai_sdk/resources/agents/api_keys.py +581 -0
  35. digitalocean_genai_sdk/resources/agents/child_agents.py +508 -0
  36. digitalocean_genai_sdk/resources/agents/functions.py +421 -0
  37. digitalocean_genai_sdk/resources/agents/knowledge_bases.py +346 -0
  38. digitalocean_genai_sdk/resources/agents/versions.py +298 -0
  39. digitalocean_genai_sdk/resources/api_keys/__init__.py +19 -0
  40. digitalocean_genai_sdk/resources/api_keys/api_keys.py +275 -0
  41. digitalocean_genai_sdk/resources/api_keys/api_keys_.py +529 -0
  42. digitalocean_genai_sdk/resources/auth/__init__.py +33 -0
  43. digitalocean_genai_sdk/resources/auth/agents/__init__.py +33 -0
  44. digitalocean_genai_sdk/resources/auth/agents/agents.py +102 -0
  45. digitalocean_genai_sdk/resources/auth/agents/token.py +173 -0
  46. digitalocean_genai_sdk/resources/auth/auth.py +102 -0
  47. digitalocean_genai_sdk/resources/chat.py +381 -0
  48. digitalocean_genai_sdk/resources/embeddings.py +201 -0
  49. digitalocean_genai_sdk/resources/indexing_jobs.py +543 -0
  50. digitalocean_genai_sdk/resources/knowledge_bases/__init__.py +33 -0
  51. digitalocean_genai_sdk/resources/knowledge_bases/data_sources.py +410 -0
  52. digitalocean_genai_sdk/resources/knowledge_bases/knowledge_bases.py +667 -0
  53. digitalocean_genai_sdk/resources/models.py +222 -0
  54. digitalocean_genai_sdk/resources/providers/__init__.py +47 -0
  55. digitalocean_genai_sdk/resources/providers/anthropic/__init__.py +33 -0
  56. digitalocean_genai_sdk/resources/providers/anthropic/anthropic.py +102 -0
  57. digitalocean_genai_sdk/resources/providers/anthropic/keys.py +662 -0
  58. digitalocean_genai_sdk/resources/providers/openai/__init__.py +33 -0
  59. digitalocean_genai_sdk/resources/providers/openai/keys.py +658 -0
  60. digitalocean_genai_sdk/resources/providers/openai/openai.py +102 -0
  61. digitalocean_genai_sdk/resources/providers/providers.py +134 -0
  62. digitalocean_genai_sdk/resources/regions.py +191 -0
  63. digitalocean_genai_sdk/types/__init__.py +57 -0
  64. digitalocean_genai_sdk/types/agent_create_params.py +39 -0
  65. digitalocean_genai_sdk/types/agent_create_response.py +16 -0
  66. digitalocean_genai_sdk/types/agent_delete_response.py +16 -0
  67. digitalocean_genai_sdk/types/agent_list_params.py +18 -0
  68. digitalocean_genai_sdk/types/agent_list_response.py +198 -0
  69. digitalocean_genai_sdk/types/agent_retrieve_response.py +16 -0
  70. digitalocean_genai_sdk/types/agent_update_params.py +65 -0
  71. digitalocean_genai_sdk/types/agent_update_response.py +16 -0
  72. digitalocean_genai_sdk/types/agent_update_status_params.py +16 -0
  73. digitalocean_genai_sdk/types/agent_update_status_response.py +16 -0
  74. digitalocean_genai_sdk/types/agents/__init__.py +31 -0
  75. digitalocean_genai_sdk/types/agents/api_key_create_params.py +15 -0
  76. digitalocean_genai_sdk/types/agents/api_key_create_response.py +12 -0
  77. digitalocean_genai_sdk/types/agents/api_key_delete_response.py +12 -0
  78. digitalocean_genai_sdk/types/agents/api_key_list_params.py +15 -0
  79. digitalocean_genai_sdk/types/agents/api_key_list_response.py +18 -0
  80. digitalocean_genai_sdk/types/agents/api_key_regenerate_response.py +12 -0
  81. digitalocean_genai_sdk/types/agents/api_key_update_params.py +19 -0
  82. digitalocean_genai_sdk/types/agents/api_key_update_response.py +12 -0
  83. digitalocean_genai_sdk/types/agents/api_link_knowledge_base_output.py +16 -0
  84. digitalocean_genai_sdk/types/agents/api_links.py +21 -0
  85. digitalocean_genai_sdk/types/agents/api_meta.py +15 -0
  86. digitalocean_genai_sdk/types/agents/child_agent_add_params.py +22 -0
  87. digitalocean_genai_sdk/types/agents/child_agent_add_response.py +14 -0
  88. digitalocean_genai_sdk/types/agents/child_agent_delete_response.py +13 -0
  89. digitalocean_genai_sdk/types/agents/child_agent_update_params.py +24 -0
  90. digitalocean_genai_sdk/types/agents/child_agent_update_response.py +18 -0
  91. digitalocean_genai_sdk/types/agents/child_agent_view_response.py +16 -0
  92. digitalocean_genai_sdk/types/agents/function_create_params.py +25 -0
  93. digitalocean_genai_sdk/types/agents/function_create_response.py +16 -0
  94. digitalocean_genai_sdk/types/agents/function_delete_response.py +16 -0
  95. digitalocean_genai_sdk/types/agents/function_update_params.py +29 -0
  96. digitalocean_genai_sdk/types/agents/function_update_response.py +16 -0
  97. digitalocean_genai_sdk/types/agents/knowledge_base_detach_response.py +16 -0
  98. digitalocean_genai_sdk/types/agents/version_list_params.py +15 -0
  99. digitalocean_genai_sdk/types/agents/version_list_response.py +118 -0
  100. digitalocean_genai_sdk/types/agents/version_update_params.py +15 -0
  101. digitalocean_genai_sdk/types/agents/version_update_response.py +30 -0
  102. digitalocean_genai_sdk/types/api_agent.py +263 -0
  103. digitalocean_genai_sdk/types/api_agent_api_key_info.py +22 -0
  104. digitalocean_genai_sdk/types/api_agreement.py +17 -0
  105. digitalocean_genai_sdk/types/api_anthropic_api_key_info.py +22 -0
  106. digitalocean_genai_sdk/types/api_deployment_visibility.py +9 -0
  107. digitalocean_genai_sdk/types/api_indexing_job.py +43 -0
  108. digitalocean_genai_sdk/types/api_key_list_params.py +42 -0
  109. digitalocean_genai_sdk/types/api_key_list_response.py +42 -0
  110. digitalocean_genai_sdk/types/api_keys/__init__.py +13 -0
  111. digitalocean_genai_sdk/types/api_keys/api_key_create_params.py +11 -0
  112. digitalocean_genai_sdk/types/api_keys/api_key_create_response.py +12 -0
  113. digitalocean_genai_sdk/types/api_keys/api_key_delete_response.py +12 -0
  114. digitalocean_genai_sdk/types/api_keys/api_key_list_params.py +15 -0
  115. digitalocean_genai_sdk/types/api_keys/api_key_list_response.py +18 -0
  116. digitalocean_genai_sdk/types/api_keys/api_key_update_params.py +15 -0
  117. digitalocean_genai_sdk/types/api_keys/api_key_update_regenerate_response.py +12 -0
  118. digitalocean_genai_sdk/types/api_keys/api_key_update_response.py +12 -0
  119. digitalocean_genai_sdk/types/api_keys/api_model_api_key_info.py +22 -0
  120. digitalocean_genai_sdk/types/api_knowledge_base.py +37 -0
  121. digitalocean_genai_sdk/types/api_model.py +57 -0
  122. digitalocean_genai_sdk/types/api_model_version.py +15 -0
  123. digitalocean_genai_sdk/types/api_openai_api_key_info.py +25 -0
  124. digitalocean_genai_sdk/types/api_retrieval_method.py +13 -0
  125. digitalocean_genai_sdk/types/auth/__init__.py +3 -0
  126. digitalocean_genai_sdk/types/auth/agents/__init__.py +6 -0
  127. digitalocean_genai_sdk/types/auth/agents/token_create_params.py +13 -0
  128. digitalocean_genai_sdk/types/auth/agents/token_create_response.py +13 -0
  129. digitalocean_genai_sdk/types/chat_completion_request_message_content_part_text_param.py +15 -0
  130. digitalocean_genai_sdk/types/chat_completion_token_logprob.py +57 -0
  131. digitalocean_genai_sdk/types/chat_create_completion_params.py +208 -0
  132. digitalocean_genai_sdk/types/chat_create_completion_response.py +81 -0
  133. digitalocean_genai_sdk/types/embedding_create_params.py +28 -0
  134. digitalocean_genai_sdk/types/embedding_create_response.py +41 -0
  135. digitalocean_genai_sdk/types/indexing_job_create_params.py +14 -0
  136. digitalocean_genai_sdk/types/indexing_job_create_response.py +12 -0
  137. digitalocean_genai_sdk/types/indexing_job_list_params.py +15 -0
  138. digitalocean_genai_sdk/types/indexing_job_list_response.py +18 -0
  139. digitalocean_genai_sdk/types/indexing_job_retrieve_data_sources_response.py +52 -0
  140. digitalocean_genai_sdk/types/indexing_job_retrieve_response.py +12 -0
  141. digitalocean_genai_sdk/types/indexing_job_update_cancel_params.py +14 -0
  142. digitalocean_genai_sdk/types/indexing_job_update_cancel_response.py +12 -0
  143. digitalocean_genai_sdk/types/knowledge_base_create_params.py +64 -0
  144. digitalocean_genai_sdk/types/knowledge_base_create_response.py +12 -0
  145. digitalocean_genai_sdk/types/knowledge_base_delete_response.py +11 -0
  146. digitalocean_genai_sdk/types/knowledge_base_list_params.py +15 -0
  147. digitalocean_genai_sdk/types/knowledge_base_list_response.py +18 -0
  148. digitalocean_genai_sdk/types/knowledge_base_retrieve_response.py +30 -0
  149. digitalocean_genai_sdk/types/knowledge_base_update_params.py +27 -0
  150. digitalocean_genai_sdk/types/knowledge_base_update_response.py +12 -0
  151. digitalocean_genai_sdk/types/knowledge_bases/__init__.py +16 -0
  152. digitalocean_genai_sdk/types/knowledge_bases/api_file_upload_data_source.py +15 -0
  153. digitalocean_genai_sdk/types/knowledge_bases/api_file_upload_data_source_param.py +15 -0
  154. digitalocean_genai_sdk/types/knowledge_bases/api_knowledge_base_data_source.py +35 -0
  155. digitalocean_genai_sdk/types/knowledge_bases/api_spaces_data_source.py +15 -0
  156. digitalocean_genai_sdk/types/knowledge_bases/api_spaces_data_source_param.py +15 -0
  157. digitalocean_genai_sdk/types/knowledge_bases/api_web_crawler_data_source.py +26 -0
  158. digitalocean_genai_sdk/types/knowledge_bases/api_web_crawler_data_source_param.py +25 -0
  159. digitalocean_genai_sdk/types/knowledge_bases/data_source_create_params.py +33 -0
  160. digitalocean_genai_sdk/types/knowledge_bases/data_source_create_response.py +12 -0
  161. digitalocean_genai_sdk/types/knowledge_bases/data_source_delete_response.py +13 -0
  162. digitalocean_genai_sdk/types/knowledge_bases/data_source_list_params.py +15 -0
  163. digitalocean_genai_sdk/types/knowledge_bases/data_source_list_response.py +18 -0
  164. digitalocean_genai_sdk/types/model.py +21 -0
  165. digitalocean_genai_sdk/types/model_list_response.py +15 -0
  166. digitalocean_genai_sdk/types/providers/__init__.py +3 -0
  167. digitalocean_genai_sdk/types/providers/anthropic/__init__.py +14 -0
  168. digitalocean_genai_sdk/types/providers/anthropic/key_create_params.py +13 -0
  169. digitalocean_genai_sdk/types/providers/anthropic/key_create_response.py +12 -0
  170. digitalocean_genai_sdk/types/providers/anthropic/key_delete_response.py +12 -0
  171. digitalocean_genai_sdk/types/providers/anthropic/key_list_agents_params.py +15 -0
  172. digitalocean_genai_sdk/types/providers/anthropic/key_list_agents_response.py +22 -0
  173. digitalocean_genai_sdk/types/providers/anthropic/key_list_params.py +15 -0
  174. digitalocean_genai_sdk/types/providers/anthropic/key_list_response.py +18 -0
  175. digitalocean_genai_sdk/types/providers/anthropic/key_retrieve_response.py +12 -0
  176. digitalocean_genai_sdk/types/providers/anthropic/key_update_params.py +17 -0
  177. digitalocean_genai_sdk/types/providers/anthropic/key_update_response.py +12 -0
  178. digitalocean_genai_sdk/types/providers/openai/__init__.py +14 -0
  179. digitalocean_genai_sdk/types/providers/openai/key_create_params.py +13 -0
  180. digitalocean_genai_sdk/types/providers/openai/key_create_response.py +12 -0
  181. digitalocean_genai_sdk/types/providers/openai/key_delete_response.py +12 -0
  182. digitalocean_genai_sdk/types/providers/openai/key_list_params.py +15 -0
  183. digitalocean_genai_sdk/types/providers/openai/key_list_response.py +18 -0
  184. digitalocean_genai_sdk/types/providers/openai/key_retrieve_agents_params.py +15 -0
  185. digitalocean_genai_sdk/types/providers/openai/key_retrieve_agents_response.py +22 -0
  186. digitalocean_genai_sdk/types/providers/openai/key_retrieve_response.py +12 -0
  187. digitalocean_genai_sdk/types/providers/openai/key_update_params.py +17 -0
  188. digitalocean_genai_sdk/types/providers/openai/key_update_response.py +12 -0
  189. digitalocean_genai_sdk/types/region_list_params.py +15 -0
  190. digitalocean_genai_sdk/types/region_list_response.py +23 -0
@@ -0,0 +1,42 @@
1
+ from __future__ import annotations
2
+
3
+ import inspect
4
+ from typing import Any, Callable
5
+
6
+
7
+ def function_has_argument(func: Callable[..., Any], arg_name: str) -> bool:
8
+ """Returns whether or not the given function has a specific parameter"""
9
+ sig = inspect.signature(func)
10
+ return arg_name in sig.parameters
11
+
12
+
13
+ def assert_signatures_in_sync(
14
+ source_func: Callable[..., Any],
15
+ check_func: Callable[..., Any],
16
+ *,
17
+ exclude_params: set[str] = set(),
18
+ ) -> None:
19
+ """Ensure that the signature of the second function matches the first."""
20
+
21
+ check_sig = inspect.signature(check_func)
22
+ source_sig = inspect.signature(source_func)
23
+
24
+ errors: list[str] = []
25
+
26
+ for name, source_param in source_sig.parameters.items():
27
+ if name in exclude_params:
28
+ continue
29
+
30
+ custom_param = check_sig.parameters.get(name)
31
+ if not custom_param:
32
+ errors.append(f"the `{name}` param is missing")
33
+ continue
34
+
35
+ if custom_param.annotation != source_param.annotation:
36
+ errors.append(
37
+ f"types for the `{name}` param are do not match; source={repr(source_param.annotation)} checking={repr(custom_param.annotation)}"
38
+ )
39
+ continue
40
+
41
+ if errors:
42
+ raise AssertionError(f"{len(errors)} errors encountered when comparing signatures:\n\n" + "\n\n".join(errors))
@@ -0,0 +1,24 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any
4
+ from typing_extensions import override
5
+
6
+ from ._proxy import LazyProxy
7
+
8
+
9
+ class ResourcesProxy(LazyProxy[Any]):
10
+ """A proxy for the `digitalocean_genai_sdk.resources` module.
11
+
12
+ This is used so that we can lazily import `digitalocean_genai_sdk.resources` only when
13
+ needed *and* so that users can just import `digitalocean_genai_sdk` and reference `digitalocean_genai_sdk.resources`
14
+ """
15
+
16
+ @override
17
+ def __load__(self) -> Any:
18
+ import importlib
19
+
20
+ mod = importlib.import_module("digitalocean_genai_sdk.resources")
21
+ return mod
22
+
23
+
24
+ resources = ResourcesProxy().__as_proxied__()
@@ -0,0 +1,12 @@
1
+ from typing import Any
2
+ from typing_extensions import Iterator, AsyncIterator
3
+
4
+
5
+ def consume_sync_iterator(iterator: Iterator[Any]) -> None:
6
+ for _ in iterator:
7
+ ...
8
+
9
+
10
+ async def consume_async_iterator(iterator: AsyncIterator[Any]) -> None:
11
+ async for _ in iterator:
12
+ ...
@@ -0,0 +1,86 @@
1
+ from __future__ import annotations
2
+
3
+ import sys
4
+ import asyncio
5
+ import functools
6
+ import contextvars
7
+ from typing import Any, TypeVar, Callable, Awaitable
8
+ from typing_extensions import ParamSpec
9
+
10
+ import anyio
11
+ import sniffio
12
+ import anyio.to_thread
13
+
14
+ T_Retval = TypeVar("T_Retval")
15
+ T_ParamSpec = ParamSpec("T_ParamSpec")
16
+
17
+
18
+ if sys.version_info >= (3, 9):
19
+ _asyncio_to_thread = asyncio.to_thread
20
+ else:
21
+ # backport of https://docs.python.org/3/library/asyncio-task.html#asyncio.to_thread
22
+ # for Python 3.8 support
23
+ async def _asyncio_to_thread(
24
+ func: Callable[T_ParamSpec, T_Retval], /, *args: T_ParamSpec.args, **kwargs: T_ParamSpec.kwargs
25
+ ) -> Any:
26
+ """Asynchronously run function *func* in a separate thread.
27
+
28
+ Any *args and **kwargs supplied for this function are directly passed
29
+ to *func*. Also, the current :class:`contextvars.Context` is propagated,
30
+ allowing context variables from the main thread to be accessed in the
31
+ separate thread.
32
+
33
+ Returns a coroutine that can be awaited to get the eventual result of *func*.
34
+ """
35
+ loop = asyncio.events.get_running_loop()
36
+ ctx = contextvars.copy_context()
37
+ func_call = functools.partial(ctx.run, func, *args, **kwargs)
38
+ return await loop.run_in_executor(None, func_call)
39
+
40
+
41
+ async def to_thread(
42
+ func: Callable[T_ParamSpec, T_Retval], /, *args: T_ParamSpec.args, **kwargs: T_ParamSpec.kwargs
43
+ ) -> T_Retval:
44
+ if sniffio.current_async_library() == "asyncio":
45
+ return await _asyncio_to_thread(func, *args, **kwargs)
46
+
47
+ return await anyio.to_thread.run_sync(
48
+ functools.partial(func, *args, **kwargs),
49
+ )
50
+
51
+
52
+ # inspired by `asyncer`, https://github.com/tiangolo/asyncer
53
+ def asyncify(function: Callable[T_ParamSpec, T_Retval]) -> Callable[T_ParamSpec, Awaitable[T_Retval]]:
54
+ """
55
+ Take a blocking function and create an async one that receives the same
56
+ positional and keyword arguments. For python version 3.9 and above, it uses
57
+ asyncio.to_thread to run the function in a separate thread. For python version
58
+ 3.8, it uses locally defined copy of the asyncio.to_thread function which was
59
+ introduced in python 3.9.
60
+
61
+ Usage:
62
+
63
+ ```python
64
+ def blocking_func(arg1, arg2, kwarg1=None):
65
+ # blocking code
66
+ return result
67
+
68
+
69
+ result = asyncify(blocking_function)(arg1, arg2, kwarg1=value1)
70
+ ```
71
+
72
+ ## Arguments
73
+
74
+ `function`: a blocking regular callable (e.g. a function)
75
+
76
+ ## Return
77
+
78
+ An async function that takes the same positional and keyword arguments as the
79
+ original one, that when called runs the same original function in a thread worker
80
+ and returns the result.
81
+ """
82
+
83
+ async def wrapper(*args: T_ParamSpec.args, **kwargs: T_ParamSpec.kwargs) -> T_Retval:
84
+ return await to_thread(function, *args, **kwargs)
85
+
86
+ return wrapper
@@ -0,0 +1,447 @@
1
+ from __future__ import annotations
2
+
3
+ import io
4
+ import base64
5
+ import pathlib
6
+ from typing import Any, Mapping, TypeVar, cast
7
+ from datetime import date, datetime
8
+ from typing_extensions import Literal, get_args, override, get_type_hints as _get_type_hints
9
+
10
+ import anyio
11
+ import pydantic
12
+
13
+ from ._utils import (
14
+ is_list,
15
+ is_given,
16
+ lru_cache,
17
+ is_mapping,
18
+ is_iterable,
19
+ )
20
+ from .._files import is_base64_file_input
21
+ from ._typing import (
22
+ is_list_type,
23
+ is_union_type,
24
+ extract_type_arg,
25
+ is_iterable_type,
26
+ is_required_type,
27
+ is_annotated_type,
28
+ strip_annotated_type,
29
+ )
30
+ from .._compat import get_origin, model_dump, is_typeddict
31
+
32
+ _T = TypeVar("_T")
33
+
34
+
35
+ # TODO: support for drilling globals() and locals()
36
+ # TODO: ensure works correctly with forward references in all cases
37
+
38
+
39
+ PropertyFormat = Literal["iso8601", "base64", "custom"]
40
+
41
+
42
+ class PropertyInfo:
43
+ """Metadata class to be used in Annotated types to provide information about a given type.
44
+
45
+ For example:
46
+
47
+ class MyParams(TypedDict):
48
+ account_holder_name: Annotated[str, PropertyInfo(alias='accountHolderName')]
49
+
50
+ This means that {'account_holder_name': 'Robert'} will be transformed to {'accountHolderName': 'Robert'} before being sent to the API.
51
+ """
52
+
53
+ alias: str | None
54
+ format: PropertyFormat | None
55
+ format_template: str | None
56
+ discriminator: str | None
57
+
58
+ def __init__(
59
+ self,
60
+ *,
61
+ alias: str | None = None,
62
+ format: PropertyFormat | None = None,
63
+ format_template: str | None = None,
64
+ discriminator: str | None = None,
65
+ ) -> None:
66
+ self.alias = alias
67
+ self.format = format
68
+ self.format_template = format_template
69
+ self.discriminator = discriminator
70
+
71
+ @override
72
+ def __repr__(self) -> str:
73
+ return f"{self.__class__.__name__}(alias='{self.alias}', format={self.format}, format_template='{self.format_template}', discriminator='{self.discriminator}')"
74
+
75
+
76
+ def maybe_transform(
77
+ data: object,
78
+ expected_type: object,
79
+ ) -> Any | None:
80
+ """Wrapper over `transform()` that allows `None` to be passed.
81
+
82
+ See `transform()` for more details.
83
+ """
84
+ if data is None:
85
+ return None
86
+ return transform(data, expected_type)
87
+
88
+
89
+ # Wrapper over _transform_recursive providing fake types
90
+ def transform(
91
+ data: _T,
92
+ expected_type: object,
93
+ ) -> _T:
94
+ """Transform dictionaries based off of type information from the given type, for example:
95
+
96
+ ```py
97
+ class Params(TypedDict, total=False):
98
+ card_id: Required[Annotated[str, PropertyInfo(alias="cardID")]]
99
+
100
+
101
+ transformed = transform({"card_id": "<my card ID>"}, Params)
102
+ # {'cardID': '<my card ID>'}
103
+ ```
104
+
105
+ Any keys / data that does not have type information given will be included as is.
106
+
107
+ It should be noted that the transformations that this function does are not represented in the type system.
108
+ """
109
+ transformed = _transform_recursive(data, annotation=cast(type, expected_type))
110
+ return cast(_T, transformed)
111
+
112
+
113
+ @lru_cache(maxsize=8096)
114
+ def _get_annotated_type(type_: type) -> type | None:
115
+ """If the given type is an `Annotated` type then it is returned, if not `None` is returned.
116
+
117
+ This also unwraps the type when applicable, e.g. `Required[Annotated[T, ...]]`
118
+ """
119
+ if is_required_type(type_):
120
+ # Unwrap `Required[Annotated[T, ...]]` to `Annotated[T, ...]`
121
+ type_ = get_args(type_)[0]
122
+
123
+ if is_annotated_type(type_):
124
+ return type_
125
+
126
+ return None
127
+
128
+
129
+ def _maybe_transform_key(key: str, type_: type) -> str:
130
+ """Transform the given `data` based on the annotations provided in `type_`.
131
+
132
+ Note: this function only looks at `Annotated` types that contain `PropertyInfo` metadata.
133
+ """
134
+ annotated_type = _get_annotated_type(type_)
135
+ if annotated_type is None:
136
+ # no `Annotated` definition for this type, no transformation needed
137
+ return key
138
+
139
+ # ignore the first argument as it is the actual type
140
+ annotations = get_args(annotated_type)[1:]
141
+ for annotation in annotations:
142
+ if isinstance(annotation, PropertyInfo) and annotation.alias is not None:
143
+ return annotation.alias
144
+
145
+ return key
146
+
147
+
148
+ def _no_transform_needed(annotation: type) -> bool:
149
+ return annotation == float or annotation == int
150
+
151
+
152
+ def _transform_recursive(
153
+ data: object,
154
+ *,
155
+ annotation: type,
156
+ inner_type: type | None = None,
157
+ ) -> object:
158
+ """Transform the given data against the expected type.
159
+
160
+ Args:
161
+ annotation: The direct type annotation given to the particular piece of data.
162
+ This may or may not be wrapped in metadata types, e.g. `Required[T]`, `Annotated[T, ...]` etc
163
+
164
+ inner_type: If applicable, this is the "inside" type. This is useful in certain cases where the outside type
165
+ is a container type such as `List[T]`. In that case `inner_type` should be set to `T` so that each entry in
166
+ the list can be transformed using the metadata from the container type.
167
+
168
+ Defaults to the same value as the `annotation` argument.
169
+ """
170
+ if inner_type is None:
171
+ inner_type = annotation
172
+
173
+ stripped_type = strip_annotated_type(inner_type)
174
+ origin = get_origin(stripped_type) or stripped_type
175
+ if is_typeddict(stripped_type) and is_mapping(data):
176
+ return _transform_typeddict(data, stripped_type)
177
+
178
+ if origin == dict and is_mapping(data):
179
+ items_type = get_args(stripped_type)[1]
180
+ return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}
181
+
182
+ if (
183
+ # List[T]
184
+ (is_list_type(stripped_type) and is_list(data))
185
+ # Iterable[T]
186
+ or (is_iterable_type(stripped_type) and is_iterable(data) and not isinstance(data, str))
187
+ ):
188
+ # dicts are technically iterable, but it is an iterable on the keys of the dict and is not usually
189
+ # intended as an iterable, so we don't transform it.
190
+ if isinstance(data, dict):
191
+ return cast(object, data)
192
+
193
+ inner_type = extract_type_arg(stripped_type, 0)
194
+ if _no_transform_needed(inner_type):
195
+ # for some types there is no need to transform anything, so we can get a small
196
+ # perf boost from skipping that work.
197
+ #
198
+ # but we still need to convert to a list to ensure the data is json-serializable
199
+ if is_list(data):
200
+ return data
201
+ return list(data)
202
+
203
+ return [_transform_recursive(d, annotation=annotation, inner_type=inner_type) for d in data]
204
+
205
+ if is_union_type(stripped_type):
206
+ # For union types we run the transformation against all subtypes to ensure that everything is transformed.
207
+ #
208
+ # TODO: there may be edge cases where the same normalized field name will transform to two different names
209
+ # in different subtypes.
210
+ for subtype in get_args(stripped_type):
211
+ data = _transform_recursive(data, annotation=annotation, inner_type=subtype)
212
+ return data
213
+
214
+ if isinstance(data, pydantic.BaseModel):
215
+ return model_dump(data, exclude_unset=True, mode="json")
216
+
217
+ annotated_type = _get_annotated_type(annotation)
218
+ if annotated_type is None:
219
+ return data
220
+
221
+ # ignore the first argument as it is the actual type
222
+ annotations = get_args(annotated_type)[1:]
223
+ for annotation in annotations:
224
+ if isinstance(annotation, PropertyInfo) and annotation.format is not None:
225
+ return _format_data(data, annotation.format, annotation.format_template)
226
+
227
+ return data
228
+
229
+
230
+ def _format_data(data: object, format_: PropertyFormat, format_template: str | None) -> object:
231
+ if isinstance(data, (date, datetime)):
232
+ if format_ == "iso8601":
233
+ return data.isoformat()
234
+
235
+ if format_ == "custom" and format_template is not None:
236
+ return data.strftime(format_template)
237
+
238
+ if format_ == "base64" and is_base64_file_input(data):
239
+ binary: str | bytes | None = None
240
+
241
+ if isinstance(data, pathlib.Path):
242
+ binary = data.read_bytes()
243
+ elif isinstance(data, io.IOBase):
244
+ binary = data.read()
245
+
246
+ if isinstance(binary, str): # type: ignore[unreachable]
247
+ binary = binary.encode()
248
+
249
+ if not isinstance(binary, bytes):
250
+ raise RuntimeError(f"Could not read bytes from {data}; Received {type(binary)}")
251
+
252
+ return base64.b64encode(binary).decode("ascii")
253
+
254
+ return data
255
+
256
+
257
+ def _transform_typeddict(
258
+ data: Mapping[str, object],
259
+ expected_type: type,
260
+ ) -> Mapping[str, object]:
261
+ result: dict[str, object] = {}
262
+ annotations = get_type_hints(expected_type, include_extras=True)
263
+ for key, value in data.items():
264
+ if not is_given(value):
265
+ # we don't need to include `NotGiven` values here as they'll
266
+ # be stripped out before the request is sent anyway
267
+ continue
268
+
269
+ type_ = annotations.get(key)
270
+ if type_ is None:
271
+ # we do not have a type annotation for this field, leave it as is
272
+ result[key] = value
273
+ else:
274
+ result[_maybe_transform_key(key, type_)] = _transform_recursive(value, annotation=type_)
275
+ return result
276
+
277
+
278
+ async def async_maybe_transform(
279
+ data: object,
280
+ expected_type: object,
281
+ ) -> Any | None:
282
+ """Wrapper over `async_transform()` that allows `None` to be passed.
283
+
284
+ See `async_transform()` for more details.
285
+ """
286
+ if data is None:
287
+ return None
288
+ return await async_transform(data, expected_type)
289
+
290
+
291
+ async def async_transform(
292
+ data: _T,
293
+ expected_type: object,
294
+ ) -> _T:
295
+ """Transform dictionaries based off of type information from the given type, for example:
296
+
297
+ ```py
298
+ class Params(TypedDict, total=False):
299
+ card_id: Required[Annotated[str, PropertyInfo(alias="cardID")]]
300
+
301
+
302
+ transformed = transform({"card_id": "<my card ID>"}, Params)
303
+ # {'cardID': '<my card ID>'}
304
+ ```
305
+
306
+ Any keys / data that does not have type information given will be included as is.
307
+
308
+ It should be noted that the transformations that this function does are not represented in the type system.
309
+ """
310
+ transformed = await _async_transform_recursive(data, annotation=cast(type, expected_type))
311
+ return cast(_T, transformed)
312
+
313
+
314
+ async def _async_transform_recursive(
315
+ data: object,
316
+ *,
317
+ annotation: type,
318
+ inner_type: type | None = None,
319
+ ) -> object:
320
+ """Transform the given data against the expected type.
321
+
322
+ Args:
323
+ annotation: The direct type annotation given to the particular piece of data.
324
+ This may or may not be wrapped in metadata types, e.g. `Required[T]`, `Annotated[T, ...]` etc
325
+
326
+ inner_type: If applicable, this is the "inside" type. This is useful in certain cases where the outside type
327
+ is a container type such as `List[T]`. In that case `inner_type` should be set to `T` so that each entry in
328
+ the list can be transformed using the metadata from the container type.
329
+
330
+ Defaults to the same value as the `annotation` argument.
331
+ """
332
+ if inner_type is None:
333
+ inner_type = annotation
334
+
335
+ stripped_type = strip_annotated_type(inner_type)
336
+ origin = get_origin(stripped_type) or stripped_type
337
+ if is_typeddict(stripped_type) and is_mapping(data):
338
+ return await _async_transform_typeddict(data, stripped_type)
339
+
340
+ if origin == dict and is_mapping(data):
341
+ items_type = get_args(stripped_type)[1]
342
+ return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}
343
+
344
+ if (
345
+ # List[T]
346
+ (is_list_type(stripped_type) and is_list(data))
347
+ # Iterable[T]
348
+ or (is_iterable_type(stripped_type) and is_iterable(data) and not isinstance(data, str))
349
+ ):
350
+ # dicts are technically iterable, but it is an iterable on the keys of the dict and is not usually
351
+ # intended as an iterable, so we don't transform it.
352
+ if isinstance(data, dict):
353
+ return cast(object, data)
354
+
355
+ inner_type = extract_type_arg(stripped_type, 0)
356
+ if _no_transform_needed(inner_type):
357
+ # for some types there is no need to transform anything, so we can get a small
358
+ # perf boost from skipping that work.
359
+ #
360
+ # but we still need to convert to a list to ensure the data is json-serializable
361
+ if is_list(data):
362
+ return data
363
+ return list(data)
364
+
365
+ return [await _async_transform_recursive(d, annotation=annotation, inner_type=inner_type) for d in data]
366
+
367
+ if is_union_type(stripped_type):
368
+ # For union types we run the transformation against all subtypes to ensure that everything is transformed.
369
+ #
370
+ # TODO: there may be edge cases where the same normalized field name will transform to two different names
371
+ # in different subtypes.
372
+ for subtype in get_args(stripped_type):
373
+ data = await _async_transform_recursive(data, annotation=annotation, inner_type=subtype)
374
+ return data
375
+
376
+ if isinstance(data, pydantic.BaseModel):
377
+ return model_dump(data, exclude_unset=True, mode="json")
378
+
379
+ annotated_type = _get_annotated_type(annotation)
380
+ if annotated_type is None:
381
+ return data
382
+
383
+ # ignore the first argument as it is the actual type
384
+ annotations = get_args(annotated_type)[1:]
385
+ for annotation in annotations:
386
+ if isinstance(annotation, PropertyInfo) and annotation.format is not None:
387
+ return await _async_format_data(data, annotation.format, annotation.format_template)
388
+
389
+ return data
390
+
391
+
392
+ async def _async_format_data(data: object, format_: PropertyFormat, format_template: str | None) -> object:
393
+ if isinstance(data, (date, datetime)):
394
+ if format_ == "iso8601":
395
+ return data.isoformat()
396
+
397
+ if format_ == "custom" and format_template is not None:
398
+ return data.strftime(format_template)
399
+
400
+ if format_ == "base64" and is_base64_file_input(data):
401
+ binary: str | bytes | None = None
402
+
403
+ if isinstance(data, pathlib.Path):
404
+ binary = await anyio.Path(data).read_bytes()
405
+ elif isinstance(data, io.IOBase):
406
+ binary = data.read()
407
+
408
+ if isinstance(binary, str): # type: ignore[unreachable]
409
+ binary = binary.encode()
410
+
411
+ if not isinstance(binary, bytes):
412
+ raise RuntimeError(f"Could not read bytes from {data}; Received {type(binary)}")
413
+
414
+ return base64.b64encode(binary).decode("ascii")
415
+
416
+ return data
417
+
418
+
419
+ async def _async_transform_typeddict(
420
+ data: Mapping[str, object],
421
+ expected_type: type,
422
+ ) -> Mapping[str, object]:
423
+ result: dict[str, object] = {}
424
+ annotations = get_type_hints(expected_type, include_extras=True)
425
+ for key, value in data.items():
426
+ if not is_given(value):
427
+ # we don't need to include `NotGiven` values here as they'll
428
+ # be stripped out before the request is sent anyway
429
+ continue
430
+
431
+ type_ = annotations.get(key)
432
+ if type_ is None:
433
+ # we do not have a type annotation for this field, leave it as is
434
+ result[key] = value
435
+ else:
436
+ result[_maybe_transform_key(key, type_)] = await _async_transform_recursive(value, annotation=type_)
437
+ return result
438
+
439
+
440
+ @lru_cache(maxsize=8096)
441
+ def get_type_hints(
442
+ obj: Any,
443
+ globalns: dict[str, Any] | None = None,
444
+ localns: Mapping[str, Any] | None = None,
445
+ include_extras: bool = False,
446
+ ) -> dict[str, Any]:
447
+ return _get_type_hints(obj, globalns=globalns, localns=localns, include_extras=include_extras)