Commit e320416b authored by Amir MOHAMMADI's avatar Amir MOHAMMADI

Merge branch 'fix-delayed-attribute' into 'master'

Fix delayed attributes

See merge request !64
parents 6d198499 22487ed7
Pipeline #51126 passed with stages
in 10 minutes and 13 seconds
......@@ -119,11 +119,28 @@ class DelayedSample(_ReprMixin):
def __init__(self, load, parent=None, delayed_attributes=None, **kwargs):
self.__running_init__ = True
self._delayed_attributes = delayed_attributes
# create the delayed attributes but leave the their values as None for now.
if delayed_attributes is not None:
kwargs.update({k: None for k in delayed_attributes})
_copy_attributes(self, parent, kwargs)
# Merge parent's and param's delayed_attributes
self._delayed_attributes = getattr(parent, "_delayed_attributes", None)
if self._delayed_attributes is not None and delayed_attributes is not None:
self._delayed_attributes.update(delayed_attributes)
elif self._delayed_attributes is None:
self._delayed_attributes = delayed_attributes
# Inherit attributes from parent, without calling delayed_attributes
for key in getattr(parent, "__dict__", []):
if (
not key.startswith("_")
and key not in SAMPLE_DATA_ATTRS
and (
self._delayed_attributes is None
or key not in self._delayed_attributes
)
):
setattr(self, key, getattr(parent, key))
# Create the delayed attributes, but leave their values as None for now.
if self._delayed_attributes is not None:
kwargs.update({k: None for k in self._delayed_attributes})
# Set attribute from kwargs
_copy_attributes(self, None, kwargs)
self._load = load
del self.__running_init__
......
......@@ -108,6 +108,16 @@ def test_delayed_samples():
def load_annot():
return "annotation"
def load_annot_variant():
return "annotation_variant"
delayed_attr_read = False
def load_check():
nonlocal delayed_attr_read
delayed_attr_read = True
return "delayed_attr_data"
delayed_sample = DelayedSample(load_data, delayed_attributes=dict(annot=load_annot))
assert delayed_sample.data == 0, delayed_sample.data
assert delayed_sample.annot == "annotation", delayed_sample.annot
......@@ -120,5 +130,21 @@ def test_delayed_samples():
"annot": "annotation",
}, child_sample.__dict__
# Overwriting and adding delayed_attributes to the child
new_delayed_attr = {
"annot": load_annot_variant, # Override parent's annot
"new_annot": load_annot, # Add the new_annot attribute
"read_check": load_check,
}
child_sample = DelayedSample(
load_data, parent=delayed_sample, delayed_attributes=new_delayed_attr
)
assert child_sample.data == 0, child_sample.data
assert child_sample.annot == "annotation_variant", child_sample.annot
assert child_sample.new_annot == "annotation", child_sample.new_annot
assert not delayed_attr_read, "delayed attribute has been read early"
assert child_sample.read_check == "delayed_attr_data", child_sample.read_check
assert delayed_attr_read, "delayed attribute should have been read by now"
delayed_sample.annot = "changed"
assert delayed_sample.annot == "changed", delayed_sample.annot
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment