From f95b1efa0e890d35dd69d74d496ec6f2fa1e716d Mon Sep 17 00:00:00 2001 From: Matthew Avaylon Date: Sat, 9 Dec 2023 08:42:20 -0800 Subject: [PATCH] Update get_key to support multiple keys (#999) * update get_key * Update CHANGELOG.md * Update test_resources.py * doc string * doc --------- Co-authored-by: Ryan Ly --- CHANGELOG.md | 1 + src/hdmf/common/resources.py | 5 +++-- tests/unit/common/test_resources.py | 10 ++++++---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 278d64838..1fc3a2995 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Minor Improvements - Updated `__gather_columns` to ignore the order of bases when generating columns from the super class. @mavaylon1 [#991](https://github.com/hdmf-dev/hdmf/pull/991) +- Update `get_key` to return all the keys if there are multiple within a `HERD` instance. @mavaylon1 [#999](https://github.com/hdmf-dev/hdmf/pull/999) - Improve HTML rendering of tables. @bendichter [#998](https://github.com/hdmf-dev/hdmf/pull/998) - Improved issue and PR templates. @rly [#1004](https://github.com/hdmf-dev/hdmf/pull/1004) diff --git a/src/hdmf/common/resources.py b/src/hdmf/common/resources.py index faead635f..8054a758f 100644 --- a/src/hdmf/common/resources.py +++ b/src/hdmf/common/resources.py @@ -484,6 +484,8 @@ def get_key(self, **kwargs): If container, relative_path, and field are provided, the Key that corresponds to the given name of the key for the given container, relative_path, and field is returned. + + If there are multiple matches, a list of all matching keys will be returned. """ key_name, container, relative_path, field = popargs('key_name', 'container', 'relative_path', 'field', kwargs) key_idx_matches = self.keys.which(key=key_name) @@ -510,8 +512,7 @@ def get_key(self, **kwargs): # the key has never been used before raise ValueError("key '%s' does not exist" % key_name) elif len(key_idx_matches) > 1: - msg = "There are more than one key with that name. Please search with additional information." - raise ValueError(msg) + return [self.keys.row[x] for x in key_idx_matches] else: return self.keys.row[key_idx_matches[0]] diff --git a/tests/unit/common/test_resources.py b/tests/unit/common/test_resources.py index 796f75db4..3040d78b6 100644 --- a/tests/unit/common/test_resources.py +++ b/tests/unit/common/test_resources.py @@ -1133,7 +1133,7 @@ class TestHERDGetKey(TestCase): def setUp(self): self.er = HERD() - def test_get_key_error_more_info(self): + def test_get_key_multiple(self): self.er.add_ref(file=HERDManagerContainer(name='file'), container=Container(name='Container'), key='key1', @@ -1145,9 +1145,11 @@ def test_get_key_error_more_info(self): entity_id="id12", entity_uri='url21') - msg = "There are more than one key with that name. Please search with additional information." - with self.assertRaisesWith(ValueError, msg): - _ = self.er.get_key(key_name='key1') + keys = self.er.get_key(key_name='key1') + self.assertIsInstance(keys[0], Key) + self.assertIsInstance(keys[1], Key) + self.assertEqual(keys[0].idx, 0) + self.assertEqual(keys[1].idx, 1) def test_get_key(self): self.er.add_ref(file=HERDManagerContainer(name='file'),