-
Notifications
You must be signed in to change notification settings - Fork 122
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 support for pathlib.Path to OmegaConf.load() and OmegaConf.save() #158
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
import io | ||
import os | ||
import pathlib | ||
import tempfile | ||
from typing import Any, Dict | ||
|
||
|
@@ -37,6 +38,20 @@ def save_load_from_filename(conf: Container, resolve: bool, expected: Any) -> No | |
os.unlink(fp.name) | ||
|
||
|
||
def save_load_from_pathlib_path(conf: Container, resolve: bool, expected: Any) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of duplicating the other function please parametrize it with pytest.mark.parametrize() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @omry. I was able to implement the other suggestions, and while I would like to keep things DRY, I'm not quite sure how to parametrise using the original save_load_from_filename. Could you please point me in the right direction? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made an implementation. Hopefully this is reasonably elegant :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's good, and trickier than usual because you can't construct the object outside due to the temp file. |
||
if expected is None: | ||
expected = conf | ||
# note that delete=False here is a work around windows incompetence. | ||
try: | ||
with tempfile.NamedTemporaryFile(delete=False) as fp: | ||
filepath = pathlib.Path(fp.name) | ||
OmegaConf.save(conf, filepath, resolve=resolve) | ||
c2 = OmegaConf.load(filepath) | ||
assert c2 == expected | ||
finally: | ||
os.unlink(fp.name) | ||
|
||
|
||
def test_load_from_invalid() -> None: | ||
with pytest.raises(TypeError): | ||
OmegaConf.load(3.1415) # type: ignore | ||
|
@@ -64,6 +79,12 @@ def test_save_load__from_filename( | |
cfg = OmegaConf.create(input_) | ||
save_load_from_filename(cfg, resolve, expected) | ||
|
||
def test_save_load__from_pathlib_path( | ||
self, input_: Dict[str, Any], resolve: bool, expected: Any | ||
) -> None: | ||
cfg = OmegaConf.create(input_) | ||
save_load_from_pathlib_path(cfg, resolve, expected) | ||
|
||
|
||
def test_save_illegal_type() -> None: | ||
with pytest.raises(TypeError): | ||
|
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.
Please update the typing information on save and load to reflect this change.
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.
Done