label-studio-sdk 1.0.7__py3-none-any.whl → 1.0.10__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.
Potentially problematic release.
This version of label-studio-sdk might be problematic. Click here for more details.
- label_studio_sdk/__init__.py +26 -7
- label_studio_sdk/_extensions/label_studio_tools/core/label_config.py +13 -4
- label_studio_sdk/_extensions/label_studio_tools/core/utils/io.py +16 -4
- label_studio_sdk/_extensions/label_studio_tools/core/utils/json_schema.py +86 -0
- label_studio_sdk/_extensions/pager_ext.py +8 -0
- label_studio_sdk/_legacy/schema/label_config_schema.json +42 -11
- label_studio_sdk/actions/client.py +91 -40
- label_studio_sdk/actions/types/actions_create_request_filters.py +14 -24
- label_studio_sdk/actions/types/actions_create_request_filters_items_item.py +16 -26
- label_studio_sdk/actions/types/actions_create_request_filters_items_item_value.py +3 -1
- label_studio_sdk/actions/types/actions_create_request_selected_items.py +1 -2
- label_studio_sdk/actions/types/actions_create_request_selected_items_excluded.py +15 -25
- label_studio_sdk/actions/types/actions_create_request_selected_items_included.py +15 -25
- label_studio_sdk/annotations/__init__.py +2 -2
- label_studio_sdk/annotations/client.py +278 -104
- label_studio_sdk/annotations/types/__init__.py +2 -1
- label_studio_sdk/annotations/types/annotations_create_bulk_request_selected_items.py +34 -0
- label_studio_sdk/annotations/types/annotations_create_bulk_response_item.py +11 -21
- label_studio_sdk/base_client.py +46 -27
- label_studio_sdk/client.py +1 -0
- label_studio_sdk/comments/client.py +190 -44
- label_studio_sdk/converter/converter.py +66 -18
- label_studio_sdk/converter/imports/yolo.py +1 -1
- label_studio_sdk/converter/utils.py +3 -2
- label_studio_sdk/core/__init__.py +21 -4
- label_studio_sdk/core/client_wrapper.py +9 -10
- label_studio_sdk/core/file.py +37 -8
- label_studio_sdk/core/http_client.py +52 -28
- label_studio_sdk/core/jsonable_encoder.py +33 -31
- label_studio_sdk/core/pagination.py +5 -4
- label_studio_sdk/core/pydantic_utilities.py +272 -4
- label_studio_sdk/core/query_encoder.py +38 -13
- label_studio_sdk/core/request_options.py +3 -0
- label_studio_sdk/core/serialization.py +272 -0
- label_studio_sdk/errors/bad_request_error.py +2 -3
- label_studio_sdk/export_storage/azure/client.py +228 -58
- label_studio_sdk/export_storage/azure/types/azure_create_response.py +19 -29
- label_studio_sdk/export_storage/azure/types/azure_update_response.py +19 -29
- label_studio_sdk/export_storage/client.py +48 -18
- label_studio_sdk/export_storage/gcs/client.py +228 -58
- label_studio_sdk/export_storage/gcs/types/gcs_create_response.py +19 -29
- label_studio_sdk/export_storage/gcs/types/gcs_update_response.py +19 -29
- label_studio_sdk/export_storage/local/client.py +222 -56
- label_studio_sdk/export_storage/local/types/local_create_response.py +17 -27
- label_studio_sdk/export_storage/local/types/local_update_response.py +17 -27
- label_studio_sdk/export_storage/redis/client.py +228 -58
- label_studio_sdk/export_storage/redis/types/redis_create_response.py +20 -30
- label_studio_sdk/export_storage/redis/types/redis_update_response.py +20 -30
- label_studio_sdk/export_storage/s3/client.py +228 -58
- label_studio_sdk/export_storage/s3/types/s3create_response.py +27 -35
- label_studio_sdk/export_storage/s3/types/s3update_response.py +27 -35
- label_studio_sdk/export_storage/s3s/client.py +187 -43
- label_studio_sdk/export_storage/types/export_storage_list_types_response_item.py +11 -21
- label_studio_sdk/files/client.py +172 -56
- label_studio_sdk/import_storage/azure/client.py +223 -53
- label_studio_sdk/import_storage/azure/types/azure_create_response.py +22 -32
- label_studio_sdk/import_storage/azure/types/azure_update_response.py +22 -32
- label_studio_sdk/import_storage/client.py +48 -18
- label_studio_sdk/import_storage/gcs/client.py +223 -53
- label_studio_sdk/import_storage/gcs/types/gcs_create_response.py +22 -32
- label_studio_sdk/import_storage/gcs/types/gcs_update_response.py +22 -32
- label_studio_sdk/import_storage/local/client.py +223 -53
- label_studio_sdk/import_storage/local/types/local_create_response.py +17 -27
- label_studio_sdk/import_storage/local/types/local_update_response.py +17 -27
- label_studio_sdk/import_storage/redis/client.py +223 -53
- label_studio_sdk/import_storage/redis/types/redis_create_response.py +20 -30
- label_studio_sdk/import_storage/redis/types/redis_update_response.py +20 -30
- label_studio_sdk/import_storage/s3/client.py +223 -53
- label_studio_sdk/import_storage/s3/types/s3create_response.py +31 -39
- label_studio_sdk/import_storage/s3/types/s3update_response.py +31 -39
- label_studio_sdk/import_storage/s3s/client.py +222 -52
- label_studio_sdk/import_storage/types/import_storage_list_types_response_item.py +11 -21
- label_studio_sdk/label_interface/control_tags.py +205 -10
- label_studio_sdk/label_interface/interface.py +80 -6
- label_studio_sdk/label_interface/region.py +1 -10
- label_studio_sdk/ml/client.py +280 -78
- label_studio_sdk/ml/types/ml_create_response.py +21 -31
- label_studio_sdk/ml/types/ml_update_response.py +21 -31
- label_studio_sdk/model_providers/client.py +656 -21
- label_studio_sdk/predictions/client.py +247 -101
- label_studio_sdk/projects/__init__.py +3 -0
- label_studio_sdk/projects/client.py +309 -115
- label_studio_sdk/projects/client_ext.py +16 -0
- label_studio_sdk/projects/exports/__init__.py +3 -0
- label_studio_sdk/projects/exports/client.py +447 -296
- label_studio_sdk/projects/exports/client_ext.py +134 -0
- label_studio_sdk/projects/exports/types/__init__.py +6 -0
- label_studio_sdk/projects/exports/types/exports_convert_response.py +24 -0
- label_studio_sdk/projects/exports/types/exports_list_formats_response_item.py +44 -0
- label_studio_sdk/projects/types/projects_create_response.py +29 -34
- label_studio_sdk/projects/types/projects_import_tasks_response.py +19 -29
- label_studio_sdk/projects/types/projects_list_response.py +11 -21
- label_studio_sdk/projects/types/projects_update_response.py +24 -34
- label_studio_sdk/prompts/client.py +309 -92
- label_studio_sdk/prompts/indicators/client.py +67 -23
- label_studio_sdk/prompts/runs/client.py +95 -40
- label_studio_sdk/prompts/types/prompts_batch_failed_predictions_request_failed_predictions_item.py +14 -24
- label_studio_sdk/prompts/types/prompts_batch_failed_predictions_response.py +11 -21
- label_studio_sdk/prompts/types/prompts_batch_predictions_request_results_item.py +26 -29
- label_studio_sdk/prompts/types/prompts_batch_predictions_response.py +11 -21
- label_studio_sdk/prompts/versions/client.py +389 -75
- label_studio_sdk/tasks/client.py +263 -90
- label_studio_sdk/tasks/types/tasks_list_response.py +15 -25
- label_studio_sdk/types/__init__.py +16 -6
- label_studio_sdk/types/annotation.py +29 -38
- label_studio_sdk/types/annotation_filter_options.py +14 -24
- label_studio_sdk/types/annotations_dm_field.py +30 -39
- label_studio_sdk/types/azure_blob_export_storage.py +28 -37
- label_studio_sdk/types/azure_blob_import_storage.py +28 -37
- label_studio_sdk/types/base_task.py +30 -39
- label_studio_sdk/types/base_task_updated_by.py +3 -1
- label_studio_sdk/types/base_user.py +14 -21
- label_studio_sdk/types/comment.py +12 -21
- label_studio_sdk/types/comment_created_by.py +1 -1
- label_studio_sdk/types/converted_format.py +12 -22
- label_studio_sdk/types/data_manager_task_serializer.py +31 -40
- label_studio_sdk/types/data_manager_task_serializer_annotators_item.py +1 -1
- label_studio_sdk/types/data_manager_task_serializer_drafts_item.py +13 -22
- label_studio_sdk/types/data_manager_task_serializer_predictions_item.py +15 -24
- label_studio_sdk/types/export.py +17 -26
- label_studio_sdk/types/export_format.py +25 -0
- label_studio_sdk/types/export_snapshot.py +45 -0
- label_studio_sdk/types/export_snapshot_status.py +5 -0
- label_studio_sdk/types/file_upload.py +11 -21
- label_studio_sdk/types/filter.py +16 -26
- label_studio_sdk/types/filter_group.py +12 -22
- label_studio_sdk/types/gcs_export_storage.py +28 -37
- label_studio_sdk/types/gcs_import_storage.py +28 -37
- label_studio_sdk/types/inference_run.py +14 -23
- label_studio_sdk/types/inference_run_cost_estimate.py +47 -0
- label_studio_sdk/types/inference_run_created_by.py +1 -1
- label_studio_sdk/types/inference_run_organization.py +1 -1
- label_studio_sdk/types/key_indicator_value.py +12 -22
- label_studio_sdk/types/key_indicators.py +0 -1
- label_studio_sdk/types/key_indicators_item.py +15 -25
- label_studio_sdk/types/key_indicators_item_additional_kpis_item.py +13 -23
- label_studio_sdk/types/key_indicators_item_extra_kpis_item.py +13 -23
- label_studio_sdk/types/local_files_export_storage.py +25 -34
- label_studio_sdk/types/local_files_import_storage.py +24 -33
- label_studio_sdk/types/ml_backend.py +23 -32
- label_studio_sdk/types/model_provider_connection.py +47 -26
- label_studio_sdk/types/model_provider_connection_budget_reset_period.py +5 -0
- label_studio_sdk/types/model_provider_connection_created_by.py +1 -1
- label_studio_sdk/types/model_provider_connection_organization.py +1 -1
- label_studio_sdk/types/model_provider_connection_provider.py +3 -1
- label_studio_sdk/types/prediction.py +21 -30
- label_studio_sdk/types/project.py +48 -55
- label_studio_sdk/types/project_import.py +21 -30
- label_studio_sdk/types/project_label_config.py +12 -22
- label_studio_sdk/types/prompt.py +24 -32
- label_studio_sdk/types/prompt_associated_projects_item.py +6 -0
- label_studio_sdk/types/prompt_associated_projects_item_id.py +20 -0
- label_studio_sdk/types/prompt_created_by.py +1 -1
- label_studio_sdk/types/prompt_organization.py +1 -1
- label_studio_sdk/types/prompt_version.py +13 -22
- label_studio_sdk/types/prompt_version_created_by.py +1 -1
- label_studio_sdk/types/prompt_version_organization.py +1 -1
- label_studio_sdk/types/prompt_version_provider.py +3 -1
- label_studio_sdk/types/redis_export_storage.py +29 -38
- label_studio_sdk/types/redis_import_storage.py +28 -37
- label_studio_sdk/types/refined_prompt_response.py +19 -29
- label_studio_sdk/types/s3export_storage.py +36 -43
- label_studio_sdk/types/s3import_storage.py +37 -44
- label_studio_sdk/types/s3s_export_storage.py +26 -33
- label_studio_sdk/types/s3s_import_storage.py +35 -42
- label_studio_sdk/types/serialization_option.py +12 -22
- label_studio_sdk/types/serialization_options.py +18 -28
- label_studio_sdk/types/task.py +46 -48
- label_studio_sdk/types/task_annotators_item.py +1 -1
- label_studio_sdk/types/task_comment_authors_item.py +5 -0
- label_studio_sdk/types/task_filter_options.py +15 -25
- label_studio_sdk/types/user_simple.py +11 -21
- label_studio_sdk/types/view.py +16 -26
- label_studio_sdk/types/webhook.py +19 -28
- label_studio_sdk/types/webhook_serializer_for_update.py +19 -28
- label_studio_sdk/types/workspace.py +22 -31
- label_studio_sdk/users/client.py +257 -63
- label_studio_sdk/users/types/users_get_token_response.py +12 -22
- label_studio_sdk/users/types/users_reset_token_response.py +12 -22
- label_studio_sdk/version.py +0 -1
- label_studio_sdk/versions/__init__.py +5 -0
- label_studio_sdk/versions/client.py +112 -0
- label_studio_sdk/versions/types/__init__.py +6 -0
- label_studio_sdk/versions/types/versions_get_response.py +73 -0
- label_studio_sdk/versions/types/versions_get_response_edition.py +5 -0
- label_studio_sdk/views/client.py +219 -52
- label_studio_sdk/views/types/views_create_request_data.py +13 -23
- label_studio_sdk/views/types/views_create_request_data_filters.py +14 -24
- label_studio_sdk/views/types/views_create_request_data_filters_items_item.py +16 -26
- label_studio_sdk/views/types/views_create_request_data_filters_items_item_value.py +3 -1
- label_studio_sdk/views/types/views_update_request_data.py +13 -23
- label_studio_sdk/views/types/views_update_request_data_filters.py +14 -24
- label_studio_sdk/views/types/views_update_request_data_filters_items_item.py +16 -26
- label_studio_sdk/views/types/views_update_request_data_filters_items_item_value.py +3 -1
- label_studio_sdk/webhooks/client.py +191 -61
- label_studio_sdk/workspaces/client.py +164 -41
- label_studio_sdk/workspaces/members/client.py +109 -31
- label_studio_sdk/workspaces/members/types/members_create_response.py +12 -22
- label_studio_sdk/workspaces/members/types/members_list_response_item.py +12 -22
- {label_studio_sdk-1.0.7.dist-info → label_studio_sdk-1.0.10.dist-info}/METADATA +9 -5
- {label_studio_sdk-1.0.7.dist-info → label_studio_sdk-1.0.10.dist-info}/RECORD +203 -186
- {label_studio_sdk-1.0.7.dist-info → label_studio_sdk-1.0.10.dist-info}/WHEEL +1 -1
- label_studio_sdk/types/export_convert.py +0 -32
- label_studio_sdk/types/export_create.py +0 -54
- label_studio_sdk/types/export_create_status.py +0 -5
- {label_studio_sdk-1.0.7.dist-info → label_studio_sdk-1.0.10.dist-info}/LICENSE +0 -0
|
@@ -8,33 +8,27 @@ Taken from FastAPI, and made a bit simpler
|
|
|
8
8
|
https://github.com/tiangolo/fastapi/blob/master/fastapi/encoders.py
|
|
9
9
|
"""
|
|
10
10
|
|
|
11
|
+
import base64
|
|
11
12
|
import dataclasses
|
|
12
13
|
import datetime as dt
|
|
13
|
-
from collections import defaultdict
|
|
14
14
|
from enum import Enum
|
|
15
15
|
from pathlib import PurePath
|
|
16
16
|
from types import GeneratorType
|
|
17
|
-
from typing import Any, Callable, Dict, List, Optional, Set,
|
|
17
|
+
from typing import Any, Callable, Dict, List, Optional, Set, Union
|
|
18
|
+
|
|
19
|
+
import pydantic
|
|
18
20
|
|
|
19
21
|
from .datetime_utils import serialize_datetime
|
|
20
|
-
from .pydantic_utilities import
|
|
22
|
+
from .pydantic_utilities import (
|
|
23
|
+
IS_PYDANTIC_V2,
|
|
24
|
+
encode_by_type,
|
|
25
|
+
to_jsonable_with_fallback,
|
|
26
|
+
)
|
|
21
27
|
|
|
22
28
|
SetIntStr = Set[Union[int, str]]
|
|
23
29
|
DictIntStrAny = Dict[Union[int, str], Any]
|
|
24
30
|
|
|
25
31
|
|
|
26
|
-
def generate_encoders_by_class_tuples(
|
|
27
|
-
type_encoder_map: Dict[Any, Callable[[Any], Any]]
|
|
28
|
-
) -> Dict[Callable[[Any], Any], Tuple[Any, ...]]:
|
|
29
|
-
encoders_by_class_tuples: Dict[Callable[[Any], Any], Tuple[Any, ...]] = defaultdict(tuple)
|
|
30
|
-
for type_, encoder in type_encoder_map.items():
|
|
31
|
-
encoders_by_class_tuples[encoder] += (type_,)
|
|
32
|
-
return encoders_by_class_tuples
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
encoders_by_class_tuples = generate_encoders_by_class_tuples(pydantic_v1.json.ENCODERS_BY_TYPE)
|
|
36
|
-
|
|
37
|
-
|
|
38
32
|
def jsonable_encoder(obj: Any, custom_encoder: Optional[Dict[Any, Callable[[Any], Any]]] = None) -> Any:
|
|
39
33
|
custom_encoder = custom_encoder or {}
|
|
40
34
|
if custom_encoder:
|
|
@@ -44,17 +38,24 @@ def jsonable_encoder(obj: Any, custom_encoder: Optional[Dict[Any, Callable[[Any]
|
|
|
44
38
|
for encoder_type, encoder_instance in custom_encoder.items():
|
|
45
39
|
if isinstance(obj, encoder_type):
|
|
46
40
|
return encoder_instance(obj)
|
|
47
|
-
if isinstance(obj,
|
|
48
|
-
|
|
41
|
+
if isinstance(obj, pydantic.BaseModel):
|
|
42
|
+
if IS_PYDANTIC_V2:
|
|
43
|
+
encoder = getattr(obj.model_config, "json_encoders", {}) # type: ignore # Pydantic v2
|
|
44
|
+
else:
|
|
45
|
+
encoder = getattr(obj.__config__, "json_encoders", {}) # type: ignore # Pydantic v1
|
|
49
46
|
if custom_encoder:
|
|
50
47
|
encoder.update(custom_encoder)
|
|
51
48
|
obj_dict = obj.dict(by_alias=True)
|
|
52
49
|
if "__root__" in obj_dict:
|
|
53
50
|
obj_dict = obj_dict["__root__"]
|
|
51
|
+
if "root" in obj_dict:
|
|
52
|
+
obj_dict = obj_dict["root"]
|
|
54
53
|
return jsonable_encoder(obj_dict, custom_encoder=encoder)
|
|
55
54
|
if dataclasses.is_dataclass(obj):
|
|
56
|
-
obj_dict = dataclasses.asdict(obj)
|
|
55
|
+
obj_dict = dataclasses.asdict(obj) # type: ignore
|
|
57
56
|
return jsonable_encoder(obj_dict, custom_encoder=custom_encoder)
|
|
57
|
+
if isinstance(obj, bytes):
|
|
58
|
+
return base64.b64encode(obj).decode("utf-8")
|
|
58
59
|
if isinstance(obj, Enum):
|
|
59
60
|
return obj.value
|
|
60
61
|
if isinstance(obj, PurePath):
|
|
@@ -80,20 +81,21 @@ def jsonable_encoder(obj: Any, custom_encoder: Optional[Dict[Any, Callable[[Any]
|
|
|
80
81
|
encoded_list.append(jsonable_encoder(item, custom_encoder=custom_encoder))
|
|
81
82
|
return encoded_list
|
|
82
83
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return encoder(obj)
|
|
84
|
+
def fallback_serializer(o: Any) -> Any:
|
|
85
|
+
attempt_encode = encode_by_type(o)
|
|
86
|
+
if attempt_encode is not None:
|
|
87
|
+
return attempt_encode
|
|
88
88
|
|
|
89
|
-
try:
|
|
90
|
-
data = dict(obj)
|
|
91
|
-
except Exception as e:
|
|
92
|
-
errors: List[Exception] = []
|
|
93
|
-
errors.append(e)
|
|
94
89
|
try:
|
|
95
|
-
data =
|
|
90
|
+
data = dict(o)
|
|
96
91
|
except Exception as e:
|
|
92
|
+
errors: List[Exception] = []
|
|
97
93
|
errors.append(e)
|
|
98
|
-
|
|
99
|
-
|
|
94
|
+
try:
|
|
95
|
+
data = vars(o)
|
|
96
|
+
except Exception as e:
|
|
97
|
+
errors.append(e)
|
|
98
|
+
raise ValueError(errors) from e
|
|
99
|
+
return jsonable_encoder(data, custom_encoder=custom_encoder)
|
|
100
|
+
|
|
101
|
+
return to_jsonable_with_fallback(obj, fallback_serializer)
|
|
@@ -4,11 +4,12 @@ import typing
|
|
|
4
4
|
|
|
5
5
|
from typing_extensions import Self
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
import pydantic
|
|
8
8
|
|
|
9
9
|
# Generic to represent the underlying type of the results within a page
|
|
10
10
|
T = typing.TypeVar("T")
|
|
11
11
|
|
|
12
|
+
|
|
12
13
|
# SDKs implement a Page ABC per-pagination request, the endpoint then retuns a pager that wraps this type
|
|
13
14
|
# for example, an endpoint will return SyncPager[UserPage] where UserPage implements the Page ABC. ex:
|
|
14
15
|
#
|
|
@@ -18,16 +19,16 @@ T = typing.TypeVar("T")
|
|
|
18
19
|
# # This should be the outer function that returns the SyncPager again
|
|
19
20
|
# get_next=lambda: list(..., cursor: response.cursor) (or list(..., offset: offset + 1))
|
|
20
21
|
# )
|
|
21
|
-
class BasePage(
|
|
22
|
+
class BasePage(pydantic.BaseModel, typing.Generic[T]):
|
|
22
23
|
has_next: bool
|
|
23
24
|
items: typing.Optional[typing.List[T]]
|
|
24
25
|
|
|
25
26
|
|
|
26
|
-
class SyncPage(BasePage, typing.Generic[T]):
|
|
27
|
+
class SyncPage(BasePage[T], typing.Generic[T]):
|
|
27
28
|
get_next: typing.Optional[typing.Callable[[], typing.Optional[Self]]]
|
|
28
29
|
|
|
29
30
|
|
|
30
|
-
class AsyncPage(BasePage, typing.Generic[T]):
|
|
31
|
+
class AsyncPage(BasePage[T], typing.Generic[T]):
|
|
31
32
|
get_next: typing.Optional[typing.Callable[[], typing.Awaitable[typing.Optional[Self]]]]
|
|
32
33
|
|
|
33
34
|
|
|
@@ -1,28 +1,296 @@
|
|
|
1
1
|
# This file was auto-generated by Fern from our API Definition.
|
|
2
2
|
|
|
3
|
+
# nopycln: file
|
|
4
|
+
import datetime as dt
|
|
3
5
|
import typing
|
|
6
|
+
from collections import defaultdict
|
|
7
|
+
|
|
8
|
+
import typing_extensions
|
|
4
9
|
|
|
5
10
|
import pydantic
|
|
6
11
|
|
|
12
|
+
from .datetime_utils import serialize_datetime
|
|
13
|
+
from .serialization import convert_and_respect_annotation_metadata
|
|
14
|
+
|
|
7
15
|
IS_PYDANTIC_V2 = pydantic.VERSION.startswith("2.")
|
|
8
16
|
|
|
9
17
|
if IS_PYDANTIC_V2:
|
|
10
|
-
|
|
18
|
+
# isort will try to reformat the comments on these imports, which breaks mypy
|
|
19
|
+
# isort: off
|
|
20
|
+
from pydantic.v1.datetime_parse import ( # type: ignore # pyright: ignore[reportMissingImports] # Pydantic v2
|
|
21
|
+
parse_date as parse_date,
|
|
22
|
+
)
|
|
23
|
+
from pydantic.v1.datetime_parse import ( # pyright: ignore[reportMissingImports] # Pydantic v2
|
|
24
|
+
parse_datetime as parse_datetime,
|
|
25
|
+
)
|
|
26
|
+
from pydantic.v1.json import ( # type: ignore # pyright: ignore[reportMissingImports] # Pydantic v2
|
|
27
|
+
ENCODERS_BY_TYPE as encoders_by_type,
|
|
28
|
+
)
|
|
29
|
+
from pydantic.v1.typing import ( # type: ignore # pyright: ignore[reportMissingImports] # Pydantic v2
|
|
30
|
+
get_args as get_args,
|
|
31
|
+
)
|
|
32
|
+
from pydantic.v1.typing import ( # pyright: ignore[reportMissingImports] # Pydantic v2
|
|
33
|
+
get_origin as get_origin,
|
|
34
|
+
)
|
|
35
|
+
from pydantic.v1.typing import ( # pyright: ignore[reportMissingImports] # Pydantic v2
|
|
36
|
+
is_literal_type as is_literal_type,
|
|
37
|
+
)
|
|
38
|
+
from pydantic.v1.typing import ( # pyright: ignore[reportMissingImports] # Pydantic v2
|
|
39
|
+
is_union as is_union,
|
|
40
|
+
)
|
|
41
|
+
from pydantic.v1.fields import ModelField as ModelField # type: ignore # pyright: ignore[reportMissingImports] # Pydantic v2
|
|
11
42
|
else:
|
|
12
|
-
|
|
43
|
+
from pydantic.datetime_parse import parse_date as parse_date # type: ignore # Pydantic v1
|
|
44
|
+
from pydantic.datetime_parse import parse_datetime as parse_datetime # type: ignore # Pydantic v1
|
|
45
|
+
from pydantic.fields import ModelField as ModelField # type: ignore # Pydantic v1
|
|
46
|
+
from pydantic.json import ENCODERS_BY_TYPE as encoders_by_type # type: ignore # Pydantic v1
|
|
47
|
+
from pydantic.typing import get_args as get_args # type: ignore # Pydantic v1
|
|
48
|
+
from pydantic.typing import get_origin as get_origin # type: ignore # Pydantic v1
|
|
49
|
+
from pydantic.typing import is_literal_type as is_literal_type # type: ignore # Pydantic v1
|
|
50
|
+
from pydantic.typing import is_union as is_union # type: ignore # Pydantic v1
|
|
51
|
+
|
|
52
|
+
# isort: on
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
T = typing.TypeVar("T")
|
|
56
|
+
Model = typing.TypeVar("Model", bound=pydantic.BaseModel)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def parse_obj_as(type_: typing.Type[T], object_: typing.Any) -> T:
|
|
60
|
+
dealiased_object = convert_and_respect_annotation_metadata(object_=object_, annotation=type_, direction="read")
|
|
61
|
+
if IS_PYDANTIC_V2:
|
|
62
|
+
adapter = pydantic.TypeAdapter(type_) # type: ignore # Pydantic v2
|
|
63
|
+
return adapter.validate_python(dealiased_object)
|
|
64
|
+
else:
|
|
65
|
+
return pydantic.parse_obj_as(type_, dealiased_object)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def to_jsonable_with_fallback(
|
|
69
|
+
obj: typing.Any, fallback_serializer: typing.Callable[[typing.Any], typing.Any]
|
|
70
|
+
) -> typing.Any:
|
|
71
|
+
if IS_PYDANTIC_V2:
|
|
72
|
+
from pydantic_core import to_jsonable_python
|
|
73
|
+
|
|
74
|
+
return to_jsonable_python(obj, fallback=fallback_serializer)
|
|
75
|
+
else:
|
|
76
|
+
return fallback_serializer(obj)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class UniversalBaseModel(pydantic.BaseModel):
|
|
80
|
+
if IS_PYDANTIC_V2:
|
|
81
|
+
model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(
|
|
82
|
+
# Allow fields begining with `model_` to be used in the model
|
|
83
|
+
protected_namespaces=(),
|
|
84
|
+
) # type: ignore # Pydantic v2
|
|
85
|
+
|
|
86
|
+
@pydantic.model_serializer(mode="wrap", when_used="json") # type: ignore # Pydantic v2
|
|
87
|
+
def serialize_model(self, handler: pydantic.SerializerFunctionWrapHandler) -> typing.Any: # type: ignore # Pydantic v2
|
|
88
|
+
serialized = handler(self)
|
|
89
|
+
data = {k: serialize_datetime(v) if isinstance(v, dt.datetime) else v for k, v in serialized.items()}
|
|
90
|
+
return data
|
|
91
|
+
|
|
92
|
+
else:
|
|
93
|
+
|
|
94
|
+
class Config:
|
|
95
|
+
smart_union = True
|
|
96
|
+
json_encoders = {dt.datetime: serialize_datetime}
|
|
97
|
+
|
|
98
|
+
@classmethod
|
|
99
|
+
def model_construct(
|
|
100
|
+
cls: typing.Type["Model"], _fields_set: typing.Optional[typing.Set[str]] = None, **values: typing.Any
|
|
101
|
+
) -> "Model":
|
|
102
|
+
dealiased_object = convert_and_respect_annotation_metadata(object_=values, annotation=cls, direction="read")
|
|
103
|
+
return cls.construct(_fields_set, **dealiased_object)
|
|
104
|
+
|
|
105
|
+
@classmethod
|
|
106
|
+
def construct(
|
|
107
|
+
cls: typing.Type["Model"], _fields_set: typing.Optional[typing.Set[str]] = None, **values: typing.Any
|
|
108
|
+
) -> "Model":
|
|
109
|
+
dealiased_object = convert_and_respect_annotation_metadata(object_=values, annotation=cls, direction="read")
|
|
110
|
+
if IS_PYDANTIC_V2:
|
|
111
|
+
return super().model_construct(_fields_set, **dealiased_object) # type: ignore # Pydantic v2
|
|
112
|
+
else:
|
|
113
|
+
return super().construct(_fields_set, **dealiased_object)
|
|
114
|
+
|
|
115
|
+
def json(self, **kwargs: typing.Any) -> str:
|
|
116
|
+
kwargs_with_defaults: typing.Any = {
|
|
117
|
+
"by_alias": True,
|
|
118
|
+
"exclude_unset": True,
|
|
119
|
+
**kwargs,
|
|
120
|
+
}
|
|
121
|
+
if IS_PYDANTIC_V2:
|
|
122
|
+
return super().model_dump_json(**kwargs_with_defaults) # type: ignore # Pydantic v2
|
|
123
|
+
else:
|
|
124
|
+
return super().json(**kwargs_with_defaults)
|
|
125
|
+
|
|
126
|
+
def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
|
|
127
|
+
"""
|
|
128
|
+
Override the default dict method to `exclude_unset` by default. This function patches
|
|
129
|
+
`exclude_unset` to work include fields within non-None default values.
|
|
130
|
+
"""
|
|
131
|
+
# Note: the logic here is multi-plexed given the levers exposed in Pydantic V1 vs V2
|
|
132
|
+
# Pydantic V1's .dict can be extremely slow, so we do not want to call it twice.
|
|
133
|
+
#
|
|
134
|
+
# We'd ideally do the same for Pydantic V2, but it shells out to a library to serialize models
|
|
135
|
+
# that we have less control over, and this is less intrusive than custom serializers for now.
|
|
136
|
+
if IS_PYDANTIC_V2:
|
|
137
|
+
kwargs_with_defaults_exclude_unset: typing.Any = {
|
|
138
|
+
**kwargs,
|
|
139
|
+
"by_alias": True,
|
|
140
|
+
"exclude_unset": True,
|
|
141
|
+
"exclude_none": False,
|
|
142
|
+
}
|
|
143
|
+
kwargs_with_defaults_exclude_none: typing.Any = {
|
|
144
|
+
**kwargs,
|
|
145
|
+
"by_alias": True,
|
|
146
|
+
"exclude_none": True,
|
|
147
|
+
"exclude_unset": False,
|
|
148
|
+
}
|
|
149
|
+
dict_dump = deep_union_pydantic_dicts(
|
|
150
|
+
super().model_dump(**kwargs_with_defaults_exclude_unset), # type: ignore # Pydantic v2
|
|
151
|
+
super().model_dump(**kwargs_with_defaults_exclude_none), # type: ignore # Pydantic v2
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
else:
|
|
155
|
+
_fields_set = self.__fields_set__.copy()
|
|
156
|
+
|
|
157
|
+
fields = _get_model_fields(self.__class__)
|
|
158
|
+
for name, field in fields.items():
|
|
159
|
+
if name not in _fields_set:
|
|
160
|
+
default = _get_field_default(field)
|
|
161
|
+
|
|
162
|
+
# If the default values are non-null act like they've been set
|
|
163
|
+
# This effectively allows exclude_unset to work like exclude_none where
|
|
164
|
+
# the latter passes through intentionally set none values.
|
|
165
|
+
if default is not None or ("exclude_unset" in kwargs and not kwargs["exclude_unset"]):
|
|
166
|
+
_fields_set.add(name)
|
|
167
|
+
|
|
168
|
+
if default is not None:
|
|
169
|
+
self.__fields_set__.add(name)
|
|
170
|
+
|
|
171
|
+
kwargs_with_defaults_exclude_unset_include_fields: typing.Any = {
|
|
172
|
+
"by_alias": True,
|
|
173
|
+
"exclude_unset": True,
|
|
174
|
+
"include": _fields_set,
|
|
175
|
+
**kwargs,
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
dict_dump = super().dict(**kwargs_with_defaults_exclude_unset_include_fields)
|
|
179
|
+
|
|
180
|
+
return convert_and_respect_annotation_metadata(object_=dict_dump, annotation=self.__class__, direction="write")
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _union_list_of_pydantic_dicts(
|
|
184
|
+
source: typing.List[typing.Any], destination: typing.List[typing.Any]
|
|
185
|
+
) -> typing.List[typing.Any]:
|
|
186
|
+
converted_list: typing.List[typing.Any] = []
|
|
187
|
+
for i, item in enumerate(source):
|
|
188
|
+
destination_value = destination[i] # type: ignore
|
|
189
|
+
if isinstance(item, dict):
|
|
190
|
+
converted_list.append(deep_union_pydantic_dicts(item, destination_value))
|
|
191
|
+
elif isinstance(item, list):
|
|
192
|
+
converted_list.append(_union_list_of_pydantic_dicts(item, destination_value))
|
|
193
|
+
else:
|
|
194
|
+
converted_list.append(item)
|
|
195
|
+
return converted_list
|
|
13
196
|
|
|
14
197
|
|
|
15
198
|
def deep_union_pydantic_dicts(
|
|
16
199
|
source: typing.Dict[str, typing.Any], destination: typing.Dict[str, typing.Any]
|
|
17
200
|
) -> typing.Dict[str, typing.Any]:
|
|
18
201
|
for key, value in source.items():
|
|
202
|
+
node = destination.setdefault(key, {})
|
|
19
203
|
if isinstance(value, dict):
|
|
20
|
-
node = destination.setdefault(key, {})
|
|
21
204
|
deep_union_pydantic_dicts(value, node)
|
|
205
|
+
# Note: we do not do this same processing for sets given we do not have sets of models
|
|
206
|
+
# and given the sets are unordered, the processing of the set and matching objects would
|
|
207
|
+
# be non-trivial.
|
|
208
|
+
elif isinstance(value, list):
|
|
209
|
+
destination[key] = _union_list_of_pydantic_dicts(value, node)
|
|
22
210
|
else:
|
|
23
211
|
destination[key] = value
|
|
24
212
|
|
|
25
213
|
return destination
|
|
26
214
|
|
|
27
215
|
|
|
28
|
-
|
|
216
|
+
if IS_PYDANTIC_V2:
|
|
217
|
+
|
|
218
|
+
class V2RootModel(UniversalBaseModel, pydantic.RootModel): # type: ignore # Pydantic v2
|
|
219
|
+
pass
|
|
220
|
+
|
|
221
|
+
UniversalRootModel: typing_extensions.TypeAlias = V2RootModel # type: ignore
|
|
222
|
+
else:
|
|
223
|
+
UniversalRootModel: typing_extensions.TypeAlias = UniversalBaseModel # type: ignore
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def encode_by_type(o: typing.Any) -> typing.Any:
|
|
227
|
+
encoders_by_class_tuples: typing.Dict[typing.Callable[[typing.Any], typing.Any], typing.Tuple[typing.Any, ...]] = (
|
|
228
|
+
defaultdict(tuple)
|
|
229
|
+
)
|
|
230
|
+
for type_, encoder in encoders_by_type.items():
|
|
231
|
+
encoders_by_class_tuples[encoder] += (type_,)
|
|
232
|
+
|
|
233
|
+
if type(o) in encoders_by_type:
|
|
234
|
+
return encoders_by_type[type(o)](o)
|
|
235
|
+
for encoder, classes_tuple in encoders_by_class_tuples.items():
|
|
236
|
+
if isinstance(o, classes_tuple):
|
|
237
|
+
return encoder(o)
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def update_forward_refs(model: typing.Type["Model"], **localns: typing.Any) -> None:
|
|
241
|
+
if IS_PYDANTIC_V2:
|
|
242
|
+
model.model_rebuild(raise_errors=False) # type: ignore # Pydantic v2
|
|
243
|
+
else:
|
|
244
|
+
model.update_forward_refs(**localns)
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
# Mirrors Pydantic's internal typing
|
|
248
|
+
AnyCallable = typing.Callable[..., typing.Any]
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def universal_root_validator(
|
|
252
|
+
pre: bool = False,
|
|
253
|
+
) -> typing.Callable[[AnyCallable], AnyCallable]:
|
|
254
|
+
def decorator(func: AnyCallable) -> AnyCallable:
|
|
255
|
+
if IS_PYDANTIC_V2:
|
|
256
|
+
return pydantic.model_validator(mode="before" if pre else "after")(func) # type: ignore # Pydantic v2
|
|
257
|
+
else:
|
|
258
|
+
return pydantic.root_validator(pre=pre)(func) # type: ignore # Pydantic v1
|
|
259
|
+
|
|
260
|
+
return decorator
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def universal_field_validator(field_name: str, pre: bool = False) -> typing.Callable[[AnyCallable], AnyCallable]:
|
|
264
|
+
def decorator(func: AnyCallable) -> AnyCallable:
|
|
265
|
+
if IS_PYDANTIC_V2:
|
|
266
|
+
return pydantic.field_validator(field_name, mode="before" if pre else "after")(func) # type: ignore # Pydantic v2
|
|
267
|
+
else:
|
|
268
|
+
return pydantic.validator(field_name, pre=pre)(func) # type: ignore # Pydantic v1
|
|
269
|
+
|
|
270
|
+
return decorator
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
PydanticField = typing.Union[ModelField, pydantic.fields.FieldInfo]
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def _get_model_fields(
|
|
277
|
+
model: typing.Type["Model"],
|
|
278
|
+
) -> typing.Mapping[str, PydanticField]:
|
|
279
|
+
if IS_PYDANTIC_V2:
|
|
280
|
+
return model.model_fields # type: ignore # Pydantic v2
|
|
281
|
+
else:
|
|
282
|
+
return model.__fields__ # type: ignore # Pydantic v1
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def _get_field_default(field: PydanticField) -> typing.Any:
|
|
286
|
+
try:
|
|
287
|
+
value = field.get_default() # type: ignore # Pydantic < v1.10.15
|
|
288
|
+
except:
|
|
289
|
+
value = field.default
|
|
290
|
+
if IS_PYDANTIC_V2:
|
|
291
|
+
from pydantic_core import PydanticUndefined
|
|
292
|
+
|
|
293
|
+
if value == PydanticUndefined:
|
|
294
|
+
return None
|
|
295
|
+
return value
|
|
296
|
+
return value
|
|
@@ -1,33 +1,58 @@
|
|
|
1
1
|
# This file was auto-generated by Fern from our API Definition.
|
|
2
2
|
|
|
3
|
-
from
|
|
4
|
-
from typing import Any, Dict, Optional
|
|
3
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
import pydantic
|
|
7
6
|
|
|
8
7
|
|
|
9
8
|
# Flattens dicts to be of the form {"key[subkey][subkey2]": value} where value is not a dict
|
|
10
|
-
def traverse_query_dict(dict_flat: Dict[str, Any], key_prefix: Optional[str] = None) ->
|
|
11
|
-
result =
|
|
9
|
+
def traverse_query_dict(dict_flat: Dict[str, Any], key_prefix: Optional[str] = None) -> List[Tuple[str, Any]]:
|
|
10
|
+
result = []
|
|
12
11
|
for k, v in dict_flat.items():
|
|
13
12
|
key = f"{key_prefix}[{k}]" if key_prefix is not None else k
|
|
14
13
|
if isinstance(v, dict):
|
|
15
|
-
result.
|
|
14
|
+
result.extend(traverse_query_dict(v, key))
|
|
15
|
+
elif isinstance(v, list):
|
|
16
|
+
for arr_v in v:
|
|
17
|
+
if isinstance(arr_v, dict):
|
|
18
|
+
result.extend(traverse_query_dict(arr_v, key))
|
|
19
|
+
else:
|
|
20
|
+
result.append((key, arr_v))
|
|
16
21
|
else:
|
|
17
|
-
result
|
|
22
|
+
result.append((key, v))
|
|
18
23
|
return result
|
|
19
24
|
|
|
20
25
|
|
|
21
|
-
def single_query_encoder(query_key: str, query_value: Any) ->
|
|
22
|
-
if isinstance(query_value,
|
|
23
|
-
if isinstance(query_value,
|
|
26
|
+
def single_query_encoder(query_key: str, query_value: Any) -> List[Tuple[str, Any]]:
|
|
27
|
+
if isinstance(query_value, pydantic.BaseModel) or isinstance(query_value, dict):
|
|
28
|
+
if isinstance(query_value, pydantic.BaseModel):
|
|
24
29
|
obj_dict = query_value.dict(by_alias=True)
|
|
25
30
|
else:
|
|
26
31
|
obj_dict = query_value
|
|
27
32
|
return traverse_query_dict(obj_dict, query_key)
|
|
33
|
+
elif isinstance(query_value, list):
|
|
34
|
+
encoded_values: List[Tuple[str, Any]] = []
|
|
35
|
+
for value in query_value:
|
|
36
|
+
if isinstance(value, pydantic.BaseModel) or isinstance(value, dict):
|
|
37
|
+
if isinstance(value, pydantic.BaseModel):
|
|
38
|
+
obj_dict = value.dict(by_alias=True)
|
|
39
|
+
elif isinstance(value, dict):
|
|
40
|
+
obj_dict = value
|
|
28
41
|
|
|
29
|
-
|
|
42
|
+
encoded_values.extend(single_query_encoder(query_key, obj_dict))
|
|
43
|
+
else:
|
|
44
|
+
encoded_values.append((query_key, value))
|
|
30
45
|
|
|
46
|
+
return encoded_values
|
|
31
47
|
|
|
32
|
-
|
|
33
|
-
|
|
48
|
+
return [(query_key, query_value)]
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def encode_query(query: Optional[Dict[str, Any]]) -> Optional[List[Tuple[str, Any]]]:
|
|
52
|
+
if query is None:
|
|
53
|
+
return None
|
|
54
|
+
|
|
55
|
+
encoded_query = []
|
|
56
|
+
for k, v in query.items():
|
|
57
|
+
encoded_query.extend(single_query_encoder(k, v))
|
|
58
|
+
return encoded_query
|
|
@@ -23,6 +23,8 @@ class RequestOptions(typing.TypedDict, total=False):
|
|
|
23
23
|
- additional_query_parameters: typing.Dict[str, typing.Any]. A dictionary containing additional parameters to spread into the request's query parameters dict
|
|
24
24
|
|
|
25
25
|
- additional_body_parameters: typing.Dict[str, typing.Any]. A dictionary containing additional parameters to spread into the request's body parameters dict
|
|
26
|
+
|
|
27
|
+
- chunk_size: int. The size, in bytes, to process each chunk of data being streamed back within the response. This equates to leveraging `chunk_size` within `requests` or `httpx`, and is only leveraged for file downloads.
|
|
26
28
|
"""
|
|
27
29
|
|
|
28
30
|
timeout_in_seconds: NotRequired[int]
|
|
@@ -30,3 +32,4 @@ class RequestOptions(typing.TypedDict, total=False):
|
|
|
30
32
|
additional_headers: NotRequired[typing.Dict[str, typing.Any]]
|
|
31
33
|
additional_query_parameters: NotRequired[typing.Dict[str, typing.Any]]
|
|
32
34
|
additional_body_parameters: NotRequired[typing.Dict[str, typing.Any]]
|
|
35
|
+
chunk_size: NotRequired[int]
|