Skip to content

HERD._get_file_from_container returns None instead of raising when no ancestor is a HERDManager #1499

Description

@rly

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

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions