Skip to content

Fix issue 324 and add tests - #325

Merged
damskii9992 merged 3 commits into
developfrom
issue324_nest_NewBases
Oct 9, 2026
Merged

damskii9992 merged 3 commits into
developfrom
issue324_nest_NewBases

Conversation

@damskii9992

Copy link
Copy Markdown
Contributor

Fixes the unique_name and display_name serialization issue with nested NewBase classes

@damskii9992 damskii9992 added [scope] bug Bug report or fix (major.minor.PATCH) [priority] highest Urgent. Needs attention ASAP [area] serialization Anything related to serialization labels Oct 8, 2026
@damskii9992
damskii9992 requested a review from rozyczko October 8, 2026 11:02
@damskii9992 damskii9992 linked an issue Oct 8, 2026 that may be closed by this pull request
@codecov

codecov Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.24%. Comparing base (3025900) to head (cbe1e3f).

Additional details and impacted files

Impacted file tree graph

@@             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     
Flag Coverage Δ
unittests 83.24% <100.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/easyscience/base_classes/easy_list.py 96.91% <100.00%> (+0.64%) ⬆️
src/easyscience/base_classes/new_base.py 100.00% <100.00%> (ø)
src/easyscience/io/serializer_base.py 90.72% <100.00%> (+0.51%) ⬆️

@damskii9992 damskii9992 mentioned this pull request Oct 8, 2026

@rozyczko rozyczko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_repr

Adding a test for EasyList as an attribute would also be helpful here.

Comment on lines +145 to +148
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serializer already acts on skip, so these checks are redundant, I think

@rozyczko

rozyczko commented Oct 8, 2026

Copy link
Copy Markdown
Member

The issue seems to have been fixed with the current commit 🔥

@rozyczko rozyczko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good now.

Consider removing extra lines as per my comment and adding a unit test for EasyList as an attribute

@damskii9992
damskii9992 merged commit 511a8e4 into develop Oct 9, 2026
22 of 24 checks passed
@damskii9992
damskii9992 deleted the issue324_nest_NewBases branch October 9, 2026 09:49
markbujehein pushed a commit that referenced this pull request Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[area] serialization Anything related to serialization [priority] highest Urgent. Needs attention ASAP [scope] bug Bug report or fix (major.minor.PATCH)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unique_name wrongly going into to_dict dicts when default.

2 participants