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

release: 0.15.1 #335

Merged
merged 5 commits into from
Feb 7, 2024
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
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.15.0"
".": "0.15.1"
}
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Changelog

## 0.15.1 (2024-02-07)

Full Changelog: [v0.15.0...v0.15.1](https://github.com/anthropics/anthropic-sdk-python/compare/v0.15.0...v0.15.1)

### Bug Fixes

* prevent crash when platform.architecture() is not allowed ([#334](https://github.com/anthropics/anthropic-sdk-python/issues/334)) ([fefb5c1](https://github.com/anthropics/anthropic-sdk-python/commit/fefb5c10c10054f28fcccf0d9f44204de93e9fe3))
* **types:** loosen most List params types to Iterable ([#338](https://github.com/anthropics/anthropic-sdk-python/issues/338)) ([6e7761b](https://github.com/anthropics/anthropic-sdk-python/commit/6e7761b89c9ef226bd8f7df465445526c08fdb2f))


### Chores

* **internal:** add lint command ([#337](https://github.com/anthropics/anthropic-sdk-python/issues/337)) ([2ebaf1d](https://github.com/anthropics/anthropic-sdk-python/commit/2ebaf1d6a85b638b502661735e3ffc5b58d5c241))
* **internal:** support serialising iterable types ([#336](https://github.com/anthropics/anthropic-sdk-python/issues/336)) ([ea3ed7b](https://github.com/anthropics/anthropic-sdk-python/commit/ea3ed7b8b91314721129480d164d7bf3bafec26c))

## 0.15.0 (2024-02-02)

Full Changelog: [v0.14.1...v0.15.0](https://github.com/anthropics/anthropic-sdk-python/compare/v0.14.1...v0.15.0)
Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "anthropic"
version = "0.15.0"
version = "0.15.1"
description = "The official Python library for the anthropic API"
readme = "README.md"
license = "MIT"
Expand Down Expand Up @@ -74,6 +74,10 @@ format = { chain = [
"format:ruff" = "ruff format"
"format:isort" = "isort ."

"lint" = { chain = [
"check:ruff",
"typecheck",
]}
"check:ruff" = "ruff ."
"fix:ruff" = "ruff --fix ."

Expand Down
34 changes: 28 additions & 6 deletions src/anthropic/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1836,8 +1836,12 @@ def __str__(self) -> str:


def get_platform() -> Platform:
system = platform.system().lower()
platform_name = platform.platform().lower()
try:
system = platform.system().lower()
platform_name = platform.platform().lower()
except Exception:
return "Unknown"

if "iphone" in platform_name or "ipad" in platform_name:
# Tested using Python3IDE on an iPhone 11 and Pythonista on an iPad 7
# system is Darwin and platform_name is a string like:
Expand Down Expand Up @@ -1880,8 +1884,8 @@ def platform_headers(version: str) -> Dict[str, str]:
"X-Stainless-Package-Version": version,
"X-Stainless-OS": str(get_platform()),
"X-Stainless-Arch": str(get_architecture()),
"X-Stainless-Runtime": platform.python_implementation(),
"X-Stainless-Runtime-Version": platform.python_version(),
"X-Stainless-Runtime": get_python_runtime(),
"X-Stainless-Runtime-Version": get_python_version(),
}


Expand All @@ -1897,9 +1901,27 @@ def __str__(self) -> str:
Arch = Union[OtherArch, Literal["x32", "x64", "arm", "arm64", "unknown"]]


def get_python_runtime() -> str:
try:
return platform.python_implementation()
except Exception:
return "unknown"


def get_python_version() -> str:
try:
return platform.python_version()
except Exception:
return "unknown"


def get_architecture() -> Arch:
python_bitness, _ = platform.architecture()
machine = platform.machine().lower()
try:
python_bitness, _ = platform.architecture()
machine = platform.machine().lower()
except Exception:
return "unknown"

if machine in ("arm64", "aarch64"):
return "arm64"

Expand Down
2 changes: 2 additions & 0 deletions src/anthropic/_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
is_mapping as is_mapping,
is_tuple_t as is_tuple_t,
parse_date as parse_date,
is_iterable as is_iterable,
is_sequence as is_sequence,
coerce_float as coerce_float,
is_mapping_t as is_mapping_t,
Expand All @@ -33,6 +34,7 @@
is_list_type as is_list_type,
is_union_type as is_union_type,
extract_type_arg as extract_type_arg,
is_iterable_type as is_iterable_type,
is_required_type as is_required_type,
is_annotated_type as is_annotated_type,
strip_annotated_type as strip_annotated_type,
Expand Down
9 changes: 8 additions & 1 deletion src/anthropic/_utils/_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
from ._utils import (
is_list,
is_mapping,
is_iterable,
)
from ._typing import (
is_list_type,
is_union_type,
extract_type_arg,
is_iterable_type,
is_required_type,
is_annotated_type,
strip_annotated_type,
Expand Down Expand Up @@ -157,7 +159,12 @@ def _transform_recursive(
if is_typeddict(stripped_type) and is_mapping(data):
return _transform_typeddict(data, stripped_type)

if is_list_type(stripped_type) and is_list(data):
if (
# List[T]
(is_list_type(stripped_type) and is_list(data))
# Iterable[T]
or (is_iterable_type(stripped_type) and is_iterable(data) and not isinstance(data, str))
):
inner_type = extract_type_arg(stripped_type, 0)
return [_transform_recursive(d, annotation=annotation, inner_type=inner_type) for d in data]

Expand Down
9 changes: 8 additions & 1 deletion src/anthropic/_utils/_typing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from typing import Any, TypeVar, cast
from typing import Any, TypeVar, Iterable, cast
from collections import abc as _c_abc
from typing_extensions import Required, Annotated, get_args, get_origin

from .._types import InheritsGeneric
Expand All @@ -15,6 +16,12 @@ def is_list_type(typ: type) -> bool:
return (get_origin(typ) or typ) == list


def is_iterable_type(typ: type) -> bool:
"""If the given type is `typing.Iterable[T]`"""
origin = get_origin(typ) or typ
return origin == Iterable or origin == _c_abc.Iterable


def is_union_type(typ: type) -> bool:
return _is_union(get_origin(typ))

Expand Down
4 changes: 4 additions & 0 deletions src/anthropic/_utils/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ def is_list(obj: object) -> TypeGuard[list[object]]:
return isinstance(obj, list)


def is_iterable(obj: object) -> TypeGuard[Iterable[object]]:
return isinstance(obj, Iterable)


def deepcopy_minimal(item: _T) -> _T:
"""Minimal reimplementation of copy.deepcopy() that will only copy certain object types:

Expand Down
2 changes: 1 addition & 1 deletion src/anthropic/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.

__title__ = "anthropic"
__version__ = "0.15.0" # x-release-please-version
__version__ = "0.15.1" # x-release-please-version
30 changes: 15 additions & 15 deletions src/anthropic/resources/beta/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import List, overload
from typing import List, Iterable, overload
from functools import partial
from typing_extensions import Literal

Expand Down Expand Up @@ -45,7 +45,7 @@ def create(
self,
*,
max_tokens: int,
messages: List[MessageParam],
messages: Iterable[MessageParam],
model: str,
metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN,
stop_sequences: List[str] | NotGiven = NOT_GIVEN,
Expand Down Expand Up @@ -215,7 +215,7 @@ def create(
self,
*,
max_tokens: int,
messages: List[MessageParam],
messages: Iterable[MessageParam],
model: str,
stream: Literal[True],
metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN,
Expand Down Expand Up @@ -385,7 +385,7 @@ def create(
self,
*,
max_tokens: int,
messages: List[MessageParam],
messages: Iterable[MessageParam],
model: str,
stream: bool,
metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN,
Expand Down Expand Up @@ -555,7 +555,7 @@ def create(
self,
*,
max_tokens: int,
messages: List[MessageParam],
messages: Iterable[MessageParam],
model: str,
metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN,
stop_sequences: List[str] | NotGiven = NOT_GIVEN,
Expand Down Expand Up @@ -602,7 +602,7 @@ def stream(
self,
*,
max_tokens: int,
messages: List[MessageParam],
messages: Iterable[MessageParam],
model: str,
metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN,
stop_sequences: List[str] | NotGiven = NOT_GIVEN,
Expand All @@ -625,7 +625,7 @@ def stream(
self,
*,
max_tokens: int,
messages: List[MessageParam],
messages: Iterable[MessageParam],
model: str,
metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN,
stop_sequences: List[str] | NotGiven = NOT_GIVEN,
Expand All @@ -648,7 +648,7 @@ def stream(
self,
*,
max_tokens: int,
messages: List[MessageParam],
messages: Iterable[MessageParam],
model: str,
metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN,
stop_sequences: List[str] | NotGiven = NOT_GIVEN,
Expand Down Expand Up @@ -713,7 +713,7 @@ async def create(
self,
*,
max_tokens: int,
messages: List[MessageParam],
messages: Iterable[MessageParam],
model: str,
metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN,
stop_sequences: List[str] | NotGiven = NOT_GIVEN,
Expand Down Expand Up @@ -883,7 +883,7 @@ async def create(
self,
*,
max_tokens: int,
messages: List[MessageParam],
messages: Iterable[MessageParam],
model: str,
stream: Literal[True],
metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN,
Expand Down Expand Up @@ -1053,7 +1053,7 @@ async def create(
self,
*,
max_tokens: int,
messages: List[MessageParam],
messages: Iterable[MessageParam],
model: str,
stream: bool,
metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN,
Expand Down Expand Up @@ -1223,7 +1223,7 @@ async def create(
self,
*,
max_tokens: int,
messages: List[MessageParam],
messages: Iterable[MessageParam],
model: str,
metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN,
stop_sequences: List[str] | NotGiven = NOT_GIVEN,
Expand Down Expand Up @@ -1270,7 +1270,7 @@ def stream(
self,
*,
max_tokens: int,
messages: List[MessageParam],
messages: Iterable[MessageParam],
model: str,
metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN,
stop_sequences: List[str] | NotGiven = NOT_GIVEN,
Expand All @@ -1293,7 +1293,7 @@ def stream(
self,
*,
max_tokens: int,
messages: List[MessageParam],
messages: Iterable[MessageParam],
model: str,
metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN,
stop_sequences: List[str] | NotGiven = NOT_GIVEN,
Expand All @@ -1316,7 +1316,7 @@ def stream(
self,
*,
max_tokens: int,
messages: List[MessageParam],
messages: Iterable[MessageParam],
model: str,
metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN,
stop_sequences: List[str] | NotGiven = NOT_GIVEN,
Expand Down
4 changes: 2 additions & 2 deletions src/anthropic/types/beta/message_create_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import List, Union, Optional
from typing import List, Union, Iterable, Optional
from typing_extensions import Literal, Required, TypedDict

from .message_param import MessageParam
Expand All @@ -22,7 +22,7 @@ class MessageCreateParamsBase(TypedDict, total=False):
for details.
"""

messages: Required[List[MessageParam]]
messages: Required[Iterable[MessageParam]]
"""Input messages.

Our models are trained to operate on alternating `user` and `assistant`
Expand Down
4 changes: 2 additions & 2 deletions src/anthropic/types/beta/message_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import List, Union
from typing import Union, Iterable
from typing_extensions import Literal, Required, TypedDict

from .text_block_param import TextBlockParam
Expand All @@ -11,6 +11,6 @@


class MessageParam(TypedDict, total=False):
content: Required[Union[str, List[TextBlockParam]]]
content: Required[Union[str, Iterable[TextBlockParam]]]

role: Required[Literal["user", "assistant"]]
34 changes: 33 additions & 1 deletion tests/test_transform.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Any, List, Union, Optional
from typing import Any, List, Union, Iterable, Optional, cast
from datetime import date, datetime
from typing_extensions import Required, Annotated, TypedDict

Expand Down Expand Up @@ -265,3 +265,35 @@ def test_pydantic_default_field() -> None:
assert model.with_none_default == "bar"
assert model.with_str_default == "baz"
assert transform(model, Any) == {"with_none_default": "bar", "with_str_default": "baz"}


class TypedDictIterableUnion(TypedDict):
foo: Annotated[Union[Bar8, Iterable[Baz8]], PropertyInfo(alias="FOO")]


class Bar8(TypedDict):
foo_bar: Annotated[str, PropertyInfo(alias="fooBar")]


class Baz8(TypedDict):
foo_baz: Annotated[str, PropertyInfo(alias="fooBaz")]


def test_iterable_of_dictionaries() -> None:
assert transform({"foo": [{"foo_baz": "bar"}]}, TypedDictIterableUnion) == {"FOO": [{"fooBaz": "bar"}]}
assert cast(Any, transform({"foo": ({"foo_baz": "bar"},)}, TypedDictIterableUnion)) == {"FOO": [{"fooBaz": "bar"}]}

def my_iter() -> Iterable[Baz8]:
yield {"foo_baz": "hello"}
yield {"foo_baz": "world"}

assert transform({"foo": my_iter()}, TypedDictIterableUnion) == {"FOO": [{"fooBaz": "hello"}, {"fooBaz": "world"}]}


class TypedDictIterableUnionStr(TypedDict):
foo: Annotated[Union[str, Iterable[Baz8]], PropertyInfo(alias="FOO")]


def test_iterable_union_str() -> None:
assert transform({"foo": "bar"}, TypedDictIterableUnionStr) == {"FOO": "bar"}
assert cast(Any, transform(iter([{"foo_baz": "bar"}]), Union[str, Iterable[Baz8]])) == [{"fooBaz": "bar"}]
Loading