diff --git a/serializable/helpers.py b/serializable/helpers.py index 289185c..2d0939a 100644 --- a/serializable/helpers.py +++ b/serializable/helpers.py @@ -228,8 +228,8 @@ def list_to_serializable_repr(x): def to_dict(obj): """ - If value wasn't isn't a primitive scalar or collection then it needs to - either implement to_dict (instances of Serializable) or has member + If value isn't a primitive scalar or collection then it needs to + either implement to_dict (instances of Serializable) or have member data matching each required arg of __init__. """ if isinstance(obj, dict): diff --git a/serializable/serializable.py b/serializable/serializable.py index e961e21..385d856 100644 --- a/serializable/serializable.py +++ b/serializable/serializable.py @@ -24,12 +24,10 @@ class Serializable: """ - Base class for all PyEnsembl objects which provides default + Base class for user-defined objects which provides default methods such as to_json, from_json, __reduce__, and from_dict - Relies on the following condition: - (1) a user-defined to_dict method - (2) the keys of to_dict() must match the arguments to __init__ + Relies on the keys of to_dict() matching the arguments to __init__. """ def __str__(self): @@ -78,7 +76,7 @@ def _update_kwargs(cls, kwargs): Rename any old keyword arguments to preserve backwards compatibility """ # check every class in the inheritance chain for its own - # definition of _KEYWORD_ALIASES + # definition of _SERIALIZABLE_KEYWORD_ALIASES for klass in cls.mro(): keyword_rename_dict = getattr(klass, "_SERIALIZABLE_KEYWORD_ALIASES", {}) for old_name, new_name in keyword_rename_dict.items(): @@ -99,7 +97,7 @@ def from_dict(cls, state_dict): def to_json(self): """ - Returns a string containing a JSON representation of this Genome. + Returns a string containing a JSON representation of this object. """ return to_json(self) @@ -112,8 +110,7 @@ def from_json(cls, json_string): def write_json_file(self, path): """ - Serialize this VariantCollection to a JSON representation and write it - out to a text file. + Serialize this object to JSON and write it to the text file at `path`. """ with open(path, "w") as f: f.write(self.to_json()) @@ -121,7 +118,7 @@ def write_json_file(self, path): @classmethod def read_json_file(cls, path): """ - Construct a VariantCollection from a JSON file. + Construct an instance of this class from the JSON file at `path`. """ with open(path) as f: json_string = f.read() diff --git a/serializable/version.py b/serializable/version.py index 4b8e122..e52182c 100644 --- a/serializable/version.py +++ b/serializable/version.py @@ -1,4 +1,4 @@ -__version__ = "1.1.1" +__version__ = "1.1.2" def print_version(): diff --git a/tests/test_object.py b/tests/test_object.py index 3a73b0a..93da2da 100644 --- a/tests/test_object.py +++ b/tests/test_object.py @@ -44,3 +44,9 @@ def test_object_to_json(): def test_object_pickle(): eq_(instance, pickle.loads(pickle.dumps(instance))) + + +def test_object_json_file(tmp_path): + path = tmp_path / "instance.json" + instance.write_json_file(path) + eq_(instance, A.read_json_file(path))