Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(internal): updates #148

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 9 additions & 3 deletions src/anthropic/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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:

Expand Down
3 changes: 2 additions & 1 deletion src/anthropic/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/anthropic/_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/anthropic/_utils/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down