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

Attempt to fix loading of simple OmegaConf pickled configs stored in 2.0 #718

Merged
merged 3 commits into from
May 19, 2021
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
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
include LICENSE
include requirements/base.txt
recursive-include tests *.py
recursive-include tests *.py *.pickle
12 changes: 12 additions & 0 deletions omegaconf/basecontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,19 @@ def __setstate__(self, d: Dict[str, Any]) -> None:

if isinstance(self, DictConfig):
key_type = d["_metadata"].key_type

# backward compatibility to load OmegaConf 2.0 configs
if key_type is None:
key_type = Any
d["_metadata"].key_type = key_type

element_type = d["_metadata"].element_type

# backward compatibility to load OmegaConf 2.0 configs
if element_type is None:
element_type = Any
d["_metadata"].element_type = element_type

ref_type = d["_metadata"].ref_type
if is_container_annotation(ref_type):
if is_generic_dict(ref_type):
Expand Down
Binary file added tests/data/2.0.6.pickle
Binary file not shown.
Binary file added tests/data/2.1.0.rc1.pickle
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/data/load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pickle
import sys

from omegaconf import OmegaConf

with open(f"{sys.argv[1]}.pickle", mode="rb") as fp:
cfg = pickle.load(fp)
assert cfg == OmegaConf.create({"a": [{"b": 10}]})
8 changes: 8 additions & 0 deletions tests/data/save.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pickle

from omegaconf import OmegaConf, __version__

cfg = OmegaConf.create({"a": [{"b": 10}]})

with open(f"{__version__}.pickle", mode="wb") as fp:
pickle.dump(cfg, fp)
16 changes: 15 additions & 1 deletion tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_load_from_invalid() -> None:
({"foo": 10, "bar": "${foo}"}, False, None, str),
({"foo": 10, "bar": "${foo}"}, False, None, pathlib.Path),
({"foo": 10, "bar": "${foo}"}, False, {"foo": 10, "bar": 10}, str),
([u"שלום"], False, None, str),
(["שלום"], False, None, str),
],
)
class TestSaveLoad:
Expand Down Expand Up @@ -257,3 +257,17 @@ def test_pickle_flags_consistency() -> None:
cfg2._set_flag("test", None)
assert cfg2._get_flag("test") is None
assert cfg2._get_node("a")._get_flag("test") is None


@mark.parametrize(
"version",
[
"2.0.6",
"2.1.0.rc1",
],
)
def test_pickle_backward_compatibility(version: str) -> None:
path = Path(__file__).parent / "data" / f"{version}.pickle"
with open(path, mode="rb") as fp:
cfg = pickle.load(fp)
assert cfg == OmegaConf.create({"a": [{"b": 10}]})