diff --git a/chanfig/flat_dict.py b/chanfig/flat_dict.py index 1608e952..b527e412 100644 --- a/chanfig/flat_dict.py +++ b/chanfig/flat_dict.py @@ -302,6 +302,10 @@ def set(self, name: Any, value: Any) -> None: if name in self and isinstance(self.get(name), Variable): self.get(name).set(value) else: + if name in get_annotations(self): + anno = get_annotations(self)[name] + if isinstance(anno, type) and not isinstance(value, anno): + value = anno(value) dict.__setitem__(self, name, value) def __setitem__(self, name: Any, value: Any) -> None: diff --git a/tests/test_flat_dict.py b/tests/test_flat_dict.py index 474f4235..4576ed65 100644 --- a/tests/test_flat_dict.py +++ b/tests/test_flat_dict.py @@ -78,9 +78,8 @@ def test_validate(self): ConfigDict(int_value=1, str_value="1", float_value=1.0) with raises(TypeError): ConfigDict(int_value="1", str_value="1", float_value=1.0) - with raises(TypeError): - self.dict.int_value = "1" - self.dict.validate() + self.dict.int_value = "1" + assert isinstance(self.dict.int_value, int) ConfigDict(list_int=[1, 2, 3]) with raises(TypeError): ConfigDict(list_int=[1, "2", 3])