I want to create a python dataclass to hold program settings (e.g., file Paths) read from a yaml configuration file.
The issue is that my dataclass (Config
) declares a ROOT_PATH
field having a type of Path
. However, when I print the field type, <class 'str'>
is displayed. How can I get my python dataclass to understand that the setting in my yaml configuration file is a Path object rather than just a plain string?
# config.yml
---
ROOT_PATH: Path("/root/path")
# constants.py
from dataclasses import dataclass
from pathlib import Path
import yaml
@dataclass
class Config:
ROOT_PATH: Path
if __name__ == "__main__":
with open("./config.yml") as file:
yml = yaml.safe_load(file.read())
config = Config(**yml)
print(f"Field type: {type(config.ROOT_PATH)}")
Output:
Field type: <class 'str'>