mixpeek 0.12.0__py3-none-any.whl → 0.13__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 (270) hide show
  1. mixpeek/__init__.py +5 -79
  2. mixpeek/_hooks/__init__.py +5 -0
  3. mixpeek/_hooks/registration.py +13 -0
  4. mixpeek/_hooks/sdkhooks.py +76 -0
  5. mixpeek/_hooks/types.py +94 -0
  6. mixpeek/_version.py +11 -3
  7. mixpeek/assets.py +1561 -0
  8. mixpeek/basesdk.py +350 -0
  9. mixpeek/collections.py +1029 -0
  10. mixpeek/featureextractors.py +205 -0
  11. mixpeek/features.py +1181 -0
  12. mixpeek/health.py +167 -0
  13. mixpeek/httpclient.py +84 -0
  14. mixpeek/ingest.py +831 -0
  15. mixpeek/interactions.py +221 -0
  16. mixpeek/models/__init__.py +751 -0
  17. mixpeek/models/actionusage.py +16 -0
  18. mixpeek/models/apierror.py +22 -0
  19. mixpeek/models/apikey.py +25 -0
  20. mixpeek/models/apikeyupdate.py +49 -0
  21. mixpeek/models/assetfeatures.py +55 -0
  22. mixpeek/models/assetresponse.py +166 -0
  23. mixpeek/models/assets_model_searchquery.py +21 -0
  24. mixpeek/models/assetupdate.py +28 -0
  25. mixpeek/models/availableindexesresponse.py +23 -0
  26. mixpeek/models/availablemodels.py +12 -0
  27. mixpeek/models/boolindexparams.py +18 -0
  28. mixpeek/models/collectionmodel.py +70 -0
  29. mixpeek/models/collectionresult.py +73 -0
  30. mixpeek/models/create_api_key_organizations_users_user_email_api_keys_postop.py +23 -0
  31. mixpeek/models/create_collection_collections_postop.py +62 -0
  32. mixpeek/models/create_interaction_features_search_interactions_postop.py +59 -0
  33. mixpeek/models/createcollectionrequest.py +59 -0
  34. mixpeek/models/createnamespacerequest.py +62 -0
  35. mixpeek/models/datetimeindexparams.py +18 -0
  36. mixpeek/models/dateusage.py +22 -0
  37. mixpeek/models/db_model_paginationresponse.py +59 -0
  38. mixpeek/models/db_model_taskresponse.py +20 -0
  39. mixpeek/models/delete_api_key_organizations_users_user_email_api_keys_key_name_deleteop.py +23 -0
  40. mixpeek/models/delete_asset_assets_asset_id_deleteop.py +57 -0
  41. mixpeek/models/delete_collection_collections_collection_deleteop.py +59 -0
  42. mixpeek/models/delete_feature_features_feature_id_deleteop.py +57 -0
  43. mixpeek/models/delete_interaction_features_search_interactions_interaction_id_deleteop.py +59 -0
  44. mixpeek/models/delete_namespace_namespaces_namespace_deleteop.py +18 -0
  45. mixpeek/models/delete_user_organizations_users_user_email_deleteop.py +16 -0
  46. mixpeek/models/denseembedding.py +16 -0
  47. mixpeek/models/embeddingrequest.py +59 -0
  48. mixpeek/models/embeddingresponse.py +64 -0
  49. mixpeek/models/errormessage.py +13 -0
  50. mixpeek/models/errorresponse.py +21 -0
  51. mixpeek/models/facedetectsettings.py +52 -0
  52. mixpeek/models/featureextractionembeddingrequest.py +54 -0
  53. mixpeek/models/featureresponse.py +74 -0
  54. mixpeek/models/features_model_paginationresponse.py +59 -0
  55. mixpeek/models/featureupdaterequest.py +21 -0
  56. mixpeek/models/filtercondition.py +74 -0
  57. mixpeek/models/floatindexparams.py +18 -0
  58. mixpeek/models/full_asset_update_assets_asset_id_putop.py +69 -0
  59. mixpeek/models/full_feature_update_features_feature_id_putop.py +69 -0
  60. mixpeek/models/geoindexparams.py +18 -0
  61. mixpeek/models/get_asset_assets_asset_id_getop.py +73 -0
  62. mixpeek/models/get_asset_with_features_assets_asset_id_features_getop.py +73 -0
  63. mixpeek/models/get_collection_collections_collection_getop.py +59 -0
  64. mixpeek/models/get_feature_features_feature_id_getop.py +70 -0
  65. mixpeek/models/get_interaction_features_search_interactions_interaction_id_getop.py +59 -0
  66. mixpeek/models/get_namespace_namespaces_namespace_getop.py +18 -0
  67. mixpeek/models/get_task_tasks_task_id_getop.py +57 -0
  68. mixpeek/models/get_user_organizations_users_user_email_getop.py +16 -0
  69. mixpeek/models/groupbyoptions.py +71 -0
  70. mixpeek/models/groupbyoptionsasset.py +71 -0
  71. mixpeek/models/groupedassetdata.py +18 -0
  72. mixpeek/models/healthcheckresponse.py +13 -0
  73. mixpeek/models/httpvalidationerror.py +21 -0
  74. mixpeek/models/imagedescribesettings.py +82 -0
  75. mixpeek/models/imagedetectsettings.py +53 -0
  76. mixpeek/models/imagereadsettings.py +71 -0
  77. mixpeek/models/imagesettings.py +79 -0
  78. mixpeek/models/ingest_image_url_ingest_images_url_postop.py +59 -0
  79. mixpeek/models/ingest_text_ingest_text_postop.py +59 -0
  80. mixpeek/models/ingest_video_url_ingest_videos_url_postop.py +59 -0
  81. mixpeek/models/inputtype.py +11 -0
  82. mixpeek/models/integerindexparams.py +24 -0
  83. mixpeek/models/interactionresponse.py +87 -0
  84. mixpeek/models/interactiontype.py +11 -0
  85. mixpeek/models/jsonimageoutputsettings.py +55 -0
  86. mixpeek/models/jsontextoutputsettings.py +55 -0
  87. mixpeek/models/jsonvideooutputsettings.py +55 -0
  88. mixpeek/models/keywordindexparams.py +21 -0
  89. mixpeek/models/kill_task_tasks_task_id_deleteop.py +57 -0
  90. mixpeek/models/list_assets_assets_postop.py +77 -0
  91. mixpeek/models/list_collections_collections_getop.py +65 -0
  92. mixpeek/models/list_features_features_postop.py +79 -0
  93. mixpeek/models/list_interactions_features_search_interactions_getop.py +96 -0
  94. mixpeek/models/listassetsrequest.py +75 -0
  95. mixpeek/models/listassetsresponse.py +22 -0
  96. mixpeek/models/listcollectionsresponse.py +22 -0
  97. mixpeek/models/listfeaturesrequest.py +77 -0
  98. mixpeek/models/listfeaturesresponse.py +22 -0
  99. mixpeek/models/logicaloperator_input.py +88 -0
  100. mixpeek/models/logicaloperator_output.py +103 -0
  101. mixpeek/models/logodetectsettings.py +52 -0
  102. mixpeek/models/modality.py +13 -0
  103. mixpeek/models/modeldetails.py +57 -0
  104. mixpeek/models/namespaceresponse.py +54 -0
  105. mixpeek/models/organizationmodel.py +45 -0
  106. mixpeek/models/partial_asset_update_assets_asset_id_patchop.py +69 -0
  107. mixpeek/models/payloadindexconfig.py +94 -0
  108. mixpeek/models/payloadindextype.py +17 -0
  109. mixpeek/models/payloadschematype.py +15 -0
  110. mixpeek/models/percolaterequest.py +57 -0
  111. mixpeek/models/permission.py +10 -0
  112. mixpeek/models/processimageurlinput.py +99 -0
  113. mixpeek/models/processtextinput.py +94 -0
  114. mixpeek/models/processvideourlinput.py +99 -0
  115. mixpeek/models/querysettings.py +56 -0
  116. mixpeek/models/rerankingoptions.py +47 -0
  117. mixpeek/models/search_assets_assets_search_postop.py +59 -0
  118. mixpeek/models/search_features_features_search_postop.py +96 -0
  119. mixpeek/models/search_model_searchquery_input.py +76 -0
  120. mixpeek/models/searchassetsrequest.py +78 -0
  121. mixpeek/models/searchinteraction.py +82 -0
  122. mixpeek/models/searchquery_output.py +79 -0
  123. mixpeek/models/searchrequestfeatures_input.py +151 -0
  124. mixpeek/models/searchrequestfeatures_output.py +151 -0
  125. mixpeek/models/sortoption.py +28 -0
  126. mixpeek/models/sparseembedding.py +21 -0
  127. mixpeek/models/tasks_model_taskresponse.py +24 -0
  128. mixpeek/models/taskstatus.py +16 -0
  129. mixpeek/models/textindexparams.py +31 -0
  130. mixpeek/models/textsettings.py +61 -0
  131. mixpeek/models/tokenizertype.py +11 -0
  132. mixpeek/models/update_api_key_organizations_users_user_email_api_keys_key_name_patchop.py +30 -0
  133. mixpeek/models/update_collection_collections_collection_putop.py +74 -0
  134. mixpeek/models/update_namespace_namespaces_namespace_putop.py +28 -0
  135. mixpeek/models/updateassetrequest.py +60 -0
  136. mixpeek/models/updatenamespacerequest.py +26 -0
  137. mixpeek/models/usage.py +18 -0
  138. mixpeek/models/usermodel_input.py +36 -0
  139. mixpeek/models/usermodel_output.py +36 -0
  140. mixpeek/models/uuidindexparams.py +21 -0
  141. mixpeek/models/validationerror.py +26 -0
  142. mixpeek/models/vectormodel.py +11 -0
  143. mixpeek/models/vectortype.py +9 -0
  144. mixpeek/models/videodescribesettings.py +82 -0
  145. mixpeek/models/videodetectsettings.py +53 -0
  146. mixpeek/models/videoreadsettings.py +71 -0
  147. mixpeek/models/videosettings.py +101 -0
  148. mixpeek/models/videotranscriptionsettings.py +69 -0
  149. mixpeek/namespaces.py +1143 -0
  150. mixpeek/organizations.py +1508 -0
  151. mixpeek/py.typed +1 -0
  152. mixpeek/sdk.py +135 -0
  153. mixpeek/sdkconfiguration.py +45 -0
  154. mixpeek/searchinteractions.py +647 -0
  155. mixpeek/tasks.py +387 -0
  156. mixpeek/types/__init__.py +19 -29
  157. mixpeek/types/basemodel.py +39 -0
  158. mixpeek/utils/__init__.py +97 -0
  159. mixpeek/utils/annotations.py +55 -0
  160. mixpeek/utils/enums.py +34 -0
  161. mixpeek/utils/eventstreaming.py +238 -0
  162. mixpeek/utils/forms.py +202 -0
  163. mixpeek/utils/headers.py +136 -0
  164. mixpeek/utils/logger.py +27 -0
  165. mixpeek/utils/metadata.py +118 -0
  166. mixpeek/utils/queryparams.py +205 -0
  167. mixpeek/utils/requestbodies.py +66 -0
  168. mixpeek/utils/retries.py +217 -0
  169. mixpeek/utils/security.py +174 -0
  170. mixpeek/utils/serializers.py +215 -0
  171. mixpeek/utils/url.py +155 -0
  172. mixpeek/utils/values.py +134 -0
  173. mixpeek-0.13.dist-info/METADATA +439 -0
  174. mixpeek-0.13.dist-info/RECORD +176 -0
  175. {mixpeek-0.12.0.dist-info → mixpeek-0.13.dist-info}/WHEEL +1 -1
  176. py.typed +1 -0
  177. mixpeek/_base_client.py +0 -2041
  178. mixpeek/_client.py +0 -444
  179. mixpeek/_compat.py +0 -221
  180. mixpeek/_constants.py +0 -14
  181. mixpeek/_exceptions.py +0 -108
  182. mixpeek/_files.py +0 -123
  183. mixpeek/_models.py +0 -788
  184. mixpeek/_qs.py +0 -150
  185. mixpeek/_resource.py +0 -43
  186. mixpeek/_response.py +0 -824
  187. mixpeek/_streaming.py +0 -333
  188. mixpeek/_types.py +0 -219
  189. mixpeek/_utils/__init__.py +0 -56
  190. mixpeek/_utils/_logs.py +0 -25
  191. mixpeek/_utils/_proxy.py +0 -62
  192. mixpeek/_utils/_reflection.py +0 -42
  193. mixpeek/_utils/_streams.py +0 -12
  194. mixpeek/_utils/_sync.py +0 -81
  195. mixpeek/_utils/_transform.py +0 -387
  196. mixpeek/_utils/_typing.py +0 -120
  197. mixpeek/_utils/_utils.py +0 -414
  198. mixpeek/lib/.keep +0 -4
  199. mixpeek/resources/__init__.py +0 -117
  200. mixpeek/resources/accounts.py +0 -271
  201. mixpeek/resources/assets/__init__.py +0 -33
  202. mixpeek/resources/assets/assets.py +0 -708
  203. mixpeek/resources/assets/features.py +0 -172
  204. mixpeek/resources/collections.py +0 -283
  205. mixpeek/resources/entities/__init__.py +0 -47
  206. mixpeek/resources/entities/entities.py +0 -134
  207. mixpeek/resources/entities/faces.py +0 -514
  208. mixpeek/resources/entities/labels.py +0 -388
  209. mixpeek/resources/features/__init__.py +0 -33
  210. mixpeek/resources/features/features.py +0 -562
  211. mixpeek/resources/features/search.py +0 -731
  212. mixpeek/resources/healthcheck.py +0 -135
  213. mixpeek/resources/index/__init__.py +0 -47
  214. mixpeek/resources/index/images.py +0 -249
  215. mixpeek/resources/index/index.py +0 -289
  216. mixpeek/resources/index/videos.py +0 -249
  217. mixpeek/resources/tasks.py +0 -258
  218. mixpeek/types/account_update_params.py +0 -28
  219. mixpeek/types/asset_create_params.py +0 -36
  220. mixpeek/types/asset_create_response.py +0 -15
  221. mixpeek/types/asset_response.py +0 -46
  222. mixpeek/types/asset_search_params.py +0 -43
  223. mixpeek/types/asset_search_response.py +0 -15
  224. mixpeek/types/asset_update_params.py +0 -25
  225. mixpeek/types/asset_update_response.py +0 -11
  226. mixpeek/types/assets/__init__.py +0 -3
  227. mixpeek/types/collection_list_params.py +0 -19
  228. mixpeek/types/collection_list_response.py +0 -14
  229. mixpeek/types/entities/__init__.py +0 -13
  230. mixpeek/types/entities/face_create_params.py +0 -21
  231. mixpeek/types/entities/face_list_params.py +0 -18
  232. mixpeek/types/entities/face_list_response.py +0 -15
  233. mixpeek/types/entities/face_response.py +0 -17
  234. mixpeek/types/entities/face_update_params.py +0 -22
  235. mixpeek/types/entities/label_list_params.py +0 -18
  236. mixpeek/types/entities/label_list_response.py +0 -30
  237. mixpeek/types/entities/label_response.py +0 -30
  238. mixpeek/types/entities/label_update_params.py +0 -21
  239. mixpeek/types/feature.py +0 -32
  240. mixpeek/types/feature_list_params.py +0 -37
  241. mixpeek/types/feature_list_response.py +0 -28
  242. mixpeek/types/feature_retrieve_params.py +0 -18
  243. mixpeek/types/feature_update_params.py +0 -16
  244. mixpeek/types/features/__init__.py +0 -8
  245. mixpeek/types/features/search_feedback_params.py +0 -23
  246. mixpeek/types/features/search_file_params.py +0 -29
  247. mixpeek/types/features/search_text_params.py +0 -72
  248. mixpeek/types/features/search_url_params.py +0 -72
  249. mixpeek/types/grouped_asset_data.py +0 -24
  250. mixpeek/types/health_check_response.py +0 -10
  251. mixpeek/types/index/__init__.py +0 -8
  252. mixpeek/types/index/image_url_params.py +0 -135
  253. mixpeek/types/index/image_url_response.py +0 -20
  254. mixpeek/types/index/video_url_params.py +0 -154
  255. mixpeek/types/index/video_url_response.py +0 -20
  256. mixpeek/types/index_text_params.py +0 -77
  257. mixpeek/types/index_text_response.py +0 -14
  258. mixpeek/types/shared/__init__.py +0 -7
  259. mixpeek/types/shared/filter_condition.py +0 -19
  260. mixpeek/types/shared/logical_operator.py +0 -12
  261. mixpeek/types/shared/model_pagination_response.py +0 -21
  262. mixpeek/types/shared/sort_option.py +0 -15
  263. mixpeek/types/shared/task_response.py +0 -15
  264. mixpeek/types/shared_params/__init__.py +0 -4
  265. mixpeek/types/shared_params/logical_operator.py +0 -13
  266. mixpeek/types/shared_params/sort_option.py +0 -15
  267. mixpeek/types/user.py +0 -34
  268. mixpeek-0.12.0.dist-info/METADATA +0 -345
  269. mixpeek-0.12.0.dist-info/RECORD +0 -99
  270. mixpeek-0.12.0.dist-info/licenses/LICENSE +0 -201
mixpeek/_models.py DELETED
@@ -1,788 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import os
4
- import inspect
5
- from typing import TYPE_CHECKING, Any, Type, Union, Generic, TypeVar, Callable, cast
6
- from datetime import date, datetime
7
- from typing_extensions import (
8
- Unpack,
9
- Literal,
10
- ClassVar,
11
- Protocol,
12
- Required,
13
- ParamSpec,
14
- TypedDict,
15
- TypeGuard,
16
- final,
17
- override,
18
- runtime_checkable,
19
- )
20
-
21
- import pydantic
22
- import pydantic.generics
23
- from pydantic.fields import FieldInfo
24
-
25
- from ._types import (
26
- Body,
27
- IncEx,
28
- Query,
29
- ModelT,
30
- Headers,
31
- Timeout,
32
- NotGiven,
33
- AnyMapping,
34
- HttpxRequestFiles,
35
- )
36
- from ._utils import (
37
- PropertyInfo,
38
- is_list,
39
- is_given,
40
- json_safe,
41
- lru_cache,
42
- is_mapping,
43
- parse_date,
44
- coerce_boolean,
45
- parse_datetime,
46
- strip_not_given,
47
- extract_type_arg,
48
- is_annotated_type,
49
- strip_annotated_type,
50
- )
51
- from ._compat import (
52
- PYDANTIC_V2,
53
- ConfigDict,
54
- GenericModel as BaseGenericModel,
55
- get_args,
56
- is_union,
57
- parse_obj,
58
- get_origin,
59
- is_literal_type,
60
- get_model_config,
61
- get_model_fields,
62
- field_get_default,
63
- )
64
- from ._constants import RAW_RESPONSE_HEADER
65
-
66
- if TYPE_CHECKING:
67
- from pydantic_core.core_schema import ModelField, LiteralSchema, ModelFieldsSchema
68
-
69
- __all__ = ["BaseModel", "GenericModel"]
70
-
71
- _T = TypeVar("_T")
72
- _BaseModelT = TypeVar("_BaseModelT", bound="BaseModel")
73
-
74
- P = ParamSpec("P")
75
-
76
-
77
- @runtime_checkable
78
- class _ConfigProtocol(Protocol):
79
- allow_population_by_field_name: bool
80
-
81
-
82
- class BaseModel(pydantic.BaseModel):
83
- if PYDANTIC_V2:
84
- model_config: ClassVar[ConfigDict] = ConfigDict(
85
- extra="allow", defer_build=coerce_boolean(os.environ.get("DEFER_PYDANTIC_BUILD", "true"))
86
- )
87
- else:
88
-
89
- @property
90
- @override
91
- def model_fields_set(self) -> set[str]:
92
- # a forwards-compat shim for pydantic v2
93
- return self.__fields_set__ # type: ignore
94
-
95
- class Config(pydantic.BaseConfig): # pyright: ignore[reportDeprecated]
96
- extra: Any = pydantic.Extra.allow # type: ignore
97
-
98
- def to_dict(
99
- self,
100
- *,
101
- mode: Literal["json", "python"] = "python",
102
- use_api_names: bool = True,
103
- exclude_unset: bool = True,
104
- exclude_defaults: bool = False,
105
- exclude_none: bool = False,
106
- warnings: bool = True,
107
- ) -> dict[str, object]:
108
- """Recursively generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
109
-
110
- By default, fields that were not set by the API will not be included,
111
- and keys will match the API response, *not* the property names from the model.
112
-
113
- For example, if the API responds with `"fooBar": true` but we've defined a `foo_bar: bool` property,
114
- the output will use the `"fooBar"` key (unless `use_api_names=False` is passed).
115
-
116
- Args:
117
- mode:
118
- If mode is 'json', the dictionary will only contain JSON serializable types. e.g. `datetime` will be turned into a string, `"2024-3-22T18:11:19.117000Z"`.
119
- If mode is 'python', the dictionary may contain any Python objects. e.g. `datetime(2024, 3, 22)`
120
-
121
- use_api_names: Whether to use the key that the API responded with or the property name. Defaults to `True`.
122
- exclude_unset: Whether to exclude fields that have not been explicitly set.
123
- exclude_defaults: Whether to exclude fields that are set to their default value from the output.
124
- exclude_none: Whether to exclude fields that have a value of `None` from the output.
125
- warnings: Whether to log warnings when invalid fields are encountered. This is only supported in Pydantic v2.
126
- """
127
- return self.model_dump(
128
- mode=mode,
129
- by_alias=use_api_names,
130
- exclude_unset=exclude_unset,
131
- exclude_defaults=exclude_defaults,
132
- exclude_none=exclude_none,
133
- warnings=warnings,
134
- )
135
-
136
- def to_json(
137
- self,
138
- *,
139
- indent: int | None = 2,
140
- use_api_names: bool = True,
141
- exclude_unset: bool = True,
142
- exclude_defaults: bool = False,
143
- exclude_none: bool = False,
144
- warnings: bool = True,
145
- ) -> str:
146
- """Generates a JSON string representing this model as it would be received from or sent to the API (but with indentation).
147
-
148
- By default, fields that were not set by the API will not be included,
149
- and keys will match the API response, *not* the property names from the model.
150
-
151
- For example, if the API responds with `"fooBar": true` but we've defined a `foo_bar: bool` property,
152
- the output will use the `"fooBar"` key (unless `use_api_names=False` is passed).
153
-
154
- Args:
155
- indent: Indentation to use in the JSON output. If `None` is passed, the output will be compact. Defaults to `2`
156
- use_api_names: Whether to use the key that the API responded with or the property name. Defaults to `True`.
157
- exclude_unset: Whether to exclude fields that have not been explicitly set.
158
- exclude_defaults: Whether to exclude fields that have the default value.
159
- exclude_none: Whether to exclude fields that have a value of `None`.
160
- warnings: Whether to show any warnings that occurred during serialization. This is only supported in Pydantic v2.
161
- """
162
- return self.model_dump_json(
163
- indent=indent,
164
- by_alias=use_api_names,
165
- exclude_unset=exclude_unset,
166
- exclude_defaults=exclude_defaults,
167
- exclude_none=exclude_none,
168
- warnings=warnings,
169
- )
170
-
171
- @override
172
- def __str__(self) -> str:
173
- # mypy complains about an invalid self arg
174
- return f'{self.__repr_name__()}({self.__repr_str__(", ")})' # type: ignore[misc]
175
-
176
- # Override the 'construct' method in a way that supports recursive parsing without validation.
177
- # Based on https://github.com/samuelcolvin/pydantic/issues/1168#issuecomment-817742836.
178
- @classmethod
179
- @override
180
- def construct( # pyright: ignore[reportIncompatibleMethodOverride]
181
- cls: Type[ModelT],
182
- _fields_set: set[str] | None = None,
183
- **values: object,
184
- ) -> ModelT:
185
- m = cls.__new__(cls)
186
- fields_values: dict[str, object] = {}
187
-
188
- config = get_model_config(cls)
189
- populate_by_name = (
190
- config.allow_population_by_field_name
191
- if isinstance(config, _ConfigProtocol)
192
- else config.get("populate_by_name")
193
- )
194
-
195
- if _fields_set is None:
196
- _fields_set = set()
197
-
198
- model_fields = get_model_fields(cls)
199
- for name, field in model_fields.items():
200
- key = field.alias
201
- if key is None or (key not in values and populate_by_name):
202
- key = name
203
-
204
- if key in values:
205
- fields_values[name] = _construct_field(value=values[key], field=field, key=key)
206
- _fields_set.add(name)
207
- else:
208
- fields_values[name] = field_get_default(field)
209
-
210
- _extra = {}
211
- for key, value in values.items():
212
- if key not in model_fields:
213
- if PYDANTIC_V2:
214
- _extra[key] = value
215
- else:
216
- _fields_set.add(key)
217
- fields_values[key] = value
218
-
219
- object.__setattr__(m, "__dict__", fields_values)
220
-
221
- if PYDANTIC_V2:
222
- # these properties are copied from Pydantic's `model_construct()` method
223
- object.__setattr__(m, "__pydantic_private__", None)
224
- object.__setattr__(m, "__pydantic_extra__", _extra)
225
- object.__setattr__(m, "__pydantic_fields_set__", _fields_set)
226
- else:
227
- # init_private_attributes() does not exist in v2
228
- m._init_private_attributes() # type: ignore
229
-
230
- # copied from Pydantic v1's `construct()` method
231
- object.__setattr__(m, "__fields_set__", _fields_set)
232
-
233
- return m
234
-
235
- if not TYPE_CHECKING:
236
- # type checkers incorrectly complain about this assignment
237
- # because the type signatures are technically different
238
- # although not in practice
239
- model_construct = construct
240
-
241
- if not PYDANTIC_V2:
242
- # we define aliases for some of the new pydantic v2 methods so
243
- # that we can just document these methods without having to specify
244
- # a specific pydantic version as some users may not know which
245
- # pydantic version they are currently using
246
-
247
- @override
248
- def model_dump(
249
- self,
250
- *,
251
- mode: Literal["json", "python"] | str = "python",
252
- include: IncEx | None = None,
253
- exclude: IncEx | None = None,
254
- by_alias: bool = False,
255
- exclude_unset: bool = False,
256
- exclude_defaults: bool = False,
257
- exclude_none: bool = False,
258
- round_trip: bool = False,
259
- warnings: bool | Literal["none", "warn", "error"] = True,
260
- context: dict[str, Any] | None = None,
261
- serialize_as_any: bool = False,
262
- ) -> dict[str, Any]:
263
- """Usage docs: https://docs.pydantic.dev/2.4/concepts/serialization/#modelmodel_dump
264
-
265
- Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
266
-
267
- Args:
268
- mode: The mode in which `to_python` should run.
269
- If mode is 'json', the dictionary will only contain JSON serializable types.
270
- If mode is 'python', the dictionary may contain any Python objects.
271
- include: A list of fields to include in the output.
272
- exclude: A list of fields to exclude from the output.
273
- by_alias: Whether to use the field's alias in the dictionary key if defined.
274
- exclude_unset: Whether to exclude fields that are unset or None from the output.
275
- exclude_defaults: Whether to exclude fields that are set to their default value from the output.
276
- exclude_none: Whether to exclude fields that have a value of `None` from the output.
277
- round_trip: Whether to enable serialization and deserialization round-trip support.
278
- warnings: Whether to log warnings when invalid fields are encountered.
279
-
280
- Returns:
281
- A dictionary representation of the model.
282
- """
283
- if mode not in {"json", "python"}:
284
- raise ValueError("mode must be either 'json' or 'python'")
285
- if round_trip != False:
286
- raise ValueError("round_trip is only supported in Pydantic v2")
287
- if warnings != True:
288
- raise ValueError("warnings is only supported in Pydantic v2")
289
- if context is not None:
290
- raise ValueError("context is only supported in Pydantic v2")
291
- if serialize_as_any != False:
292
- raise ValueError("serialize_as_any is only supported in Pydantic v2")
293
- dumped = super().dict( # pyright: ignore[reportDeprecated]
294
- include=include,
295
- exclude=exclude,
296
- by_alias=by_alias,
297
- exclude_unset=exclude_unset,
298
- exclude_defaults=exclude_defaults,
299
- exclude_none=exclude_none,
300
- )
301
-
302
- return cast(dict[str, Any], json_safe(dumped)) if mode == "json" else dumped
303
-
304
- @override
305
- def model_dump_json(
306
- self,
307
- *,
308
- indent: int | None = None,
309
- include: IncEx | None = None,
310
- exclude: IncEx | None = None,
311
- by_alias: bool = False,
312
- exclude_unset: bool = False,
313
- exclude_defaults: bool = False,
314
- exclude_none: bool = False,
315
- round_trip: bool = False,
316
- warnings: bool | Literal["none", "warn", "error"] = True,
317
- context: dict[str, Any] | None = None,
318
- serialize_as_any: bool = False,
319
- ) -> str:
320
- """Usage docs: https://docs.pydantic.dev/2.4/concepts/serialization/#modelmodel_dump_json
321
-
322
- Generates a JSON representation of the model using Pydantic's `to_json` method.
323
-
324
- Args:
325
- indent: Indentation to use in the JSON output. If None is passed, the output will be compact.
326
- include: Field(s) to include in the JSON output. Can take either a string or set of strings.
327
- exclude: Field(s) to exclude from the JSON output. Can take either a string or set of strings.
328
- by_alias: Whether to serialize using field aliases.
329
- exclude_unset: Whether to exclude fields that have not been explicitly set.
330
- exclude_defaults: Whether to exclude fields that have the default value.
331
- exclude_none: Whether to exclude fields that have a value of `None`.
332
- round_trip: Whether to use serialization/deserialization between JSON and class instance.
333
- warnings: Whether to show any warnings that occurred during serialization.
334
-
335
- Returns:
336
- A JSON string representation of the model.
337
- """
338
- if round_trip != False:
339
- raise ValueError("round_trip is only supported in Pydantic v2")
340
- if warnings != True:
341
- raise ValueError("warnings is only supported in Pydantic v2")
342
- if context is not None:
343
- raise ValueError("context is only supported in Pydantic v2")
344
- if serialize_as_any != False:
345
- raise ValueError("serialize_as_any is only supported in Pydantic v2")
346
- return super().json( # type: ignore[reportDeprecated]
347
- indent=indent,
348
- include=include,
349
- exclude=exclude,
350
- by_alias=by_alias,
351
- exclude_unset=exclude_unset,
352
- exclude_defaults=exclude_defaults,
353
- exclude_none=exclude_none,
354
- )
355
-
356
-
357
- def _construct_field(value: object, field: FieldInfo, key: str) -> object:
358
- if value is None:
359
- return field_get_default(field)
360
-
361
- if PYDANTIC_V2:
362
- type_ = field.annotation
363
- else:
364
- type_ = cast(type, field.outer_type_) # type: ignore
365
-
366
- if type_ is None:
367
- raise RuntimeError(f"Unexpected field type is None for {key}")
368
-
369
- return construct_type(value=value, type_=type_)
370
-
371
-
372
- def is_basemodel(type_: type) -> bool:
373
- """Returns whether or not the given type is either a `BaseModel` or a union of `BaseModel`"""
374
- if is_union(type_):
375
- for variant in get_args(type_):
376
- if is_basemodel(variant):
377
- return True
378
-
379
- return False
380
-
381
- return is_basemodel_type(type_)
382
-
383
-
384
- def is_basemodel_type(type_: type) -> TypeGuard[type[BaseModel] | type[GenericModel]]:
385
- origin = get_origin(type_) or type_
386
- if not inspect.isclass(origin):
387
- return False
388
- return issubclass(origin, BaseModel) or issubclass(origin, GenericModel)
389
-
390
-
391
- def build(
392
- base_model_cls: Callable[P, _BaseModelT],
393
- *args: P.args,
394
- **kwargs: P.kwargs,
395
- ) -> _BaseModelT:
396
- """Construct a BaseModel class without validation.
397
-
398
- This is useful for cases where you need to instantiate a `BaseModel`
399
- from an API response as this provides type-safe params which isn't supported
400
- by helpers like `construct_type()`.
401
-
402
- ```py
403
- build(MyModel, my_field_a="foo", my_field_b=123)
404
- ```
405
- """
406
- if args:
407
- raise TypeError(
408
- "Received positional arguments which are not supported; Keyword arguments must be used instead",
409
- )
410
-
411
- return cast(_BaseModelT, construct_type(type_=base_model_cls, value=kwargs))
412
-
413
-
414
- def construct_type_unchecked(*, value: object, type_: type[_T]) -> _T:
415
- """Loose coercion to the expected type with construction of nested values.
416
-
417
- Note: the returned value from this function is not guaranteed to match the
418
- given type.
419
- """
420
- return cast(_T, construct_type(value=value, type_=type_))
421
-
422
-
423
- def construct_type(*, value: object, type_: object) -> object:
424
- """Loose coercion to the expected type with construction of nested values.
425
-
426
- If the given value does not match the expected type then it is returned as-is.
427
- """
428
- # we allow `object` as the input type because otherwise, passing things like
429
- # `Literal['value']` will be reported as a type error by type checkers
430
- type_ = cast("type[object]", type_)
431
-
432
- # unwrap `Annotated[T, ...]` -> `T`
433
- if is_annotated_type(type_):
434
- meta: tuple[Any, ...] = get_args(type_)[1:]
435
- type_ = extract_type_arg(type_, 0)
436
- else:
437
- meta = tuple()
438
-
439
- # we need to use the origin class for any types that are subscripted generics
440
- # e.g. Dict[str, object]
441
- origin = get_origin(type_) or type_
442
- args = get_args(type_)
443
-
444
- if is_union(origin):
445
- try:
446
- return validate_type(type_=cast("type[object]", type_), value=value)
447
- except Exception:
448
- pass
449
-
450
- # if the type is a discriminated union then we want to construct the right variant
451
- # in the union, even if the data doesn't match exactly, otherwise we'd break code
452
- # that relies on the constructed class types, e.g.
453
- #
454
- # class FooType:
455
- # kind: Literal['foo']
456
- # value: str
457
- #
458
- # class BarType:
459
- # kind: Literal['bar']
460
- # value: int
461
- #
462
- # without this block, if the data we get is something like `{'kind': 'bar', 'value': 'foo'}` then
463
- # we'd end up constructing `FooType` when it should be `BarType`.
464
- discriminator = _build_discriminated_union_meta(union=type_, meta_annotations=meta)
465
- if discriminator and is_mapping(value):
466
- variant_value = value.get(discriminator.field_alias_from or discriminator.field_name)
467
- if variant_value and isinstance(variant_value, str):
468
- variant_type = discriminator.mapping.get(variant_value)
469
- if variant_type:
470
- return construct_type(type_=variant_type, value=value)
471
-
472
- # if the data is not valid, use the first variant that doesn't fail while deserializing
473
- for variant in args:
474
- try:
475
- return construct_type(value=value, type_=variant)
476
- except Exception:
477
- continue
478
-
479
- raise RuntimeError(f"Could not convert data into a valid instance of {type_}")
480
-
481
- if origin == dict:
482
- if not is_mapping(value):
483
- return value
484
-
485
- _, items_type = get_args(type_) # Dict[_, items_type]
486
- return {key: construct_type(value=item, type_=items_type) for key, item in value.items()}
487
-
488
- if not is_literal_type(type_) and (issubclass(origin, BaseModel) or issubclass(origin, GenericModel)):
489
- if is_list(value):
490
- return [cast(Any, type_).construct(**entry) if is_mapping(entry) else entry for entry in value]
491
-
492
- if is_mapping(value):
493
- if issubclass(type_, BaseModel):
494
- return type_.construct(**value) # type: ignore[arg-type]
495
-
496
- return cast(Any, type_).construct(**value)
497
-
498
- if origin == list:
499
- if not is_list(value):
500
- return value
501
-
502
- inner_type = args[0] # List[inner_type]
503
- return [construct_type(value=entry, type_=inner_type) for entry in value]
504
-
505
- if origin == float:
506
- if isinstance(value, int):
507
- coerced = float(value)
508
- if coerced != value:
509
- return value
510
- return coerced
511
-
512
- return value
513
-
514
- if type_ == datetime:
515
- try:
516
- return parse_datetime(value) # type: ignore
517
- except Exception:
518
- return value
519
-
520
- if type_ == date:
521
- try:
522
- return parse_date(value) # type: ignore
523
- except Exception:
524
- return value
525
-
526
- return value
527
-
528
-
529
- @runtime_checkable
530
- class CachedDiscriminatorType(Protocol):
531
- __discriminator__: DiscriminatorDetails
532
-
533
-
534
- class DiscriminatorDetails:
535
- field_name: str
536
- """The name of the discriminator field in the variant class, e.g.
537
-
538
- ```py
539
- class Foo(BaseModel):
540
- type: Literal['foo']
541
- ```
542
-
543
- Will result in field_name='type'
544
- """
545
-
546
- field_alias_from: str | None
547
- """The name of the discriminator field in the API response, e.g.
548
-
549
- ```py
550
- class Foo(BaseModel):
551
- type: Literal['foo'] = Field(alias='type_from_api')
552
- ```
553
-
554
- Will result in field_alias_from='type_from_api'
555
- """
556
-
557
- mapping: dict[str, type]
558
- """Mapping of discriminator value to variant type, e.g.
559
-
560
- {'foo': FooVariant, 'bar': BarVariant}
561
- """
562
-
563
- def __init__(
564
- self,
565
- *,
566
- mapping: dict[str, type],
567
- discriminator_field: str,
568
- discriminator_alias: str | None,
569
- ) -> None:
570
- self.mapping = mapping
571
- self.field_name = discriminator_field
572
- self.field_alias_from = discriminator_alias
573
-
574
-
575
- def _build_discriminated_union_meta(*, union: type, meta_annotations: tuple[Any, ...]) -> DiscriminatorDetails | None:
576
- if isinstance(union, CachedDiscriminatorType):
577
- return union.__discriminator__
578
-
579
- discriminator_field_name: str | None = None
580
-
581
- for annotation in meta_annotations:
582
- if isinstance(annotation, PropertyInfo) and annotation.discriminator is not None:
583
- discriminator_field_name = annotation.discriminator
584
- break
585
-
586
- if not discriminator_field_name:
587
- return None
588
-
589
- mapping: dict[str, type] = {}
590
- discriminator_alias: str | None = None
591
-
592
- for variant in get_args(union):
593
- variant = strip_annotated_type(variant)
594
- if is_basemodel_type(variant):
595
- if PYDANTIC_V2:
596
- field = _extract_field_schema_pv2(variant, discriminator_field_name)
597
- if not field:
598
- continue
599
-
600
- # Note: if one variant defines an alias then they all should
601
- discriminator_alias = field.get("serialization_alias")
602
-
603
- field_schema = field["schema"]
604
-
605
- if field_schema["type"] == "literal":
606
- for entry in cast("LiteralSchema", field_schema)["expected"]:
607
- if isinstance(entry, str):
608
- mapping[entry] = variant
609
- else:
610
- field_info = cast("dict[str, FieldInfo]", variant.__fields__).get(discriminator_field_name) # pyright: ignore[reportDeprecated, reportUnnecessaryCast]
611
- if not field_info:
612
- continue
613
-
614
- # Note: if one variant defines an alias then they all should
615
- discriminator_alias = field_info.alias
616
-
617
- if field_info.annotation and is_literal_type(field_info.annotation):
618
- for entry in get_args(field_info.annotation):
619
- if isinstance(entry, str):
620
- mapping[entry] = variant
621
-
622
- if not mapping:
623
- return None
624
-
625
- details = DiscriminatorDetails(
626
- mapping=mapping,
627
- discriminator_field=discriminator_field_name,
628
- discriminator_alias=discriminator_alias,
629
- )
630
- cast(CachedDiscriminatorType, union).__discriminator__ = details
631
- return details
632
-
633
-
634
- def _extract_field_schema_pv2(model: type[BaseModel], field_name: str) -> ModelField | None:
635
- schema = model.__pydantic_core_schema__
636
- if schema["type"] != "model":
637
- return None
638
-
639
- fields_schema = schema["schema"]
640
- if fields_schema["type"] != "model-fields":
641
- return None
642
-
643
- fields_schema = cast("ModelFieldsSchema", fields_schema)
644
-
645
- field = fields_schema["fields"].get(field_name)
646
- if not field:
647
- return None
648
-
649
- return cast("ModelField", field) # pyright: ignore[reportUnnecessaryCast]
650
-
651
-
652
- def validate_type(*, type_: type[_T], value: object) -> _T:
653
- """Strict validation that the given value matches the expected type"""
654
- if inspect.isclass(type_) and issubclass(type_, pydantic.BaseModel):
655
- return cast(_T, parse_obj(type_, value))
656
-
657
- return cast(_T, _validate_non_model_type(type_=type_, value=value))
658
-
659
-
660
- def set_pydantic_config(typ: Any, config: pydantic.ConfigDict) -> None:
661
- """Add a pydantic config for the given type.
662
-
663
- Note: this is a no-op on Pydantic v1.
664
- """
665
- setattr(typ, "__pydantic_config__", config) # noqa: B010
666
-
667
-
668
- # our use of subclasssing here causes weirdness for type checkers,
669
- # so we just pretend that we don't subclass
670
- if TYPE_CHECKING:
671
- GenericModel = BaseModel
672
- else:
673
-
674
- class GenericModel(BaseGenericModel, BaseModel):
675
- pass
676
-
677
-
678
- if PYDANTIC_V2:
679
- from pydantic import TypeAdapter as _TypeAdapter
680
-
681
- _CachedTypeAdapter = cast("TypeAdapter[object]", lru_cache(maxsize=None)(_TypeAdapter))
682
-
683
- if TYPE_CHECKING:
684
- from pydantic import TypeAdapter
685
- else:
686
- TypeAdapter = _CachedTypeAdapter
687
-
688
- def _validate_non_model_type(*, type_: type[_T], value: object) -> _T:
689
- return TypeAdapter(type_).validate_python(value)
690
-
691
- elif not TYPE_CHECKING: # TODO: condition is weird
692
-
693
- class RootModel(GenericModel, Generic[_T]):
694
- """Used as a placeholder to easily convert runtime types to a Pydantic format
695
- to provide validation.
696
-
697
- For example:
698
- ```py
699
- validated = RootModel[int](__root__="5").__root__
700
- # validated: 5
701
- ```
702
- """
703
-
704
- __root__: _T
705
-
706
- def _validate_non_model_type(*, type_: type[_T], value: object) -> _T:
707
- model = _create_pydantic_model(type_).validate(value)
708
- return cast(_T, model.__root__)
709
-
710
- def _create_pydantic_model(type_: _T) -> Type[RootModel[_T]]:
711
- return RootModel[type_] # type: ignore
712
-
713
-
714
- class FinalRequestOptionsInput(TypedDict, total=False):
715
- method: Required[str]
716
- url: Required[str]
717
- params: Query
718
- headers: Headers
719
- max_retries: int
720
- timeout: float | Timeout | None
721
- files: HttpxRequestFiles | None
722
- idempotency_key: str
723
- json_data: Body
724
- extra_json: AnyMapping
725
-
726
-
727
- @final
728
- class FinalRequestOptions(pydantic.BaseModel):
729
- method: str
730
- url: str
731
- params: Query = {}
732
- headers: Union[Headers, NotGiven] = NotGiven()
733
- max_retries: Union[int, NotGiven] = NotGiven()
734
- timeout: Union[float, Timeout, None, NotGiven] = NotGiven()
735
- files: Union[HttpxRequestFiles, None] = None
736
- idempotency_key: Union[str, None] = None
737
- post_parser: Union[Callable[[Any], Any], NotGiven] = NotGiven()
738
-
739
- # It should be noted that we cannot use `json` here as that would override
740
- # a BaseModel method in an incompatible fashion.
741
- json_data: Union[Body, None] = None
742
- extra_json: Union[AnyMapping, None] = None
743
-
744
- if PYDANTIC_V2:
745
- model_config: ClassVar[ConfigDict] = ConfigDict(arbitrary_types_allowed=True)
746
- else:
747
-
748
- class Config(pydantic.BaseConfig): # pyright: ignore[reportDeprecated]
749
- arbitrary_types_allowed: bool = True
750
-
751
- def get_max_retries(self, max_retries: int) -> int:
752
- if isinstance(self.max_retries, NotGiven):
753
- return max_retries
754
- return self.max_retries
755
-
756
- def _strip_raw_response_header(self) -> None:
757
- if not is_given(self.headers):
758
- return
759
-
760
- if self.headers.get(RAW_RESPONSE_HEADER):
761
- self.headers = {**self.headers}
762
- self.headers.pop(RAW_RESPONSE_HEADER)
763
-
764
- # override the `construct` method so that we can run custom transformations.
765
- # this is necessary as we don't want to do any actual runtime type checking
766
- # (which means we can't use validators) but we do want to ensure that `NotGiven`
767
- # values are not present
768
- #
769
- # type ignore required because we're adding explicit types to `**values`
770
- @classmethod
771
- def construct( # type: ignore
772
- cls,
773
- _fields_set: set[str] | None = None,
774
- **values: Unpack[FinalRequestOptionsInput],
775
- ) -> FinalRequestOptions:
776
- kwargs: dict[str, Any] = {
777
- # we unconditionally call `strip_not_given` on any value
778
- # as it will just ignore any non-mapping types
779
- key: strip_not_given(value)
780
- for key, value in values.items()
781
- }
782
- if PYDANTIC_V2:
783
- return super().model_construct(_fields_set, **kwargs)
784
- return cast(FinalRequestOptions, super().construct(_fields_set, **kwargs)) # pyright: ignore[reportDeprecated]
785
-
786
- if not TYPE_CHECKING:
787
- # type checkers incorrectly complain about this assignment
788
- model_construct = construct