Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for pathlib.Path type #126

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dataconf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Loading