Skip to content

Commit

Permalink
Warning for links (#891)
Browse files Browse the repository at this point in the history
* warning:

* Update CHANGELOG.md

* Update src/hdmf/container.py

Co-authored-by: Oliver Ruebel <oruebel@users.noreply.github.com>

* Update CHANGELOG.md

---------

Co-authored-by: Oliver Ruebel <oruebel@users.noreply.github.com>
  • Loading branch information
mavaylon1 and oruebel committed Jul 3, 2023
1 parent 5269c1f commit 82baf69
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### New features and minor improvements
- Updated `ExternalResources` to have EntityKeyTable with updated tests/documentation and minor bug fix to ObjectKeyTable. @mavaylon1 [#872](https://github.com/hdmf-dev/hdmf/pull/872)
- Added warning for DynamicTableRegion links that are not added to the same parent as the original container object. @mavaylon1 [#891](https://github.com/hdmf-dev/hdmf/pull/891)

## HMDF 3.6.1 (May 18, 2023)

Expand Down
7 changes: 7 additions & 0 deletions src/hdmf/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,13 @@ def parent(self, parent_container):
if isinstance(parent_container, Container):
parent_container.__children.append(self)
parent_container.set_modified()
for child in self.children:
if type(child).__name__ == "DynamicTableRegion":
if child.table.parent is None:
msg = "The table for this DynamicTableRegion has not been added to the parent."
warn(msg)
else:
continue

def _remove_child(self, child):
"""Remove a child Container. Intended for use in subclasses that allow dynamic addition of child Containers."""
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from hdmf.common.resources import ExternalResources
from hdmf.testing import TestCase
from hdmf.utils import docval
from hdmf.common import (DynamicTable, VectorData, DynamicTableRegion)


class Subcontainer(Container):
Expand Down Expand Up @@ -136,6 +137,47 @@ def test_add_child(self):
self.assertTrue(parent_obj.modified)
self.assertIs(parent_obj.children[0], child_obj)

def test_parent_set_link_warning(self):
col1 = VectorData(
name='col1',
description='column #1',
data=[1, 2],
)
col2 = VectorData(
name='col2',
description='column #2',
data=['a', 'b'],
)

# this table will have two rows with ids 0 and 1
table = DynamicTable(
name='my table',
description='an example table',
columns=[col1, col2],
)

dtr_col = DynamicTableRegion(
name='table1_ref',
description='references rows of earlier table',
data=[0, 1, 0, 0], # refers to row indices of the 'table' variable
table=table
)

data_col = VectorData(
name='col2',
description='column #2',
data=['a', 'a', 'a', 'b'],
)

table2 = DynamicTable(
name='my_table',
description='an example table',
columns=[dtr_col, data_col],
)

with self.assertWarns(Warning):
table2.parent=ContainerWithChild()

def test_set_parent_exists(self):
"""Test that setting a parent a second time does nothing
"""
Expand Down

0 comments on commit 82baf69

Please sign in to comment.