frameio 0.0.28__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.
- frameio/__init__.py +1158 -0
- frameio/account_permissions/__init__.py +4 -0
- frameio/account_permissions/client.py +193 -0
- frameio/account_permissions/raw_client.py +333 -0
- frameio/accounts/__init__.py +4 -0
- frameio/accounts/client.py +162 -0
- frameio/accounts/raw_client.py +309 -0
- frameio/client.py +439 -0
- frameio/comments/__init__.py +38 -0
- frameio/comments/client.py +654 -0
- frameio/comments/raw_client.py +1316 -0
- frameio/comments/types/__init__.py +40 -0
- frameio/comments/types/comments_show_request_include.py +5 -0
- frameio/comments/types/create_comment_params_data.py +33 -0
- frameio/comments/types/update_comment_params_data.py +33 -0
- frameio/core/__init__.py +122 -0
- frameio/core/api_error.py +23 -0
- frameio/core/client_wrapper.py +95 -0
- frameio/core/custom_pagination.py +152 -0
- frameio/core/datetime_utils.py +28 -0
- frameio/core/file.py +67 -0
- frameio/core/force_multipart.py +18 -0
- frameio/core/http_client.py +663 -0
- frameio/core/http_response.py +55 -0
- frameio/core/http_sse/__init__.py +42 -0
- frameio/core/http_sse/_api.py +112 -0
- frameio/core/http_sse/_decoders.py +61 -0
- frameio/core/http_sse/_exceptions.py +7 -0
- frameio/core/http_sse/_models.py +17 -0
- frameio/core/jsonable_encoder.py +100 -0
- frameio/core/pagination.py +82 -0
- frameio/core/pydantic_utilities.py +361 -0
- frameio/core/query_encoder.py +58 -0
- frameio/core/remove_none_from_dict.py +11 -0
- frameio/core/request_options.py +35 -0
- frameio/core/serialization.py +276 -0
- frameio/core/unchecked_base_model.py +376 -0
- frameio/environment.py +7 -0
- frameio/errors/__init__.py +53 -0
- frameio/errors/bad_request_error.py +11 -0
- frameio/errors/forbidden_error.py +11 -0
- frameio/errors/not_found_error.py +11 -0
- frameio/errors/too_many_requests_error.py +11 -0
- frameio/errors/unauthorized_error.py +11 -0
- frameio/errors/unprocessable_entity_error.py +10 -0
- frameio/files/__init__.py +58 -0
- frameio/files/client.py +1171 -0
- frameio/files/raw_client.py +2517 -0
- frameio/files/types/__init__.py +56 -0
- frameio/files/types/file_copy_params_data.py +15 -0
- frameio/files/types/file_create_local_upload_params_data.py +20 -0
- frameio/files/types/file_create_params_data.py +31 -0
- frameio/files/types/file_create_remote_upload_params_data.py +20 -0
- frameio/files/types/file_move_params_data.py +15 -0
- frameio/files/types/file_update_params_data.py +15 -0
- frameio/files/types/files_copy_request_copy_comments.py +5 -0
- frameio/folders/__init__.py +39 -0
- frameio/folders/client.py +1004 -0
- frameio/folders/raw_client.py +2074 -0
- frameio/folders/types/__init__.py +42 -0
- frameio/folders/types/folder_copy_params_data.py +15 -0
- frameio/folders/types/folder_create_params_data.py +15 -0
- frameio/folders/types/folder_move_params_data.py +15 -0
- frameio/folders/types/folder_update_params_data.py +15 -0
- frameio/metadata/__init__.py +37 -0
- frameio/metadata/client.py +293 -0
- frameio/metadata/raw_client.py +509 -0
- frameio/metadata/types/__init__.py +38 -0
- frameio/metadata/types/bulk_update_metadata_params_data.py +23 -0
- frameio/metadata/types/bulk_update_metadata_params_data_values_item.py +13 -0
- frameio/metadata_fields/__init__.py +103 -0
- frameio/metadata_fields/client.py +536 -0
- frameio/metadata_fields/raw_client.py +996 -0
- frameio/metadata_fields/types/__init__.py +105 -0
- frameio/metadata_fields/types/create_field_definition_params_data.py +112 -0
- frameio/metadata_fields/types/update_field_definition_params_data.py +118 -0
- frameio/project_permissions/__init__.py +4 -0
- frameio/project_permissions/client.py +426 -0
- frameio/project_permissions/raw_client.py +824 -0
- frameio/projects/__init__.py +38 -0
- frameio/projects/client.py +604 -0
- frameio/projects/raw_client.py +1286 -0
- frameio/projects/types/__init__.py +40 -0
- frameio/projects/types/project_params_data.py +20 -0
- frameio/projects/types/project_update_params_data.py +26 -0
- frameio/projects/types/project_update_params_data_status.py +5 -0
- frameio/py.typed +0 -0
- frameio/shares/__init__.py +64 -0
- frameio/shares/client.py +1217 -0
- frameio/shares/raw_client.py +2511 -0
- frameio/shares/types/__init__.py +61 -0
- frameio/shares/types/add_asset_params_data.py +15 -0
- frameio/shares/types/add_reviewers_to_share_params_data.py +18 -0
- frameio/shares/types/add_reviewers_to_share_params_data_reviewers.py +27 -0
- frameio/shares/types/create_share_params_data.py +26 -0
- frameio/shares/types/remove_reviewer_params_data.py +13 -0
- frameio/shares/types/remove_reviewer_params_data_reviewers.py +27 -0
- frameio/shares/types/update_share_params_data.py +34 -0
- frameio/shares/types/update_share_params_data_access.py +5 -0
- frameio/types/__init__.py +983 -0
- frameio/types/account.py +70 -0
- frameio/types/account_roles_item.py +5 -0
- frameio/types/account_user_role.py +23 -0
- frameio/types/account_user_role_role.py +5 -0
- frameio/types/account_user_roles_response.py +27 -0
- frameio/types/accounts_response.py +23 -0
- frameio/types/add_asset_response.py +13 -0
- frameio/types/add_asset_response_data.py +15 -0
- frameio/types/asset_common.py +48 -0
- frameio/types/asset_common_type.py +5 -0
- frameio/types/asset_common_with_includes.py +58 -0
- frameio/types/asset_common_with_includes_type.py +5 -0
- frameio/types/asset_include.py +5 -0
- frameio/types/asset_share_params.py +39 -0
- frameio/types/asset_share_params_access.py +5 -0
- frameio/types/asset_with_includes.py +76 -0
- frameio/types/assets_with_includes_response.py +23 -0
- frameio/types/bad_request.py +13 -0
- frameio/types/bad_request_errors_item.py +15 -0
- frameio/types/bad_request_errors_item_source.py +12 -0
- frameio/types/boolean_value.py +12 -0
- frameio/types/children_type.py +3 -0
- frameio/types/comment.py +59 -0
- frameio/types/comment_include.py +5 -0
- frameio/types/comment_response.py +13 -0
- frameio/types/comment_with_includes.py +17 -0
- frameio/types/comment_with_includes_response.py +13 -0
- frameio/types/comments_with_includes_response.py +23 -0
- frameio/types/date_definition.py +49 -0
- frameio/types/date_definition_field_configuration.py +21 -0
- frameio/types/date_definition_field_configuration_display_format.py +7 -0
- frameio/types/date_definition_field_configuration_time_format.py +5 -0
- frameio/types/date_definition_field_type.py +10 -0
- frameio/types/date_definition_params.py +17 -0
- frameio/types/date_definition_params_field_configuration.py +23 -0
- frameio/types/date_definition_params_field_configuration_display_format.py +7 -0
- frameio/types/date_definition_params_field_configuration_time_format.py +7 -0
- frameio/types/date_definition_with_includes.py +13 -0
- frameio/types/date_value.py +12 -0
- frameio/types/email.py +3 -0
- frameio/types/empty_json.py +5 -0
- frameio/types/field_definition.py +167 -0
- frameio/types/field_definition_include.py +5 -0
- frameio/types/field_definition_response.py +13 -0
- frameio/types/field_definition_with_includes.py +178 -0
- frameio/types/field_definitions_with_includes_response.py +27 -0
- frameio/types/field_value_common.py +29 -0
- frameio/types/file.py +60 -0
- frameio/types/file_copy_response.py +13 -0
- frameio/types/file_remote_upload_response.py +17 -0
- frameio/types/file_response.py +13 -0
- frameio/types/file_status.py +5 -0
- frameio/types/file_upload_status.py +41 -0
- frameio/types/file_upload_status_response.py +13 -0
- frameio/types/file_with_includes.py +25 -0
- frameio/types/file_with_includes_response.py +13 -0
- frameio/types/file_with_includes_status.py +5 -0
- frameio/types/file_with_media_links_include.py +19 -0
- frameio/types/file_with_upload_urls.py +16 -0
- frameio/types/file_with_upload_urls_response.py +13 -0
- frameio/types/folder.py +15 -0
- frameio/types/folder_copy_response.py +13 -0
- frameio/types/folder_response.py +13 -0
- frameio/types/folder_with_includes.py +63 -0
- frameio/types/folder_with_includes_response.py +13 -0
- frameio/types/folders_with_includes_response.py +23 -0
- frameio/types/forbidden.py +13 -0
- frameio/types/forbidden_errors_item.py +15 -0
- frameio/types/forbidden_errors_item_source.py +12 -0
- frameio/types/include.py +5 -0
- frameio/types/include_total_count.py +3 -0
- frameio/types/integer_value.py +12 -0
- frameio/types/json_error_response.py +13 -0
- frameio/types/json_error_response_errors_item.py +15 -0
- frameio/types/json_error_response_errors_item_source.py +12 -0
- frameio/types/links.py +22 -0
- frameio/types/long_text_definition.py +49 -0
- frameio/types/long_text_definition_field_configuration.py +10 -0
- frameio/types/long_text_definition_field_type.py +10 -0
- frameio/types/long_text_definition_params.py +15 -0
- frameio/types/long_text_definition_with_includes.py +13 -0
- frameio/types/media_link_common.py +17 -0
- frameio/types/media_links_collection.py +24 -0
- frameio/types/metadata_field.py +149 -0
- frameio/types/metadata_response.py +13 -0
- frameio/types/metadata_with_definition.py +21 -0
- frameio/types/multi_select_value.py +14 -0
- frameio/types/multi_user_value.py +20 -0
- frameio/types/multi_user_value_member_options_type.py +5 -0
- frameio/types/no_content.py +3 -0
- frameio/types/not_found.py +13 -0
- frameio/types/not_found_errors_item.py +15 -0
- frameio/types/not_found_errors_item_source.py +12 -0
- frameio/types/number_definition.py +43 -0
- frameio/types/number_definition_field_configuration.py +21 -0
- frameio/types/number_definition_field_configuration_number_format.py +7 -0
- frameio/types/number_definition_params.py +17 -0
- frameio/types/number_definition_params_field_configuration.py +23 -0
- frameio/types/number_definition_params_field_configuration_number_format.py +7 -0
- frameio/types/number_definition_with_includes.py +13 -0
- frameio/types/number_value.py +12 -0
- frameio/types/original_media_link.py +16 -0
- frameio/types/profile.py +34 -0
- frameio/types/profile_response.py +17 -0
- frameio/types/project.py +66 -0
- frameio/types/project_include.py +5 -0
- frameio/types/project_response.py +17 -0
- frameio/types/project_status.py +5 -0
- frameio/types/project_with_includes.py +18 -0
- frameio/types/project_with_includes_response.py +17 -0
- frameio/types/projects_with_includes_response.py +27 -0
- frameio/types/rating_definition.py +43 -0
- frameio/types/rating_definition_field_configuration.py +22 -0
- frameio/types/rating_definition_field_configuration_style.py +5 -0
- frameio/types/rating_definition_params.py +17 -0
- frameio/types/rating_definition_params_field_configuration.py +22 -0
- frameio/types/rating_definition_params_field_configuration_style.py +5 -0
- frameio/types/rating_definition_with_includes.py +13 -0
- frameio/types/remove_asset_response.py +13 -0
- frameio/types/remove_asset_response_data.py +15 -0
- frameio/types/rendition_media_link.py +19 -0
- frameio/types/request_after_opaque_cursor.py +5 -0
- frameio/types/request_page_size.py +3 -0
- frameio/types/select_definition.py +43 -0
- frameio/types/select_definition_field_configuration.py +18 -0
- frameio/types/select_definition_field_configuration_options_item.py +22 -0
- frameio/types/select_definition_params.py +17 -0
- frameio/types/select_definition_params_field_configuration.py +20 -0
- frameio/types/select_definition_params_field_configuration_options_item.py +20 -0
- frameio/types/select_definition_with_includes.py +13 -0
- frameio/types/select_multi_definition.py +49 -0
- frameio/types/select_multi_definition_field_configuration.py +18 -0
- frameio/types/select_multi_definition_field_configuration_options_item.py +22 -0
- frameio/types/select_multi_definition_field_type.py +10 -0
- frameio/types/select_multi_definition_params.py +17 -0
- frameio/types/select_multi_definition_params_field_configuration.py +20 -0
- frameio/types/select_multi_definition_params_field_configuration_options_item.py +20 -0
- frameio/types/select_multi_definition_with_includes.py +13 -0
- frameio/types/select_option.py +20 -0
- frameio/types/select_value.py +14 -0
- frameio/types/share.py +66 -0
- frameio/types/share_access.py +5 -0
- frameio/types/share_response.py +13 -0
- frameio/types/share_reviewers_response.py +27 -0
- frameio/types/shares_response.py +27 -0
- frameio/types/single_user_value.py +20 -0
- frameio/types/single_user_value_member_options_type.py +5 -0
- frameio/types/text_definition.py +49 -0
- frameio/types/text_definition_field_configuration.py +10 -0
- frameio/types/text_definition_field_type.py +10 -0
- frameio/types/text_definition_params.py +15 -0
- frameio/types/text_definition_with_includes.py +13 -0
- frameio/types/text_value.py +12 -0
- frameio/types/time_stamp.py +5 -0
- frameio/types/toggle_definition.py +43 -0
- frameio/types/toggle_definition_field_configuration.py +15 -0
- frameio/types/toggle_definition_params.py +17 -0
- frameio/types/toggle_definition_params_field_configuration.py +15 -0
- frameio/types/toggle_definition_with_includes.py +13 -0
- frameio/types/too_many_requests.py +13 -0
- frameio/types/too_many_requests_errors_item.py +15 -0
- frameio/types/too_many_requests_errors_item_source.py +12 -0
- frameio/types/unauthorized.py +13 -0
- frameio/types/unauthorized_errors_item.py +15 -0
- frameio/types/unauthorized_errors_item_source.py +12 -0
- frameio/types/unprocessable_entity.py +13 -0
- frameio/types/unprocessable_entity_errors_item.py +15 -0
- frameio/types/unprocessable_entity_errors_item_source.py +12 -0
- frameio/types/update_date_definition_params.py +17 -0
- frameio/types/update_date_definition_params_field_configuration.py +25 -0
- frameio/types/update_date_definition_params_field_configuration_display_format.py +7 -0
- frameio/types/update_date_definition_params_field_configuration_time_format.py +7 -0
- frameio/types/update_long_text_definition_params.py +15 -0
- frameio/types/update_number_definition_params.py +17 -0
- frameio/types/update_number_definition_params_field_configuration.py +25 -0
- frameio/types/update_number_definition_params_field_configuration_number_format.py +7 -0
- frameio/types/update_rating_definition_params.py +17 -0
- frameio/types/update_rating_definition_params_field_configuration.py +24 -0
- frameio/types/update_rating_definition_params_field_configuration_style.py +7 -0
- frameio/types/update_select_definition_params.py +17 -0
- frameio/types/update_select_definition_params_field_configuration.py +20 -0
- frameio/types/update_select_definition_params_field_configuration_options_item.py +20 -0
- frameio/types/update_select_multi_definition_params.py +19 -0
- frameio/types/update_select_multi_definition_params_field_configuration.py +20 -0
- frameio/types/update_select_multi_definition_params_field_configuration_options_item.py +20 -0
- frameio/types/update_text_definition_params.py +15 -0
- frameio/types/update_toggle_definition_params.py +17 -0
- frameio/types/update_toggle_definition_params_field_configuration.py +15 -0
- frameio/types/update_user_multi_definition_params.py +17 -0
- frameio/types/update_user_multi_definition_params_field_configuration.py +25 -0
- frameio/types/update_user_multi_definition_params_field_configuration_custom_members_item.py +20 -0
- frameio/types/update_user_multi_definition_params_field_configuration_custom_members_item_type.py +7 -0
- frameio/types/update_user_multi_definition_params_field_configuration_member_options_type.py +7 -0
- frameio/types/update_user_roles_params.py +17 -0
- frameio/types/update_user_roles_params_data.py +13 -0
- frameio/types/update_user_roles_params_data_role.py +7 -0
- frameio/types/update_user_roles_response.py +13 -0
- frameio/types/update_user_roles_response_data.py +13 -0
- frameio/types/update_user_roles_response_data_role.py +7 -0
- frameio/types/update_user_single_definition_params.py +17 -0
- frameio/types/update_user_single_definition_params_field_configuration.py +25 -0
- frameio/types/update_user_single_definition_params_field_configuration_custom_members_item.py +20 -0
- frameio/types/update_user_single_definition_params_field_configuration_custom_members_item_type.py +7 -0
- frameio/types/update_user_single_definition_params_field_configuration_member_options_type.py +7 -0
- frameio/types/upload_url.py +20 -0
- frameio/types/user.py +44 -0
- frameio/types/user_multi_definition.py +49 -0
- frameio/types/user_multi_definition_field_configuration.py +23 -0
- frameio/types/user_multi_definition_field_configuration_custom_members_item.py +20 -0
- frameio/types/user_multi_definition_field_configuration_custom_members_item_type.py +7 -0
- frameio/types/user_multi_definition_field_configuration_member_options_type.py +7 -0
- frameio/types/user_multi_definition_field_type.py +10 -0
- frameio/types/user_multi_definition_params.py +17 -0
- frameio/types/user_multi_definition_params_field_configuration.py +23 -0
- frameio/types/user_multi_definition_params_field_configuration_custom_members_item.py +20 -0
- frameio/types/user_multi_definition_params_field_configuration_custom_members_item_type.py +7 -0
- frameio/types/user_multi_definition_params_field_configuration_member_options_type.py +7 -0
- frameio/types/user_multi_definition_with_includes.py +13 -0
- frameio/types/user_role.py +19 -0
- frameio/types/user_role_role.py +7 -0
- frameio/types/user_roles_response.py +27 -0
- frameio/types/user_single_definition.py +49 -0
- frameio/types/user_single_definition_field_configuration.py +23 -0
- frameio/types/user_single_definition_field_configuration_custom_members_item.py +20 -0
- frameio/types/user_single_definition_field_configuration_custom_members_item_type.py +7 -0
- frameio/types/user_single_definition_field_configuration_member_options_type.py +7 -0
- frameio/types/user_single_definition_field_type.py +10 -0
- frameio/types/user_single_definition_params.py +17 -0
- frameio/types/user_single_definition_params_field_configuration.py +23 -0
- frameio/types/user_single_definition_params_field_configuration_custom_members_item.py +20 -0
- frameio/types/user_single_definition_params_field_configuration_custom_members_item_type.py +7 -0
- frameio/types/user_single_definition_params_field_configuration_member_options_type.py +7 -0
- frameio/types/user_single_definition_with_includes.py +13 -0
- frameio/types/user_value.py +18 -0
- frameio/types/user_value_type.py +5 -0
- frameio/types/uuid_.py +3 -0
- frameio/types/version_stack.py +50 -0
- frameio/types/version_stack_copy_response.py +13 -0
- frameio/types/version_stack_response.py +13 -0
- frameio/types/version_stack_with_includes.py +60 -0
- frameio/types/version_stack_with_includes_response.py +13 -0
- frameio/types/version_stacks_with_includes_response.py +19 -0
- frameio/types/webhook.py +36 -0
- frameio/types/webhook_create_response.py +17 -0
- frameio/types/webhook_create_response_data.py +15 -0
- frameio/types/webhook_events.py +5 -0
- frameio/types/webhook_response.py +13 -0
- frameio/types/webhook_with_includes.py +13 -0
- frameio/types/webhook_with_includes_response.py +13 -0
- frameio/types/webhooks_with_includes_response.py +23 -0
- frameio/types/workspace.py +40 -0
- frameio/types/workspace_include.py +5 -0
- frameio/types/workspace_params.py +17 -0
- frameio/types/workspace_params_data.py +15 -0
- frameio/types/workspace_response.py +17 -0
- frameio/types/workspace_with_includes.py +13 -0
- frameio/types/workspace_with_includes_response.py +17 -0
- frameio/types/workspaces_with_includes_response.py +27 -0
- frameio/users/__init__.py +4 -0
- frameio/users/client.py +100 -0
- frameio/users/raw_client.py +234 -0
- frameio/version.py +3 -0
- frameio/version_stacks/__init__.py +49 -0
- frameio/version_stacks/client.py +818 -0
- frameio/version_stacks/raw_client.py +1614 -0
- frameio/version_stacks/types/__init__.py +47 -0
- frameio/version_stacks/types/version_stack_copy_params_data.py +15 -0
- frameio/version_stacks/types/version_stack_create_params_data.py +19 -0
- frameio/version_stacks/types/version_stack_move_params_data.py +15 -0
- frameio/version_stacks/types/version_stacks_show_request_include.py +19 -0
- frameio/webhooks/__init__.py +34 -0
- frameio/webhooks/client.py +793 -0
- frameio/webhooks/raw_client.py +1347 -0
- frameio/webhooks/types/__init__.py +38 -0
- frameio/webhooks/types/webhook_create_params_data.py +18 -0
- frameio/webhooks/types/webhook_update_params_data.py +23 -0
- frameio/workspace_permissions/__init__.py +4 -0
- frameio/workspace_permissions/client.py +430 -0
- frameio/workspace_permissions/raw_client.py +824 -0
- frameio/workspaces/__init__.py +4 -0
- frameio/workspaces/client.py +563 -0
- frameio/workspaces/raw_client.py +1259 -0
- frameio-0.0.28.dist-info/METADATA +259 -0
- frameio-0.0.28.dist-info/RECORD +385 -0
- frameio-0.0.28.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
# This file was auto-generated by Fern from our API Definition.
|
|
2
|
+
|
|
3
|
+
import collections
|
|
4
|
+
import inspect
|
|
5
|
+
import typing
|
|
6
|
+
|
|
7
|
+
import pydantic
|
|
8
|
+
import typing_extensions
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class FieldMetadata:
|
|
12
|
+
"""
|
|
13
|
+
Metadata class used to annotate fields to provide additional information.
|
|
14
|
+
|
|
15
|
+
Example:
|
|
16
|
+
class MyDict(TypedDict):
|
|
17
|
+
field: typing.Annotated[str, FieldMetadata(alias="field_name")]
|
|
18
|
+
|
|
19
|
+
Will serialize: `{"field": "value"}`
|
|
20
|
+
To: `{"field_name": "value"}`
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
alias: str
|
|
24
|
+
|
|
25
|
+
def __init__(self, *, alias: str) -> None:
|
|
26
|
+
self.alias = alias
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def convert_and_respect_annotation_metadata(
|
|
30
|
+
*,
|
|
31
|
+
object_: typing.Any,
|
|
32
|
+
annotation: typing.Any,
|
|
33
|
+
inner_type: typing.Optional[typing.Any] = None,
|
|
34
|
+
direction: typing.Literal["read", "write"],
|
|
35
|
+
) -> typing.Any:
|
|
36
|
+
"""
|
|
37
|
+
Respect the metadata annotations on a field, such as aliasing. This function effectively
|
|
38
|
+
manipulates the dict-form of an object to respect the metadata annotations. This is primarily used for
|
|
39
|
+
TypedDicts, which cannot support aliasing out of the box, and can be extended for additional
|
|
40
|
+
utilities, such as defaults.
|
|
41
|
+
|
|
42
|
+
Parameters
|
|
43
|
+
----------
|
|
44
|
+
object_ : typing.Any
|
|
45
|
+
|
|
46
|
+
annotation : type
|
|
47
|
+
The type we're looking to apply typing annotations from
|
|
48
|
+
|
|
49
|
+
inner_type : typing.Optional[type]
|
|
50
|
+
|
|
51
|
+
Returns
|
|
52
|
+
-------
|
|
53
|
+
typing.Any
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
if object_ is None:
|
|
57
|
+
return None
|
|
58
|
+
if inner_type is None:
|
|
59
|
+
inner_type = annotation
|
|
60
|
+
|
|
61
|
+
clean_type = _remove_annotations(inner_type)
|
|
62
|
+
# Pydantic models
|
|
63
|
+
if (
|
|
64
|
+
inspect.isclass(clean_type)
|
|
65
|
+
and issubclass(clean_type, pydantic.BaseModel)
|
|
66
|
+
and isinstance(object_, typing.Mapping)
|
|
67
|
+
):
|
|
68
|
+
return _convert_mapping(object_, clean_type, direction)
|
|
69
|
+
# TypedDicts
|
|
70
|
+
if typing_extensions.is_typeddict(clean_type) and isinstance(object_, typing.Mapping):
|
|
71
|
+
return _convert_mapping(object_, clean_type, direction)
|
|
72
|
+
|
|
73
|
+
if (
|
|
74
|
+
typing_extensions.get_origin(clean_type) == typing.Dict
|
|
75
|
+
or typing_extensions.get_origin(clean_type) == dict
|
|
76
|
+
or clean_type == typing.Dict
|
|
77
|
+
) and isinstance(object_, typing.Dict):
|
|
78
|
+
key_type = typing_extensions.get_args(clean_type)[0]
|
|
79
|
+
value_type = typing_extensions.get_args(clean_type)[1]
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
key: convert_and_respect_annotation_metadata(
|
|
83
|
+
object_=value,
|
|
84
|
+
annotation=annotation,
|
|
85
|
+
inner_type=value_type,
|
|
86
|
+
direction=direction,
|
|
87
|
+
)
|
|
88
|
+
for key, value in object_.items()
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
# If you're iterating on a string, do not bother to coerce it to a sequence.
|
|
92
|
+
if not isinstance(object_, str):
|
|
93
|
+
if (
|
|
94
|
+
typing_extensions.get_origin(clean_type) == typing.Set
|
|
95
|
+
or typing_extensions.get_origin(clean_type) == set
|
|
96
|
+
or clean_type == typing.Set
|
|
97
|
+
) and isinstance(object_, typing.Set):
|
|
98
|
+
inner_type = typing_extensions.get_args(clean_type)[0]
|
|
99
|
+
return {
|
|
100
|
+
convert_and_respect_annotation_metadata(
|
|
101
|
+
object_=item,
|
|
102
|
+
annotation=annotation,
|
|
103
|
+
inner_type=inner_type,
|
|
104
|
+
direction=direction,
|
|
105
|
+
)
|
|
106
|
+
for item in object_
|
|
107
|
+
}
|
|
108
|
+
elif (
|
|
109
|
+
(
|
|
110
|
+
typing_extensions.get_origin(clean_type) == typing.List
|
|
111
|
+
or typing_extensions.get_origin(clean_type) == list
|
|
112
|
+
or clean_type == typing.List
|
|
113
|
+
)
|
|
114
|
+
and isinstance(object_, typing.List)
|
|
115
|
+
) or (
|
|
116
|
+
(
|
|
117
|
+
typing_extensions.get_origin(clean_type) == typing.Sequence
|
|
118
|
+
or typing_extensions.get_origin(clean_type) == collections.abc.Sequence
|
|
119
|
+
or clean_type == typing.Sequence
|
|
120
|
+
)
|
|
121
|
+
and isinstance(object_, typing.Sequence)
|
|
122
|
+
):
|
|
123
|
+
inner_type = typing_extensions.get_args(clean_type)[0]
|
|
124
|
+
return [
|
|
125
|
+
convert_and_respect_annotation_metadata(
|
|
126
|
+
object_=item,
|
|
127
|
+
annotation=annotation,
|
|
128
|
+
inner_type=inner_type,
|
|
129
|
+
direction=direction,
|
|
130
|
+
)
|
|
131
|
+
for item in object_
|
|
132
|
+
]
|
|
133
|
+
|
|
134
|
+
if typing_extensions.get_origin(clean_type) == typing.Union:
|
|
135
|
+
# We should be able to ~relatively~ safely try to convert keys against all
|
|
136
|
+
# member types in the union, the edge case here is if one member aliases a field
|
|
137
|
+
# of the same name to a different name from another member
|
|
138
|
+
# Or if another member aliases a field of the same name that another member does not.
|
|
139
|
+
for member in typing_extensions.get_args(clean_type):
|
|
140
|
+
object_ = convert_and_respect_annotation_metadata(
|
|
141
|
+
object_=object_,
|
|
142
|
+
annotation=annotation,
|
|
143
|
+
inner_type=member,
|
|
144
|
+
direction=direction,
|
|
145
|
+
)
|
|
146
|
+
return object_
|
|
147
|
+
|
|
148
|
+
annotated_type = _get_annotation(annotation)
|
|
149
|
+
if annotated_type is None:
|
|
150
|
+
return object_
|
|
151
|
+
|
|
152
|
+
# If the object is not a TypedDict, a Union, or other container (list, set, sequence, etc.)
|
|
153
|
+
# Then we can safely call it on the recursive conversion.
|
|
154
|
+
return object_
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _convert_mapping(
|
|
158
|
+
object_: typing.Mapping[str, object],
|
|
159
|
+
expected_type: typing.Any,
|
|
160
|
+
direction: typing.Literal["read", "write"],
|
|
161
|
+
) -> typing.Mapping[str, object]:
|
|
162
|
+
converted_object: typing.Dict[str, object] = {}
|
|
163
|
+
try:
|
|
164
|
+
annotations = typing_extensions.get_type_hints(expected_type, include_extras=True)
|
|
165
|
+
except NameError:
|
|
166
|
+
# The TypedDict contains a circular reference, so
|
|
167
|
+
# we use the __annotations__ attribute directly.
|
|
168
|
+
annotations = getattr(expected_type, "__annotations__", {})
|
|
169
|
+
aliases_to_field_names = _get_alias_to_field_name(annotations)
|
|
170
|
+
for key, value in object_.items():
|
|
171
|
+
if direction == "read" and key in aliases_to_field_names:
|
|
172
|
+
dealiased_key = aliases_to_field_names.get(key)
|
|
173
|
+
if dealiased_key is not None:
|
|
174
|
+
type_ = annotations.get(dealiased_key)
|
|
175
|
+
else:
|
|
176
|
+
type_ = annotations.get(key)
|
|
177
|
+
# Note you can't get the annotation by the field name if you're in read mode, so you must check the aliases map
|
|
178
|
+
#
|
|
179
|
+
# So this is effectively saying if we're in write mode, and we don't have a type, or if we're in read mode and we don't have an alias
|
|
180
|
+
# then we can just pass the value through as is
|
|
181
|
+
if type_ is None:
|
|
182
|
+
converted_object[key] = value
|
|
183
|
+
elif direction == "read" and key not in aliases_to_field_names:
|
|
184
|
+
converted_object[key] = convert_and_respect_annotation_metadata(
|
|
185
|
+
object_=value, annotation=type_, direction=direction
|
|
186
|
+
)
|
|
187
|
+
else:
|
|
188
|
+
converted_object[_alias_key(key, type_, direction, aliases_to_field_names)] = (
|
|
189
|
+
convert_and_respect_annotation_metadata(object_=value, annotation=type_, direction=direction)
|
|
190
|
+
)
|
|
191
|
+
return converted_object
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _get_annotation(type_: typing.Any) -> typing.Optional[typing.Any]:
|
|
195
|
+
maybe_annotated_type = typing_extensions.get_origin(type_)
|
|
196
|
+
if maybe_annotated_type is None:
|
|
197
|
+
return None
|
|
198
|
+
|
|
199
|
+
if maybe_annotated_type == typing_extensions.NotRequired:
|
|
200
|
+
type_ = typing_extensions.get_args(type_)[0]
|
|
201
|
+
maybe_annotated_type = typing_extensions.get_origin(type_)
|
|
202
|
+
|
|
203
|
+
if maybe_annotated_type == typing_extensions.Annotated:
|
|
204
|
+
return type_
|
|
205
|
+
|
|
206
|
+
return None
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def _remove_annotations(type_: typing.Any) -> typing.Any:
|
|
210
|
+
maybe_annotated_type = typing_extensions.get_origin(type_)
|
|
211
|
+
if maybe_annotated_type is None:
|
|
212
|
+
return type_
|
|
213
|
+
|
|
214
|
+
if maybe_annotated_type == typing_extensions.NotRequired:
|
|
215
|
+
return _remove_annotations(typing_extensions.get_args(type_)[0])
|
|
216
|
+
|
|
217
|
+
if maybe_annotated_type == typing_extensions.Annotated:
|
|
218
|
+
return _remove_annotations(typing_extensions.get_args(type_)[0])
|
|
219
|
+
|
|
220
|
+
return type_
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def get_alias_to_field_mapping(type_: typing.Any) -> typing.Dict[str, str]:
|
|
224
|
+
annotations = typing_extensions.get_type_hints(type_, include_extras=True)
|
|
225
|
+
return _get_alias_to_field_name(annotations)
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def get_field_to_alias_mapping(type_: typing.Any) -> typing.Dict[str, str]:
|
|
229
|
+
annotations = typing_extensions.get_type_hints(type_, include_extras=True)
|
|
230
|
+
return _get_field_to_alias_name(annotations)
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _get_alias_to_field_name(
|
|
234
|
+
field_to_hint: typing.Dict[str, typing.Any],
|
|
235
|
+
) -> typing.Dict[str, str]:
|
|
236
|
+
aliases = {}
|
|
237
|
+
for field, hint in field_to_hint.items():
|
|
238
|
+
maybe_alias = _get_alias_from_type(hint)
|
|
239
|
+
if maybe_alias is not None:
|
|
240
|
+
aliases[maybe_alias] = field
|
|
241
|
+
return aliases
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _get_field_to_alias_name(
|
|
245
|
+
field_to_hint: typing.Dict[str, typing.Any],
|
|
246
|
+
) -> typing.Dict[str, str]:
|
|
247
|
+
aliases = {}
|
|
248
|
+
for field, hint in field_to_hint.items():
|
|
249
|
+
maybe_alias = _get_alias_from_type(hint)
|
|
250
|
+
if maybe_alias is not None:
|
|
251
|
+
aliases[field] = maybe_alias
|
|
252
|
+
return aliases
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def _get_alias_from_type(type_: typing.Any) -> typing.Optional[str]:
|
|
256
|
+
maybe_annotated_type = _get_annotation(type_)
|
|
257
|
+
|
|
258
|
+
if maybe_annotated_type is not None:
|
|
259
|
+
# The actual annotations are 1 onward, the first is the annotated type
|
|
260
|
+
annotations = typing_extensions.get_args(maybe_annotated_type)[1:]
|
|
261
|
+
|
|
262
|
+
for annotation in annotations:
|
|
263
|
+
if isinstance(annotation, FieldMetadata) and annotation.alias is not None:
|
|
264
|
+
return annotation.alias
|
|
265
|
+
return None
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _alias_key(
|
|
269
|
+
key: str,
|
|
270
|
+
type_: typing.Any,
|
|
271
|
+
direction: typing.Literal["read", "write"],
|
|
272
|
+
aliases_to_field_names: typing.Dict[str, str],
|
|
273
|
+
) -> str:
|
|
274
|
+
if direction == "read":
|
|
275
|
+
return aliases_to_field_names.get(key, key)
|
|
276
|
+
return _get_alias_from_type(type_=type_) or key
|
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
# This file was auto-generated by Fern from our API Definition.
|
|
2
|
+
|
|
3
|
+
import datetime as dt
|
|
4
|
+
import inspect
|
|
5
|
+
import typing
|
|
6
|
+
import uuid
|
|
7
|
+
|
|
8
|
+
import pydantic
|
|
9
|
+
import typing_extensions
|
|
10
|
+
from .pydantic_utilities import (
|
|
11
|
+
IS_PYDANTIC_V2,
|
|
12
|
+
ModelField,
|
|
13
|
+
UniversalBaseModel,
|
|
14
|
+
get_args,
|
|
15
|
+
get_origin,
|
|
16
|
+
is_literal_type,
|
|
17
|
+
is_union,
|
|
18
|
+
parse_date,
|
|
19
|
+
parse_datetime,
|
|
20
|
+
parse_obj_as,
|
|
21
|
+
)
|
|
22
|
+
from .serialization import get_field_to_alias_mapping
|
|
23
|
+
from pydantic_core import PydanticUndefined
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class UnionMetadata:
|
|
27
|
+
discriminant: str
|
|
28
|
+
|
|
29
|
+
def __init__(self, *, discriminant: str) -> None:
|
|
30
|
+
self.discriminant = discriminant
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
Model = typing.TypeVar("Model", bound=pydantic.BaseModel)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class UncheckedBaseModel(UniversalBaseModel):
|
|
37
|
+
if IS_PYDANTIC_V2:
|
|
38
|
+
model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(extra="allow") # type: ignore # Pydantic v2
|
|
39
|
+
else:
|
|
40
|
+
|
|
41
|
+
class Config:
|
|
42
|
+
extra = pydantic.Extra.allow
|
|
43
|
+
|
|
44
|
+
@classmethod
|
|
45
|
+
def model_construct(
|
|
46
|
+
cls: typing.Type["Model"],
|
|
47
|
+
_fields_set: typing.Optional[typing.Set[str]] = None,
|
|
48
|
+
**values: typing.Any,
|
|
49
|
+
) -> "Model":
|
|
50
|
+
# Fallback construct function to the specified override below.
|
|
51
|
+
return cls.construct(_fields_set=_fields_set, **values)
|
|
52
|
+
|
|
53
|
+
# Allow construct to not validate model
|
|
54
|
+
# Implementation taken from: https://github.com/pydantic/pydantic/issues/1168#issuecomment-817742836
|
|
55
|
+
@classmethod
|
|
56
|
+
def construct(
|
|
57
|
+
cls: typing.Type["Model"],
|
|
58
|
+
_fields_set: typing.Optional[typing.Set[str]] = None,
|
|
59
|
+
**values: typing.Any,
|
|
60
|
+
) -> "Model":
|
|
61
|
+
m = cls.__new__(cls)
|
|
62
|
+
fields_values = {}
|
|
63
|
+
|
|
64
|
+
if _fields_set is None:
|
|
65
|
+
_fields_set = set(values.keys())
|
|
66
|
+
|
|
67
|
+
fields = _get_model_fields(cls)
|
|
68
|
+
populate_by_name = _get_is_populate_by_name(cls)
|
|
69
|
+
field_aliases = get_field_to_alias_mapping(cls)
|
|
70
|
+
|
|
71
|
+
for name, field in fields.items():
|
|
72
|
+
# Key here is only used to pull data from the values dict
|
|
73
|
+
# you should always use the NAME of the field to for field_values, etc.
|
|
74
|
+
# because that's how the object is constructed from a pydantic perspective
|
|
75
|
+
key = field.alias
|
|
76
|
+
if (key is None or field.alias == name) and name in field_aliases:
|
|
77
|
+
key = field_aliases[name]
|
|
78
|
+
|
|
79
|
+
if key is None or (key not in values and populate_by_name): # Added this to allow population by field name
|
|
80
|
+
key = name
|
|
81
|
+
|
|
82
|
+
if key in values:
|
|
83
|
+
if IS_PYDANTIC_V2:
|
|
84
|
+
type_ = field.annotation # type: ignore # Pydantic v2
|
|
85
|
+
else:
|
|
86
|
+
type_ = typing.cast(typing.Type, field.outer_type_) # type: ignore # Pydantic < v1.10.15
|
|
87
|
+
|
|
88
|
+
fields_values[name] = (
|
|
89
|
+
construct_type(object_=values[key], type_=type_) if type_ is not None else values[key]
|
|
90
|
+
)
|
|
91
|
+
_fields_set.add(name)
|
|
92
|
+
else:
|
|
93
|
+
default = _get_field_default(field)
|
|
94
|
+
fields_values[name] = default
|
|
95
|
+
|
|
96
|
+
# If the default values are non-null act like they've been set
|
|
97
|
+
# This effectively allows exclude_unset to work like exclude_none where
|
|
98
|
+
# the latter passes through intentionally set none values.
|
|
99
|
+
if default != None and default != PydanticUndefined:
|
|
100
|
+
_fields_set.add(name)
|
|
101
|
+
|
|
102
|
+
# Add extras back in
|
|
103
|
+
extras = {}
|
|
104
|
+
pydantic_alias_fields = [field.alias for field in fields.values()]
|
|
105
|
+
internal_alias_fields = list(field_aliases.values())
|
|
106
|
+
for key, value in values.items():
|
|
107
|
+
# If the key is not a field by name, nor an alias to a field, then it's extra
|
|
108
|
+
if (key not in pydantic_alias_fields and key not in internal_alias_fields) and key not in fields:
|
|
109
|
+
if IS_PYDANTIC_V2:
|
|
110
|
+
extras[key] = value
|
|
111
|
+
else:
|
|
112
|
+
_fields_set.add(key)
|
|
113
|
+
fields_values[key] = value
|
|
114
|
+
|
|
115
|
+
object.__setattr__(m, "__dict__", fields_values)
|
|
116
|
+
|
|
117
|
+
if IS_PYDANTIC_V2:
|
|
118
|
+
object.__setattr__(m, "__pydantic_private__", None)
|
|
119
|
+
object.__setattr__(m, "__pydantic_extra__", extras)
|
|
120
|
+
object.__setattr__(m, "__pydantic_fields_set__", _fields_set)
|
|
121
|
+
else:
|
|
122
|
+
object.__setattr__(m, "__fields_set__", _fields_set)
|
|
123
|
+
m._init_private_attributes() # type: ignore # Pydantic v1
|
|
124
|
+
return m
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _validate_collection_items_compatible(collection: typing.Any, target_type: typing.Type[typing.Any]) -> bool:
|
|
128
|
+
"""
|
|
129
|
+
Validate that all items in a collection are compatible with the target type.
|
|
130
|
+
|
|
131
|
+
Args:
|
|
132
|
+
collection: The collection to validate (list, set, or dict values)
|
|
133
|
+
target_type: The target type to validate against
|
|
134
|
+
|
|
135
|
+
Returns:
|
|
136
|
+
True if all items are compatible, False otherwise
|
|
137
|
+
"""
|
|
138
|
+
if inspect.isclass(target_type) and issubclass(target_type, pydantic.BaseModel):
|
|
139
|
+
for item in collection:
|
|
140
|
+
try:
|
|
141
|
+
# Try to validate the item against the target type
|
|
142
|
+
if isinstance(item, dict):
|
|
143
|
+
parse_obj_as(target_type, item)
|
|
144
|
+
else:
|
|
145
|
+
# If it's not a dict, it might already be the right type
|
|
146
|
+
if not isinstance(item, target_type):
|
|
147
|
+
return False
|
|
148
|
+
except Exception:
|
|
149
|
+
return False
|
|
150
|
+
return True
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _convert_undiscriminated_union_type(union_type: typing.Type[typing.Any], object_: typing.Any) -> typing.Any:
|
|
154
|
+
inner_types = get_args(union_type)
|
|
155
|
+
if typing.Any in inner_types:
|
|
156
|
+
return object_
|
|
157
|
+
|
|
158
|
+
for inner_type in inner_types:
|
|
159
|
+
# Handle lists of objects that need parsing
|
|
160
|
+
if get_origin(inner_type) is list and isinstance(object_, list):
|
|
161
|
+
list_inner_type = get_args(inner_type)[0]
|
|
162
|
+
try:
|
|
163
|
+
if inspect.isclass(list_inner_type) and issubclass(list_inner_type, pydantic.BaseModel):
|
|
164
|
+
# Validate that all items in the list are compatible with the target type
|
|
165
|
+
if _validate_collection_items_compatible(object_, list_inner_type):
|
|
166
|
+
parsed_list = [parse_obj_as(object_=item, type_=list_inner_type) for item in object_]
|
|
167
|
+
return parsed_list
|
|
168
|
+
except Exception:
|
|
169
|
+
pass
|
|
170
|
+
|
|
171
|
+
try:
|
|
172
|
+
if inspect.isclass(inner_type) and issubclass(inner_type, pydantic.BaseModel):
|
|
173
|
+
# Attempt a validated parse until one works
|
|
174
|
+
return parse_obj_as(inner_type, object_)
|
|
175
|
+
except Exception:
|
|
176
|
+
continue
|
|
177
|
+
|
|
178
|
+
# If none of the types work, try matching literal fields first, then fall back
|
|
179
|
+
# First pass: try types where all literal fields match the object's values
|
|
180
|
+
for inner_type in inner_types:
|
|
181
|
+
if inspect.isclass(inner_type) and issubclass(inner_type, pydantic.BaseModel):
|
|
182
|
+
fields = _get_model_fields(inner_type)
|
|
183
|
+
literal_fields_match = True
|
|
184
|
+
|
|
185
|
+
for field_name, field in fields.items():
|
|
186
|
+
# Check if this field has a Literal type
|
|
187
|
+
if IS_PYDANTIC_V2:
|
|
188
|
+
field_type = field.annotation # type: ignore # Pydantic v2
|
|
189
|
+
else:
|
|
190
|
+
field_type = field.outer_type_ # type: ignore # Pydantic v1
|
|
191
|
+
|
|
192
|
+
if is_literal_type(field_type): # type: ignore[arg-type]
|
|
193
|
+
field_default = _get_field_default(field)
|
|
194
|
+
name_or_alias = get_field_to_alias_mapping(inner_type).get(field_name, field_name)
|
|
195
|
+
# Get the value from the object
|
|
196
|
+
if isinstance(object_, dict):
|
|
197
|
+
object_value = object_.get(name_or_alias)
|
|
198
|
+
else:
|
|
199
|
+
object_value = getattr(object_, name_or_alias, None)
|
|
200
|
+
|
|
201
|
+
# If the literal field value doesn't match, this type is not a match
|
|
202
|
+
if object_value is not None and field_default != object_value:
|
|
203
|
+
literal_fields_match = False
|
|
204
|
+
break
|
|
205
|
+
|
|
206
|
+
# If all literal fields match, try to construct this type
|
|
207
|
+
if literal_fields_match:
|
|
208
|
+
try:
|
|
209
|
+
return construct_type(object_=object_, type_=inner_type)
|
|
210
|
+
except Exception:
|
|
211
|
+
continue
|
|
212
|
+
|
|
213
|
+
# Second pass: if no literal matches, just return the first successful cast
|
|
214
|
+
for inner_type in inner_types:
|
|
215
|
+
try:
|
|
216
|
+
return construct_type(object_=object_, type_=inner_type)
|
|
217
|
+
except Exception:
|
|
218
|
+
continue
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _convert_union_type(type_: typing.Type[typing.Any], object_: typing.Any) -> typing.Any:
|
|
222
|
+
base_type = get_origin(type_) or type_
|
|
223
|
+
union_type = type_
|
|
224
|
+
if base_type == typing_extensions.Annotated: # type: ignore[comparison-overlap]
|
|
225
|
+
union_type = get_args(type_)[0]
|
|
226
|
+
annotated_metadata = get_args(type_)[1:]
|
|
227
|
+
for metadata in annotated_metadata:
|
|
228
|
+
if isinstance(metadata, UnionMetadata):
|
|
229
|
+
try:
|
|
230
|
+
# Cast to the correct type, based on the discriminant
|
|
231
|
+
for inner_type in get_args(union_type):
|
|
232
|
+
try:
|
|
233
|
+
objects_discriminant = getattr(object_, metadata.discriminant)
|
|
234
|
+
except:
|
|
235
|
+
objects_discriminant = object_[metadata.discriminant]
|
|
236
|
+
if inner_type.__fields__[metadata.discriminant].default == objects_discriminant:
|
|
237
|
+
return construct_type(object_=object_, type_=inner_type)
|
|
238
|
+
except Exception:
|
|
239
|
+
# Allow to fall through to our regular union handling
|
|
240
|
+
pass
|
|
241
|
+
return _convert_undiscriminated_union_type(union_type, object_)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def construct_type(*, type_: typing.Type[typing.Any], object_: typing.Any) -> typing.Any:
|
|
245
|
+
"""
|
|
246
|
+
Here we are essentially creating the same `construct` method in spirit as the above, but for all types, not just
|
|
247
|
+
Pydantic models.
|
|
248
|
+
The idea is to essentially attempt to coerce object_ to type_ (recursively)
|
|
249
|
+
"""
|
|
250
|
+
# Short circuit when dealing with optionals, don't try to coerces None to a type
|
|
251
|
+
if object_ is None:
|
|
252
|
+
return None
|
|
253
|
+
|
|
254
|
+
base_type = get_origin(type_) or type_
|
|
255
|
+
is_annotated = base_type == typing_extensions.Annotated # type: ignore[comparison-overlap]
|
|
256
|
+
maybe_annotation_members = get_args(type_)
|
|
257
|
+
is_annotated_union = is_annotated and is_union(get_origin(maybe_annotation_members[0]))
|
|
258
|
+
|
|
259
|
+
if base_type == typing.Any: # type: ignore[comparison-overlap]
|
|
260
|
+
return object_
|
|
261
|
+
|
|
262
|
+
if base_type == dict:
|
|
263
|
+
if not isinstance(object_, typing.Mapping):
|
|
264
|
+
return object_
|
|
265
|
+
|
|
266
|
+
key_type, items_type = get_args(type_)
|
|
267
|
+
d = {
|
|
268
|
+
construct_type(object_=key, type_=key_type): construct_type(object_=item, type_=items_type)
|
|
269
|
+
for key, item in object_.items()
|
|
270
|
+
}
|
|
271
|
+
return d
|
|
272
|
+
|
|
273
|
+
if base_type == list:
|
|
274
|
+
if not isinstance(object_, list):
|
|
275
|
+
return object_
|
|
276
|
+
|
|
277
|
+
inner_type = get_args(type_)[0]
|
|
278
|
+
return [construct_type(object_=entry, type_=inner_type) for entry in object_]
|
|
279
|
+
|
|
280
|
+
if base_type == set:
|
|
281
|
+
if not isinstance(object_, set) and not isinstance(object_, list):
|
|
282
|
+
return object_
|
|
283
|
+
|
|
284
|
+
inner_type = get_args(type_)[0]
|
|
285
|
+
return {construct_type(object_=entry, type_=inner_type) for entry in object_}
|
|
286
|
+
|
|
287
|
+
if is_union(base_type) or is_annotated_union:
|
|
288
|
+
return _convert_union_type(type_, object_)
|
|
289
|
+
|
|
290
|
+
# Cannot do an `issubclass` with a literal type, let's also just confirm we have a class before this call
|
|
291
|
+
if (
|
|
292
|
+
object_ is not None
|
|
293
|
+
and not is_literal_type(type_)
|
|
294
|
+
and (
|
|
295
|
+
(inspect.isclass(base_type) and issubclass(base_type, pydantic.BaseModel))
|
|
296
|
+
or (
|
|
297
|
+
is_annotated
|
|
298
|
+
and inspect.isclass(maybe_annotation_members[0])
|
|
299
|
+
and issubclass(maybe_annotation_members[0], pydantic.BaseModel)
|
|
300
|
+
)
|
|
301
|
+
)
|
|
302
|
+
):
|
|
303
|
+
if IS_PYDANTIC_V2:
|
|
304
|
+
return type_.model_construct(**object_)
|
|
305
|
+
else:
|
|
306
|
+
return type_.construct(**object_)
|
|
307
|
+
|
|
308
|
+
if base_type == dt.datetime:
|
|
309
|
+
try:
|
|
310
|
+
return parse_datetime(object_)
|
|
311
|
+
except Exception:
|
|
312
|
+
return object_
|
|
313
|
+
|
|
314
|
+
if base_type == dt.date:
|
|
315
|
+
try:
|
|
316
|
+
return parse_date(object_)
|
|
317
|
+
except Exception:
|
|
318
|
+
return object_
|
|
319
|
+
|
|
320
|
+
if base_type == uuid.UUID:
|
|
321
|
+
try:
|
|
322
|
+
return uuid.UUID(object_)
|
|
323
|
+
except Exception:
|
|
324
|
+
return object_
|
|
325
|
+
|
|
326
|
+
if base_type == int:
|
|
327
|
+
try:
|
|
328
|
+
return int(object_)
|
|
329
|
+
except Exception:
|
|
330
|
+
return object_
|
|
331
|
+
|
|
332
|
+
if base_type == bool:
|
|
333
|
+
try:
|
|
334
|
+
if isinstance(object_, str):
|
|
335
|
+
stringified_object = object_.lower()
|
|
336
|
+
return stringified_object == "true" or stringified_object == "1"
|
|
337
|
+
|
|
338
|
+
return bool(object_)
|
|
339
|
+
except Exception:
|
|
340
|
+
return object_
|
|
341
|
+
|
|
342
|
+
return object_
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def _get_is_populate_by_name(model: typing.Type["Model"]) -> bool:
|
|
346
|
+
if IS_PYDANTIC_V2:
|
|
347
|
+
return model.model_config.get("populate_by_name", False) # type: ignore # Pydantic v2
|
|
348
|
+
return model.__config__.allow_population_by_field_name # type: ignore # Pydantic v1
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
PydanticField = typing.Union[ModelField, pydantic.fields.FieldInfo]
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
# Pydantic V1 swapped the typing of __fields__'s values from ModelField to FieldInfo
|
|
355
|
+
# And so we try to handle both V1 cases, as well as V2 (FieldInfo from model.model_fields)
|
|
356
|
+
def _get_model_fields(
|
|
357
|
+
model: typing.Type["Model"],
|
|
358
|
+
) -> typing.Mapping[str, PydanticField]:
|
|
359
|
+
if IS_PYDANTIC_V2:
|
|
360
|
+
return model.model_fields # type: ignore # Pydantic v2
|
|
361
|
+
else:
|
|
362
|
+
return model.__fields__ # type: ignore # Pydantic v1
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def _get_field_default(field: PydanticField) -> typing.Any:
|
|
366
|
+
try:
|
|
367
|
+
value = field.get_default() # type: ignore # Pydantic < v1.10.15
|
|
368
|
+
except:
|
|
369
|
+
value = field.default
|
|
370
|
+
if IS_PYDANTIC_V2:
|
|
371
|
+
from pydantic_core import PydanticUndefined
|
|
372
|
+
|
|
373
|
+
if value == PydanticUndefined:
|
|
374
|
+
return None
|
|
375
|
+
return value
|
|
376
|
+
return value
|