From 7965492be94921934d733692642ea00907c8fbec Mon Sep 17 00:00:00 2001 From: isra17 Date: Fri, 11 Nov 2022 12:36:58 -0500 Subject: [PATCH] Support TypedDict field as Dict[str, Any] --- changelog.d/237.change.rst | 1 + src/desert/_make.py | 21 ++++++++++++++++++++- tests/test_make.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 changelog.d/237.change.rst diff --git a/changelog.d/237.change.rst b/changelog.d/237.change.rst new file mode 100644 index 0000000..a604775 --- /dev/null +++ b/changelog.d/237.change.rst @@ -0,0 +1 @@ +Add `TypedDict` subclass support to fields. These are treated the same as `Dict[str, Any]`. diff --git a/src/desert/_make.py b/src/desert/_make.py index ca37d21..6b89ba1 100644 --- a/src/desert/_make.py +++ b/src/desert/_make.py @@ -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 _is_typeddict(typ): + field = marshmallow.fields.Dict( + keys=marshmallow.fields.String, + values=marshmallow.fields.Raw, + ) + # Nested dataclasses forward_reference = getattr(typ, "__forward_arg__", None) @@ -370,6 +377,18 @@ def _get_field_default( raise TypeError(field) +def _is_typeddict(typ: t.Any) -> bool: + # typing_inspect misses some case. + # python>=3.10: use t.is_typeddict + if hasattr(t, "is_typeddict"): + return t.cast(bool, t.is_typeddict(typ)) # type: ignore[attr-defined] + # python>=3.8; <3.10: Reimplement t.is_typeddict + if hasattr(t, "_TypedDictMeta"): + return isinstance(typ, t._TypedDictMeta) # type: ignore[attr-defined] + # Fallback to typing_inspect + return typing_inspect.typed_dict_keys(typ) is not None + + @attr.frozen class _DesertSentinel: pass diff --git a/tests/test_make.py b/tests/test_make.py index 9a2991c..10414ec 100644 --- a/tests/test_make.py +++ b/tests/test_make.py @@ -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] @@ -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=(