diff --git a/news/473.feature b/news/473.feature index 09a46d524..01061c606 100644 --- a/news/473.feature +++ b/news/473.feature @@ -1 +1 @@ -Add tentative support for typing.TypedDict +Add minimal support for typing.TypedDict diff --git a/omegaconf/omegaconf.py b/omegaconf/omegaconf.py index 3a87f1934..ec186d55a 100644 --- a/omegaconf/omegaconf.py +++ b/omegaconf/omegaconf.py @@ -761,7 +761,7 @@ def _node_wrap( ref_type: Any = None, ) -> Node: node: Node - is_dict = type(value) is dict or is_dict_annotation(type_) + is_dict = is_primitive_dict(value) or is_dict_annotation(type_) is_list = ( type(value) in (list, tuple) or is_list_annotation(type_) diff --git a/tests/structured_conf/data/attr_classes.py b/tests/structured_conf/data/attr_classes.py index 29f710e05..d7a8d5626 100644 --- a/tests/structured_conf/data/attr_classes.py +++ b/tests/structured_conf/data/attr_classes.py @@ -27,7 +27,7 @@ def __eq__(self, other: Any) -> Any: if sys.version_info >= (3, 8): # pragma: no cover class TypedDictSubclass(TypedDict): - foo: str + foo: int @attr.s(auto_attribs=True) diff --git a/tests/structured_conf/test_structured_config.py b/tests/structured_conf/test_structured_config.py index a6e6b1759..783968483 100644 --- a/tests/structured_conf/test_structured_config.py +++ b/tests/structured_conf/test_structured_config.py @@ -462,8 +462,12 @@ def test_dict_field(self, class_type: str) -> None: def test_typed_dict_field(self, class_type: str) -> None: module: Any = import_module(class_type) input_ = module.WithTypedDictField - conf = OmegaConf.structured(input_(dict={"foo": "bar"})) - assert conf.dict["foo"] == "bar" + conf = OmegaConf.structured(input_(dict={"foo": 10})) + assert conf.dict["foo"] == 10 + + # typed dicts does not currently runtime type safety. + conf = OmegaConf.merge(conf, {"dict": {"foo": "not_failed"}}) + assert conf.dict["foo"] == "not_failed" def test_merged_type1(self, class_type: str) -> None: # Test that the merged type is that of the last merged config