diff --git a/dataconf/utils.py b/dataconf/utils.py index 943e4e8..89ee85d 100644 --- a/dataconf/utils.py +++ b/dataconf/utils.py @@ -6,6 +6,7 @@ from enum import Enum from enum import IntEnum from inspect import isclass +from pathlib import Path from typing import Any, Literal from typing import Dict @@ -200,6 +201,9 @@ def __parse(value: any, clazz: Type, path: str, strict: bool, ignore_unexpected: raise TypeConfigException(f"expected str or int at {path}, got {type(value)}") + if isclass(clazz) and issubclass(clazz, Path): + return clazz.__call__(value) + if get_origin(clazz) is (Literal): if value in args: return value diff --git a/tests/test_parse.py b/tests/test_parse.py index 9ee9c96..c9cf37e 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -5,6 +5,7 @@ from enum import Enum from enum import IntEnum import os +from pathlib import Path from typing import Any, Literal from typing import Dict from typing import List @@ -269,6 +270,16 @@ class A: """ assert loads(conf_value, A) == A(b=IntColor.GREEN) + def test_path(self) -> None: + @dataclass + class P: + p: Path + + conf_name = """ + p = /tmp/test.yaml + """ + assert loads(conf_name, P) == P(p=Path("/tmp/test.yaml")) + def test_datetime(self) -> None: @dataclass class A: