diff --git a/src/deepgram/core/client_wrapper.py b/src/deepgram/core/client_wrapper.py index 1db0612d..93446e05 100644 --- a/src/deepgram/core/client_wrapper.py +++ b/src/deepgram/core/client_wrapper.py @@ -3,6 +3,7 @@ import typing import httpx + from ..environment import DeepgramClientEnvironment from .http_client import AsyncHttpClient, HttpClient @@ -26,7 +27,7 @@ def get_headers(self) -> typing.Dict[str, str]: "X-Fern-Language": "Python", "X-Fern-SDK-Name": "deepgram", # x-release-please-start-version - "X-Fern-SDK-Version": "5.2.0", + "X-Fern-SDK-Version": "5.2.0", # x-release-please-end **(self.get_custom_headers() or {}), } diff --git a/src/deepgram/core/http_client.py b/src/deepgram/core/http_client.py index e4173f99..eced438b 100644 --- a/src/deepgram/core/http_client.py +++ b/src/deepgram/core/http_client.py @@ -10,13 +10,14 @@ from random import random import httpx +from httpx._types import RequestFiles + from .file import File, convert_file_dict_to_httpx_tuples from .force_multipart import FORCE_MULTIPART from .jsonable_encoder import jsonable_encoder from .query_encoder import encode_query from .remove_none_from_dict import remove_none_from_dict from .request_options import RequestOptions -from httpx._types import RequestFiles INITIAL_RETRY_DELAY_SECONDS = 0.5 MAX_RETRY_DELAY_SECONDS = 10 diff --git a/src/deepgram/core/pydantic_utilities.py b/src/deepgram/core/pydantic_utilities.py index 8906cdfa..d941fac7 100644 --- a/src/deepgram/core/pydantic_utilities.py +++ b/src/deepgram/core/pydantic_utilities.py @@ -3,17 +3,22 @@ # nopycln: file import datetime as dt from collections import defaultdict -from typing import Any, Callable, ClassVar, Dict, List, Mapping, Optional, Set, Tuple, Type, TypeVar, Union, cast +from functools import lru_cache +from typing import (Any, Callable, ClassVar, Dict, List, Mapping, Optional, + Set, Tuple, Type, TypeVar, Union, cast) import pydantic +from deepgram.core.serialization import convert_and_respect_annotation_metadata + IS_PYDANTIC_V2 = pydantic.VERSION.startswith("2.") if IS_PYDANTIC_V2: from pydantic.v1.datetime_parse import parse_date as parse_date from pydantic.v1.datetime_parse import parse_datetime as parse_datetime from pydantic.v1.fields import ModelField as ModelField - from pydantic.v1.json import ENCODERS_BY_TYPE as encoders_by_type # type: ignore[attr-defined] + from pydantic.v1.json import \ + ENCODERS_BY_TYPE as encoders_by_type # type: ignore[attr-defined] from pydantic.v1.typing import get_args as get_args from pydantic.v1.typing import get_origin as get_origin from pydantic.v1.typing import is_literal_type as is_literal_type @@ -28,9 +33,10 @@ from pydantic.typing import is_literal_type as is_literal_type # type: ignore[no-redef] from pydantic.typing import is_union as is_union # type: ignore[no-redef] +from typing_extensions import TypeAlias + from .datetime_utils import serialize_datetime from .serialization import convert_and_respect_annotation_metadata -from typing_extensions import TypeAlias T = TypeVar("T") Model = TypeVar("Model", bound=pydantic.BaseModel) @@ -39,7 +45,7 @@ def parse_obj_as(type_: Type[T], object_: Any) -> T: dealiased_object = convert_and_respect_annotation_metadata(object_=object_, annotation=type_, direction="read") if IS_PYDANTIC_V2: - adapter = pydantic.TypeAdapter(type_) # type: ignore[attr-defined] + adapter = get_type_adapter(type_) return adapter.validate_python(dealiased_object) return pydantic.parse_obj_as(type_, dealiased_object) @@ -256,3 +262,8 @@ def _get_field_default(field: PydanticField) -> Any: return None return value return value + + +@lru_cache(maxsize=128) +def get_type_adapter(type_): + return pydantic.TypeAdapter(type_) # type: ignore[attr-defined]