Skip to content
Open
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
6 changes: 4 additions & 2 deletions src/anthropic/_utils/_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ def _transform_recursive(
return _transform_typeddict(data, stripped_type)

if origin == dict and is_mapping(data):
items_type = get_args(stripped_type)[1]
args = get_args(stripped_type)
items_type = args[1] if len(args) >= 2 else Any
return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}

if (
Expand Down Expand Up @@ -346,7 +347,8 @@ async def _async_transform_recursive(
return await _async_transform_typeddict(data, stripped_type)

if origin == dict and is_mapping(data):
items_type = get_args(stripped_type)[1]
args = get_args(stripped_type)
items_type = args[1] if len(args) >= 2 else Any
return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}

if (
Expand Down
9 changes: 9 additions & 0 deletions tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,12 @@ async def test_strips_notgiven(use_async: bool) -> None:
async def test_strips_omit(use_async: bool) -> None:
assert await transform({"foo_bar": "bar"}, Foo1, use_async) == {"fooBar": "bar"}
assert await transform({"foo_bar": omit}, Foo1, use_async) == {}


@parametrize
@pytest.mark.asyncio
async def test_transform_recursive_bare_dict_no_index_error(use_async: bool) -> None:
# A bare (unparameterized) `dict` annotation must not raise IndexError when
# get_args(dict) returns () and the code tries to index position [1].
result = await transform({"key": "value"}, dict, use_async)
assert result == {"key": "value"}