diff --git a/pyproject.toml b/pyproject.toml index fbc9c34a..5bd78c4b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,6 +34,7 @@ nox = "^2023.4.22" nox-poetry = "^1.0.3" + [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" diff --git a/src/anthropic/_compat.py b/src/anthropic/_compat.py index f8e73e40..fed1df05 100644 --- a/src/anthropic/_compat.py +++ b/src/anthropic/_compat.py @@ -120,10 +120,10 @@ def model_copy(model: _ModelT) -> _ModelT: return model.copy() # type: ignore -def model_json(model: pydantic.BaseModel) -> str: +def model_json(model: pydantic.BaseModel, *, indent: int | None = None) -> str: if PYDANTIC_V2: - return model.model_dump_json() - return model.json() # type: ignore + return model.model_dump_json(indent=indent) + return model.json(indent=indent) # type: ignore def model_dump(model: pydantic.BaseModel) -> dict[str, Any]: @@ -132,6 +132,12 @@ def model_dump(model: pydantic.BaseModel) -> dict[str, Any]: return cast("dict[str, Any]", model.dict()) # pyright: ignore[reportDeprecated, reportUnnecessaryCast] +def model_parse(model: type[_ModelT], data: Any) -> _ModelT: + if PYDANTIC_V2: + return model.model_validate(data) + return model.parse_obj(data) # pyright: ignore[reportDeprecated] + + # generic models if TYPE_CHECKING: diff --git a/src/anthropic/_types.py b/src/anthropic/_types.py index 125e1fa2..7ed4bc8a 100644 --- a/src/anthropic/_types.py +++ b/src/anthropic/_types.py @@ -31,7 +31,8 @@ _T = TypeVar("_T") # Approximates httpx internal ProxiesTypes and RequestFiles types -ProxiesTypes = Union[str, Proxy, Dict[str, Union[None, str, Proxy]]] +ProxiesDict = Dict[str, Union[None, str, Proxy]] +ProxiesTypes = Union[str, Proxy, ProxiesDict] FileContent = Union[IO[bytes], bytes] FileTypes = Union[ # file (or bytes) diff --git a/src/anthropic/_utils/__init__.py b/src/anthropic/_utils/__init__.py index 05a82c73..b45dc1b1 100644 --- a/src/anthropic/_utils/__init__.py +++ b/src/anthropic/_utils/__init__.py @@ -1,6 +1,7 @@ from ._utils import flatten as flatten from ._utils import is_dict as is_dict from ._utils import is_list as is_list +from ._utils import is_given as is_given from ._utils import is_mapping as is_mapping from ._utils import parse_date as parse_date from ._utils import coerce_float as coerce_float diff --git a/src/anthropic/_utils/_utils.py b/src/anthropic/_utils/_utils.py index a5541bec..dde4b457 100644 --- a/src/anthropic/_utils/_utils.py +++ b/src/anthropic/_utils/_utils.py @@ -7,7 +7,7 @@ from pathlib import Path from typing_extensions import Required, Annotated, TypeGuard, get_args, get_origin -from .._types import NotGiven, FileTypes +from .._types import NotGiven, FileTypes, NotGivenOr from .._compat import is_union as _is_union from .._compat import parse_date as parse_date from .._compat import parse_datetime as parse_datetime @@ -100,6 +100,10 @@ def _extract_items( return [] +def is_given(obj: NotGivenOr[_T]) -> TypeGuard[_T]: + return not isinstance(obj, NotGiven) + + # Type safe methods for narrowing types with TypeVars. # The default narrowing for isinstance(obj, dict) is dict[unknown, unknown], # however this cause Pyright to rightfully report errors. As we know we don't