`MSection.m_copy` instantiates empty sub-sections in the deep copy

When a deep copy of a section is made, all the sub-sections, including the ones not instantiated in the original section, are instantiated in the copy and set to None.

When making a deep copy of a section containing instantiated sub-sections with the value None, m_copy runs into attribute error, as the method expects sub-section to be an MSection, not None.

To recreate the issue:

from nomad.datamodel.metainfo.basesections import PureSubstanceComponent

sec = PureSubstanceComponent()
print(sec)             # output: PureSubstanceComponent()

sec_copy1 = sec.m_copy(deep=True)
print(sec_copy1)       # output: PureSubstanceComponent(pure_substance)

sec_copy2 = sec_copy1.m_copy(deep=True)         # gives error

Possible fix:

  • Check if a sub-section is instantiated in the original, and only add it to the copy if True.
  • Check if a sub-section is not None before adding it to the copy.
Assignee Loading
Time tracking Loading