Skip to content

Commit

Permalink
chore(internal): fix type traversing dictionary params (#2434)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] committed Feb 6, 2025
1 parent c6ba380 commit 5f76235
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/cloudflare/_utils/_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
is_annotated_type,
strip_annotated_type,
)
from .._compat import model_dump, is_typeddict
from .._compat import get_origin, model_dump, is_typeddict

_T = TypeVar("_T")

Expand Down Expand Up @@ -164,9 +164,14 @@ def _transform_recursive(
inner_type = annotation

stripped_type = strip_annotated_type(inner_type)
origin = get_origin(stripped_type) or stripped_type
if is_typeddict(stripped_type) and is_mapping(data):
return _transform_typeddict(data, stripped_type)

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

if (
# List[T]
(is_list_type(stripped_type) and is_list(data))
Expand Down Expand Up @@ -307,9 +312,14 @@ async def _async_transform_recursive(
inner_type = annotation

stripped_type = strip_annotated_type(inner_type)
origin = get_origin(stripped_type) or stripped_type
if is_typeddict(stripped_type) and is_mapping(data):
return await _async_transform_typeddict(data, stripped_type)

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

if (
# List[T]
(is_list_type(stripped_type) and is_list(data))
Expand Down
11 changes: 10 additions & 1 deletion tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down Expand Up @@ -388,6 +388,15 @@ def my_iter() -> Iterable[Baz8]:
}


@parametrize
@pytest.mark.asyncio
async def test_dictionary_items(use_async: bool) -> None:
class DictItems(TypedDict):
foo_baz: Annotated[str, PropertyInfo(alias="fooBaz")]

assert await transform({"foo": {"foo_baz": "bar"}}, Dict[str, DictItems], use_async) == {"foo": {"fooBaz": "bar"}}


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

Expand Down

0 comments on commit 5f76235

Please sign in to comment.