Skip to content

Commit

Permalink
Improve "already exists" error message (#1165)
Browse files Browse the repository at this point in the history
* Improve "already exists" error message

* Fix test, improve msg

* Update changelog

---------

Co-authored-by: Matthew Avaylon <[email protected]>
  • Loading branch information
rly and mavaylon1 authored Aug 19, 2024
1 parent 875712b commit 2f339d1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 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 @@

### Enhancements
- Added support to append to a dataset of references for HDMF-Zarr. @mavaylon1 [#1157](https://github.com/hdmf-dev/hdmf/pull/1157)
- Improved "already exists" error message when adding a container to a `MultiContainerInterface`. @rly [#1165](https://github.com/hdmf-dev/hdmf/pull/1165)

## HDMF 3.14.3 (July 29, 2024)

Expand Down
4 changes: 3 additions & 1 deletion src/hdmf/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,9 @@ def _func(self, **kwargs):
# still need to mark self as modified
self.set_modified()
if tmp.name in d:
msg = "'%s' already exists in %s '%s'" % (tmp.name, cls.__name__, self.name)
msg = (f"Cannot add {tmp.__class__} '{tmp.name}' at 0x{id(tmp)} to dict attribute '{attr_name}' in "
f"{cls} '{self.name}'. {d[tmp.name].__class__} '{tmp.name}' at 0x{id(d[tmp.name])} "
f"already exists in '{attr_name}' and has the same name.")
raise ValueError(msg)
d[tmp.name] = tmp
return container
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/test_multicontainerinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ def test_add_single_dup(self):
"""Test that adding a container to the attribute dict correctly adds the container."""
obj1 = Container('obj1')
foo = Foo(obj1)
msg = "'obj1' already exists in Foo 'Foo'"
msg = (f"Cannot add <class 'hdmf.container.Container'> 'obj1' at 0x{id(obj1)} to dict attribute "
"'containers' in <class 'tests.unit.test_multicontainerinterface.Foo'> 'Foo'. "
f"<class 'hdmf.container.Container'> 'obj1' at 0x{id(obj1)} already exists in 'containers' "
"and has the same name.")
with self.assertRaisesWith(ValueError, msg):
foo.add_container(obj1)

Expand Down

0 comments on commit 2f339d1

Please sign in to comment.