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
10 changes: 8 additions & 2 deletions src/groq/_utils/_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ 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)
if not args:
return data
items_type = args[1]
return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}

if (
Expand Down Expand Up @@ -346,7 +349,10 @@ 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)
if not args:
return data
items_type = args[1]
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 @@ -453,6 +453,15 @@ async def test_strips_notgiven(use_async: bool) -> None:
assert await transform({"foo_bar": not_given}, Foo1, use_async) == {}


@parametrize
@pytest.mark.asyncio
async def test_bare_dict_annotation_no_indexerror(use_async: bool) -> None:
# A bare `dict` annotation (no type args) previously raised IndexError because
# get_args(dict) returns () and the code unconditionally indexed [1].
result = await transform({"key": {"a": 1}}, Annotated[dict, PropertyInfo(alias="key")], use_async)
assert result == {"key": {"a": 1}}


@parametrize
@pytest.mark.asyncio
async def test_strips_omit(use_async: bool) -> None:
Expand Down