Skip to content

Commit

Permalink
Warn when unexpected keys in specs (#1134)
Browse files Browse the repository at this point in the history
* Warn when unexpected keys in specs

* Update changelog
  • Loading branch information
rly authored Jul 1, 2024
1 parent 539ecf4 commit 5e13d64
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

## HDMF 3.14.2 (Upcoming)

### Bug fixes
- Fix iterator increment causing an extra +1 added after the end of completion. @CodyCBakerPhD [#1128](https://github.com/hdmf-dev/hdmf/pull/1128)

### Enhancements
- Warn when unexpected keys are present in specs. @rly [#1134](https://github.com/hdmf-dev/hdmf/pull/1134)
- Support appending to zarr arrays. @mavaylon1 [#1136](https://github.com/hdmf-dev/hdmf/pull/1136)

### Bug fixes
- Fix iterator increment causing an extra +1 added after the end of completion. @CodyCBakerPhD [#1128](https://github.com/hdmf-dev/hdmf/pull/1128)

## HDMF 3.14.1 (June 6, 2024)

### Bug fixes
Expand Down
4 changes: 4 additions & 0 deletions src/hdmf/spec/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@ def build_spec(cls, spec_dict):
vargs = cls.build_const_args(spec_dict)
kwargs = dict()
# iterate through the Spec docval and construct kwargs based on matching values in spec_dict
unused_vargs = list(vargs)
for x in get_docval(cls.__init__):
if x['name'] in vargs:
kwargs[x['name']] = vargs.get(x['name'])
unused_vargs.remove(x['name'])
if unused_vargs:
warn(f'Unexpected keys {unused_vargs} in spec {spec_dict}')
return cls(**kwargs)


Expand Down
12 changes: 12 additions & 0 deletions tests/unit/spec_tests/test_attribute_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,15 @@ def test_build_spec_no_doc(self):
msg = "AttributeSpec.__init__: missing argument 'doc'"
with self.assertRaisesWith(TypeError, msg):
AttributeSpec.build_spec(spec_dict)

def test_build_warn_extra_args(self):
spec_dict = {
'name': 'attribute1',
'doc': 'test attribute',
'dtype': 'int',
'quantity': '?',
}
msg = ("Unexpected keys ['quantity'] in spec {'name': 'attribute1', 'doc': 'test attribute', "
"'dtype': 'int', 'quantity': '?'}")
with self.assertWarnsWith(UserWarning, msg):
AttributeSpec.build_spec(spec_dict)
12 changes: 12 additions & 0 deletions tests/unit/spec_tests/test_dataset_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,15 @@ def test_data_type_property_value(self):
group = GroupSpec('A group', name='group',
data_type_inc=data_type_inc, data_type_def=data_type_def)
self.assertEqual(group.data_type, data_type)

def test_build_warn_extra_args(self):
spec_dict = {
'name': 'dataset1',
'doc': 'test dataset',
'dtype': 'int',
'required': True,
}
msg = ("Unexpected keys ['required'] in spec {'name': 'dataset1', 'doc': 'test dataset', "
"'dtype': 'int', 'required': True}")
with self.assertWarnsWith(UserWarning, msg):
DatasetSpec.build_spec(spec_dict)
10 changes: 10 additions & 0 deletions tests/unit/spec_tests/test_group_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,16 @@ def test_get_namespace_spec(self):
expected = AttributeSpec('namespace', 'the namespace for the data type of this object', 'text', required=False)
self.assertDictEqual(GroupSpec.get_namespace_spec(), expected)

def test_build_warn_extra_args(self):
spec_dict = {
'name': 'group1',
'doc': 'test group',
'required': True,
}
msg = "Unexpected keys ['required'] in spec {'name': 'group1', 'doc': 'test group', 'required': True}"
with self.assertWarnsWith(UserWarning, msg):
GroupSpec.build_spec(spec_dict)


class TestNotAllowedConfig(TestCase):

Expand Down
12 changes: 12 additions & 0 deletions tests/unit/spec_tests/test_link_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,15 @@ def test_required_is_many(self):
)
self.assertEqual(spec.required, req)
self.assertEqual(spec.is_many(), many)

def test_build_warn_extra_args(self):
spec_dict = {
'name': 'link1',
'doc': 'test link',
'target_type': 'TestType',
'required': True,
}
msg = ("Unexpected keys ['required'] in spec {'name': 'link1', 'doc': 'test link', "
"'target_type': 'TestType', 'required': True}")
with self.assertWarnsWith(UserWarning, msg):
LinkSpec.build_spec(spec_dict)

0 comments on commit 5e13d64

Please sign in to comment.