Support pathlib.Path in YAMLWizard
assafge opened this issue · 3 comments
- Dataclass Wizard version:0.22.2
- Python version:3.8.16
- Operating System:Ubuntu 22
Description
class pathlib.Path is not supported.
I've tried to go over the code, and it seems that the hook for pathlib.Path is not implemented
What I Did
@dataclass
class Test(YAMLWizard):
path: Path
c = Test('test/path')
c
Test(path='test/path')
c.to_yaml_file('test.yaml')
with open('test.yaml') as f:
print('\n'.join(f.readlines()))
path: test/path
read_c = Test.from_yaml_file('test.yaml')
Traceback (most recent call last):
File "/snap/pycharm-community/312/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<input>", line 1, in <module>
File "/home/assaf/.virtualenvs/insptool/lib/python3.8/site-packages/dataclass_wizard/wizard_mixins.py", line 147, in from_yaml_file
return cls.from_yaml(in_file, decoder=decoder,
File "/home/assaf/.virtualenvs/insptool/lib/python3.8/site-packages/dataclass_wizard/wizard_mixins.py", line 136, in from_yaml
return fromdict(cls, o) if isinstance(o, dict) else fromlist(cls, o)
File "/home/assaf/.virtualenvs/insptool/lib/python3.8/site-packages/dataclass_wizard/loaders.py", line 534, in fromdict
load = load_func_for_dataclass(cls)
File "/home/assaf/.virtualenvs/insptool/lib/python3.8/site-packages/dataclass_wizard/loaders.py", line 581, in load_func_for_dataclass
field_to_parser = dataclass_field_to_load_parser(cls_loader, cls, config)
File "/home/assaf/.virtualenvs/insptool/lib/python3.8/site-packages/dataclass_wizard/class_helper.py", line 120, in dataclass_field_to_load_parser
return _setup_load_config_for_cls(cls_loader, cls, config, save)
File "/home/assaf/.virtualenvs/insptool/lib/python3.8/site-packages/dataclass_wizard/class_helper.py", line 189, in _setup_load_config_for_cls
name_to_parser[f.name] = cls_loader.get_parser_for_annotation(
File "/home/assaf/.virtualenvs/insptool/lib/python3.8/site-packages/dataclass_wizard/loaders.py", line 437, in get_parser_for_annotation
raise ParseError(
dataclass_wizard.errors.ParseError: Failure parsing field `None` in class `None`. Expected a type Path, got NoneType.
value: None
error: Provided type is not currently supported.
unsupported_type: <class 'pathlib.Path'>
I think i have the solution for this, and I've tried to push a feature branch and got the following error:
remote: Permission to rnag/dataclass-wizard.git denied to assafge. fatal: unable to access 'https://github.com/rnag/dataclass-wizard.git/': The requested URL returned error: 403
Hi @assafge, thanks for opening this PR, and for willing to contribute to this issue.
Unfortunately, pushing to the repo is restricted to project owners so it wouldn't be the best way for adding changes.
The ideal approach that I would recommend for developers would be:
- Fork the project repo - i.e. so that it is under
<your user>/dataclass-wizard
- Add - or copy over - the changes to the main (or a dev) branch under your forked repo
- Create a Pull Request (PR) from the forked repo to the main branch under the project repo
rnag/dataclass-wizard
.
Also see Pull Request Guidelines and ensure that test cases forpathlib.Path
are added.
The above info is also covered in the Contributing guide in the docs.
Hope this is helpful, and look forward to reviewing the (much-needed) changes for this issue! 😃