Skip to content

Commit

Permalink
Support TypedDict field as Dict[str, Any]
Browse files Browse the repository at this point in the history
  • Loading branch information
isra17 committed Nov 17, 2022
1 parent 95117b0 commit 62f510a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/237.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `TypedDict` subclass support to fields. These are treated the same as `Dict[str, Any]`.
9 changes: 8 additions & 1 deletion src/desert/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,18 @@ def field_for_schema(
field = field_for_schema(newtype_supertype, default=default)

# enumerations
if type(typ) is enum.EnumMeta:
elif type(typ) is enum.EnumMeta:
import marshmallow_enum

field = marshmallow_enum.EnumField(typ, metadata=metadata)

# TypedDict
elif typing_inspect.typed_dict_keys(typ) is not None:
field = marshmallow.fields.Dict(
keys=marshmallow.fields.String,
values=marshmallow.fields.Raw,
)

# Nested dataclasses
forward_reference = getattr(typ, "__forward_arg__", None)

Expand Down
29 changes: 29 additions & 0 deletions tests/test_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def dataclass_param(request: _pytest.fixtures.SubRequest) -> DataclassModule:
return module


@pytest.fixture
def typeddict() -> None:
try:
from typing import TypedDict
except ImportError:
raise pytest.skip("No TypedDict support")


class AssertLoadDumpProtocol(typing_extensions.Protocol):
def __call__(
self, schema: marshmallow.Schema, loaded: t.Any, dumped: t.Dict[t.Any, t.Any]
Expand Down Expand Up @@ -437,6 +445,27 @@ class A:
assert_dump_load(schema=schema, loaded=loaded, dumped=dumped)


def test_typeddict(
module: DataclassModule,
assert_dump_load: AssertLoadDumpProtocol,
typeddict: None,
) -> None:
"""Test dataclasses with basic TypedDict support"""

class B(t.TypedDict):
x: int

@module.dataclass
class A:
x: B

schema = desert.schema_class(A)()
dumped = {"x": {"x": 1}}
loaded = A(x={"x": 1}) # type: ignore[call-arg]

assert_dump_load(schema=schema, loaded=loaded, dumped=dumped)


@pytest.mark.xfail(
strict=True,
reason=(
Expand Down

0 comments on commit 62f510a

Please sign in to comment.