What happened?
HERD._get_file_from_container (src/hdmf/common/resources.py:401-421) only raises ValueError("Could not find file. Add container to the file.") when the container has no parent. If the container has a parent chain but none of the ancestors is a HERDManager, the while loop exhausts and the method returns None implicitly.
Callers (e.g. add_ref, get_object_entities, add_ref_termset, get_key) then do file.object_id on None, producing a confusing AttributeError: 'NoneType' object has no attribute 'object_id' instead of the intended clear error.
def _get_file_from_container(self, **kwargs):
container = kwargs['container']
if isinstance(container, HERDManager):
return container
else:
parent = container.parent
if parent is not None:
while parent is not None:
if isinstance(parent, HERDManager):
return parent
else:
parent = parent.parent
# falls through here -> returns None implicitly <-- BUG
else:
msg = 'Could not find file. Add container to the file.'
raise ValueError(msg)
Steps to reproduce
from hdmf.common.resources import HERD
from hdmf import Container
parent = Container(name='parent')
child = Container(name='child')
child.parent = parent # parent chain exists, but no HERDManager anywhere
er = HERD()
print(repr(er._get_file_from_container(container=child))) # None, expected ValueError
Expected
The ValueError should be raised whenever no HERDManager ancestor is found, regardless of whether the container has parents. The raise should be moved after the loop so both the "no parent" and "parents but no HERDManager" cases are covered.
Code of Conduct
What happened?
HERD._get_file_from_container(src/hdmf/common/resources.py:401-421) only raisesValueError("Could not find file. Add container to the file.")when the container has no parent. If the container has a parent chain but none of the ancestors is aHERDManager, thewhileloop exhausts and the method returnsNoneimplicitly.Callers (e.g.
add_ref,get_object_entities,add_ref_termset,get_key) then dofile.object_idonNone, producing a confusingAttributeError: 'NoneType' object has no attribute 'object_id'instead of the intended clear error.Steps to reproduce
Expected
The
ValueErrorshould be raised whenever noHERDManagerancestor is found, regardless of whether the container has parents. Theraiseshould be moved after the loop so both the "no parent" and "parents but no HERDManager" cases are covered.Code of Conduct