Repository navigation
Fix issue 324 and add tests - #325
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #325 +/- ##
===========================================
+ Coverage 83.20% 83.24% +0.04%
===========================================
Files 63 63
Lines 5322 5324 +2
===========================================
+ Hits 4428 4432 +4
+ Misses 894 892 -2
Flags with carried forward coverage won't be shown. Click here to find out more.
|
rozyczko
left a comment
There was a problem hiding this comment.
Nice. This also fixes the issue of having the caller's skip list mutated and another issue of __copy__ not keeping nested children's display names.
HOWEVER...
If any model uses EasyList as an attribute, serializing will now raise TypeError, since EasyList.to_dict has no skip.
Check this out. Sample has layers which is an EasyList.
from easyscience.base_classes import EasyList, ModelBase, NewBase
class Sample(ModelBase):
def __init__(self,
layers=None,
unique_name=None,
display_name=None):
super().__init__(unique_name=unique_name, display_name=display_name)
self.layers = layers
base = NewBase(unique_name='base')
layer=EasyList([base])
sample = Sample(layers=layer, unique_name='sample')
print(sample.to_dict())in develop, this suffers from mangled unique_name serialization (unique_name base -> EasyList_0):
{'@module': '__main__', '@class': 'Sample', '@version': None, 'layers': {'@module': 'easyscience.base_classes.easy_list', '@class': 'EasyList', '@version': '2.5.1+dev4', 'unique_name': 'EasyList_0'}, 'unique_name': 'sample'}
in your branch, the operation fails
TypeError: EasyList.to_dict() got an unexpected keyword argument 'skip'
my 292-remove-objbase branch gives the correct string, with both unique_names correct:
{'@module': '__main__', '@class': 'Sample', '@version': None, 'layers': {'@module': 'easyscience.base_classes.easy_list', '@class': 'EasyList', '@version': '2.5.1+dev4', 'data': [{'@module': 'easyscience.base_classes.new_base', '@class': 'NewBase', '@version': '2.5.1+dev4', 'unique_name': 'base'}]}, 'unique_name': 'sample'}
I think the fix in your branch would be extremely simple:
just add skip to to_dict in EasyList.
- def to_dict(self) -> dict:
+ def to_dict(self, skip) -> dict:
"""
Convert the EasyList to a dictionary for serialization.
Returns
-------
dict
Dictionary representation of the EasyList.
"""
- dict_repr = super().to_dict()
+ dict_repr = super().to_dict(skip=skip)
if self._protected_types != [NewBase]:
dict_repr['protected_types'] = [
{'@module': cls_.__module__, '@class': cls_.__name__}
for cls_ in self._protected_types
] # noqa: E501
- dict_repr['data'] = [item.to_dict() for item in self._data]
+ dict_repr['data'] = [item.to_dict(skip=skip) for item in self._data]
return dict_reprAdding a test for EasyList as an attribute would also be helpful here.
| if self._default_unique_name or 'unique_name' in skip: | ||
| temp_dict.pop('unique_name', None) | ||
| if self._display_name is None or 'display_name' in skip: | ||
| temp_dict.pop('display_name', None) |
There was a problem hiding this comment.
serializer already acts on skip, so these checks are redundant, I think
|
The issue seems to have been fixed with the current commit 🔥 |
rozyczko
left a comment
There was a problem hiding this comment.
It looks good now.
Consider removing extra lines as per my comment and adding a unit test for EasyList as an attribute
Fixes the unique_name and display_name serialization issue with nested
NewBaseclasses