Skip to content

Commit

Permalink
Serializable instance attributes (#520)
Browse files Browse the repository at this point in the history
* make sure all init arguments are saved as instance attributes, as required when creating references during model saving

* fix unit tests
  • Loading branch information
msperber committed Aug 18, 2018
1 parent 662cffe commit 65e3f6a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 0 additions & 3 deletions test/test_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ def test_shallow(self):
initalized = persistence.initialize_if_needed(preloaded)
persistence.save_to_file(self.model_file, initalized)

@unittest.expectedFailure # TODO: need to fix case of serializable not storing its arguments as object members
def test_mid(self):
test_obj = yaml.load("""
a: !DummyArgClass
Expand All @@ -304,7 +303,6 @@ def test_mid(self):
initalized = persistence.initialize_if_needed(preloaded)
persistence.save_to_file(self.model_file, initalized)

@unittest.expectedFailure # TODO: need to fix case of serializable not storing its arguments as object members
def test_deep(self):
test_obj = yaml.load("""
a: !DummyArgClass
Expand All @@ -321,7 +319,6 @@ def test_deep(self):
initalized = persistence.initialize_if_needed(preloaded)
persistence.save_to_file(self.model_file, initalized)

@unittest.expectedFailure # TODO: need to fix case of serializable not storing its arguments as object members
def test_double_ref(self):
test_obj = yaml.load("""
a: !DummyArgClass
Expand Down
5 changes: 5 additions & 0 deletions xnmt/persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ def wrapper(obj, *args, **kwargs):
if "yaml_path" in serialize_params: del serialize_params["yaml_path"]
obj.serialize_params = serialize_params
obj.init_completed = True
# TODO: the below is needed for proper reference creation when saving the model, but should be replaced with
# something safer
for key, arg in serialize_params.items():
if not hasattr(obj, key):
setattr(obj, key, arg)

wrapper.uses_serializable_init = True
return wrapper
Expand Down

0 comments on commit 65e3f6a

Please sign in to comment.