Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve "already exists" error message #1165

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading