Improve documentation for MultiContainerInterface
Description
When a user creates a custom API class that extends MultiContainerInterface but they define their own constructor, then when a file is written with the extension and loaded back, the children Container objects of the custom MultiContainerInterface are not set as children.
This is because the constructor for MultiContainerInterface with docval arguments is not auto-generated when the API class already has its own custom constructor:
https://github.com/NeurodataWithoutBorders/pynwb/blob/dev/src/pynwb/core.py#L592-L595
We should either:
- improve the documentation of
MultiContainerInterfaceor extensions to specify the caveat above - allow "smart" auto-generation of the constructor of a
MultiContainerInterfaceto handle both custom fields and children fields.
I prefer option 1 since option 2 could have some hard-to-catch edge cases and involves too much "magic"
Steps to Reproduce
@register_class('MyType', 'my-ext')
class MyType(MultiContainerInterface):
@docval({'name': 'name', 'type': str, 'doc': 'name of the type'},
{'name': 'id', 'type': 'int', 'doc': 'an id for this type'})
def __init__(self, **kwargs):
super().__init__(kwargs['name'])
self.id = kwargs['id']
__nwbfields__ = ('id', )
__clsconf__ = [
{
'attr': 'my_children',
'type': MyChild,
'add': 'add_mychild',
'get': 'get_mychild'
}
]
Workaround: Explicitly add the kwarg my_children to the constructor and set the object's field to the kwarg in the constructor
@register_class('MyType', 'my-ext')
class MyType(MultiContainerInterface):
@docval({'name': 'name', 'type': str, 'doc': 'name of the type'},
{'name': 'id', 'type': 'int', 'doc': 'an id for this type'},
{'name': 'my_children', 'type': (list, tuple), 'doc': 'my children for this type', 'default': list()})
def __init__(self, **kwargs):
super().__init__(kwargs['name'])
self.id = kwargs['id']
self.my_children = kwargs['my_children']
__nwbfields__ = ('id', )
__clsconf__ = [
{
'attr': 'my_children',
'type': MyChild,
'add': 'add_mychild',
'get': 'get_mychild'
}
]