Skip to content

Commit

Permalink
Allow "value" in DatasetSpec (#1143)
Browse files Browse the repository at this point in the history
  • Loading branch information
rly authored Jul 8, 2024
1 parent eb67626 commit 639d0ca
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### 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)
- Support specifying "value" key in DatasetSpec. @rly [#1143](https://github.com/hdmf-dev/hdmf/pull/1143)
- Add support for numpy 2. @rly [#1139](https://github.com/hdmf-dev/hdmf/pull/1139)

### Bug fixes
Expand Down
11 changes: 10 additions & 1 deletion src/hdmf/spec/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ def build_const_args(cls, spec_dict):
{'name': 'linkable', 'type': bool, 'doc': 'whether or not this group can be linked', 'default': True},
{'name': 'quantity', 'type': (str, int), 'doc': 'the required number of allowed instance', 'default': 1},
{'name': 'default_value', 'type': None, 'doc': 'a default value for this dataset', 'default': None},
{'name': 'value', 'type': None, 'doc': 'a fixed value for this dataset', 'default': None},
{'name': 'data_type_def', 'type': str, 'doc': 'the data type this specification represents', 'default': None},
{'name': 'data_type_inc', 'type': (str, 'DatasetSpec'),
'doc': 'the data type this specification extends', 'default': None},
Expand All @@ -662,7 +663,8 @@ class DatasetSpec(BaseStorageSpec):

@docval(*_dataset_args)
def __init__(self, **kwargs):
doc, shape, dims, dtype, default_value = popargs('doc', 'shape', 'dims', 'dtype', 'default_value', kwargs)
doc, shape, dims, dtype = popargs('doc', 'shape', 'dims', 'dtype', kwargs)
default_value, value = popargs('default_value', 'value', kwargs)
if shape is not None:
self['shape'] = shape
if dims is not None:
Expand All @@ -685,6 +687,8 @@ def __init__(self, **kwargs):
super().__init__(doc, **kwargs)
if default_value is not None:
self['default_value'] = default_value
if value is not None:
self['value'] = value
if self.name is not None:
valid_quant_vals = [1, 'zero_or_one', ZERO_OR_ONE]
if self.quantity not in valid_quant_vals:
Expand Down Expand Up @@ -762,6 +766,11 @@ def default_value(self):
'''The default value of the dataset or None if not specified'''
return self.get('default_value', None)

@property
def value(self):
'''The fixed value of the dataset or None if not specified'''
return self.get('value', None)

@classmethod
def dtype_spec_cls(cls):
''' The class to use when constructing DtypeSpec objects
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/spec_tests/test_dataset_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ def test_data_type_property_value(self):
data_type_inc=data_type_inc, data_type_def=data_type_def)
self.assertEqual(group.data_type, data_type)

def test_constructor_value(self):
spec = DatasetSpec(doc='my first dataset', dtype='int', name='dataset1', value=42)
assert spec.value == 42

def test_build_warn_extra_args(self):
spec_dict = {
'name': 'dataset1',
Expand Down

0 comments on commit 639d0ca

Please sign in to comment.