-
Notifications
You must be signed in to change notification settings - Fork 116
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
add pathlib.Path support #873
Conversation
This pull request introduces 1 alert when merging 1df8a05 into 5e73ee6 - view on LGTM.com new alerts:
|
Closes #97. |
Thanks very much for taking the time to create this PR, @gwenzek. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
>>> OmegaConf.create({"foo": Path("bar")})
Traceback (most recent call last):
...
omegaconf.errors.UnsupportedValueType: Value 'PosixPath' is not a supported primitive type
full_key: foo
object_type=dict
I think we should support the above use-case. This would probably require updating the is_primitive_type
function
good point, I've added a test case for this, Note that I've extrcted the list of base types to a constant for cosmetic reasons. |
Thanks @gwenzek. The implementation looks good to me. |
I rebased to resolve the conflict with the new |
Demo of the API: >>> from omegaconf import OmegaConf; from pathlib import Path
>>> OmegaConf.create({"foo": Path("hello.txt")})
{'foo': PosixPath('hello.txt')}
>>> cfg = OmegaConf.create({"foo": Path("hello.txt")})
>>> print(OmegaConf.to_yaml(cfg))
foo: !!python/object/apply:pathlib.PosixPath
- hello.txt
>>> OmegaConf.create(OmegaConf.to_yaml(cfg))
{'foo': PosixPath('hello.txt')} Notes:
cc @rsokl, @odelalleau |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks great!
Thanks a lot @Jasha10 for adding the necessary tests and a updating the documentation. |
Awesome! Thanks for getting this in Hydra! |
Add support for Path in Omegaconf.
Path type will never be automatically inferred when loading from YAML, so we will continue creating StringNode by default.
But if you use
structured
or_promote
with a dataclass using Path some ofStringNode
can be converted toPathNode
eg:https://github.com/gwenzek/omegaconf/blob/1df8a05f9f740b7085fb65fe7a4d8b1e034f2ea6/tests/structured_conf/test_structured_config.py#L819-L833
This change is backward compatible, because code that currently use dataclass with Path fields currently see errors when running
structured
.This should resolve #97
cc: @odelalleau @Mortimerp9