Skip to content

Commit 639d0ca

Browse files
authored
Allow "value" in DatasetSpec (#1143)
1 parent eb67626 commit 639d0ca

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Enhancements
66
- Warn when unexpected keys are present in specs. @rly [#1134](https://github.com/hdmf-dev/hdmf/pull/1134)
77
- Support appending to zarr arrays. @mavaylon1 [#1136](https://github.com/hdmf-dev/hdmf/pull/1136)
8+
- Support specifying "value" key in DatasetSpec. @rly [#1143](https://github.com/hdmf-dev/hdmf/pull/1143)
89
- Add support for numpy 2. @rly [#1139](https://github.com/hdmf-dev/hdmf/pull/1139)
910

1011
### Bug fixes

src/hdmf/spec/spec.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ def build_const_args(cls, spec_dict):
648648
{'name': 'linkable', 'type': bool, 'doc': 'whether or not this group can be linked', 'default': True},
649649
{'name': 'quantity', 'type': (str, int), 'doc': 'the required number of allowed instance', 'default': 1},
650650
{'name': 'default_value', 'type': None, 'doc': 'a default value for this dataset', 'default': None},
651+
{'name': 'value', 'type': None, 'doc': 'a fixed value for this dataset', 'default': None},
651652
{'name': 'data_type_def', 'type': str, 'doc': 'the data type this specification represents', 'default': None},
652653
{'name': 'data_type_inc', 'type': (str, 'DatasetSpec'),
653654
'doc': 'the data type this specification extends', 'default': None},
@@ -662,7 +663,8 @@ class DatasetSpec(BaseStorageSpec):
662663

663664
@docval(*_dataset_args)
664665
def __init__(self, **kwargs):
665-
doc, shape, dims, dtype, default_value = popargs('doc', 'shape', 'dims', 'dtype', 'default_value', kwargs)
666+
doc, shape, dims, dtype = popargs('doc', 'shape', 'dims', 'dtype', kwargs)
667+
default_value, value = popargs('default_value', 'value', kwargs)
666668
if shape is not None:
667669
self['shape'] = shape
668670
if dims is not None:
@@ -685,6 +687,8 @@ def __init__(self, **kwargs):
685687
super().__init__(doc, **kwargs)
686688
if default_value is not None:
687689
self['default_value'] = default_value
690+
if value is not None:
691+
self['value'] = value
688692
if self.name is not None:
689693
valid_quant_vals = [1, 'zero_or_one', ZERO_OR_ONE]
690694
if self.quantity not in valid_quant_vals:
@@ -762,6 +766,11 @@ def default_value(self):
762766
'''The default value of the dataset or None if not specified'''
763767
return self.get('default_value', None)
764768

769+
@property
770+
def value(self):
771+
'''The fixed value of the dataset or None if not specified'''
772+
return self.get('value', None)
773+
765774
@classmethod
766775
def dtype_spec_cls(cls):
767776
''' The class to use when constructing DtypeSpec objects

tests/unit/spec_tests/test_dataset_spec.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ def test_data_type_property_value(self):
246246
data_type_inc=data_type_inc, data_type_def=data_type_def)
247247
self.assertEqual(group.data_type, data_type)
248248

249+
def test_constructor_value(self):
250+
spec = DatasetSpec(doc='my first dataset', dtype='int', name='dataset1', value=42)
251+
assert spec.value == 42
252+
249253
def test_build_warn_extra_args(self):
250254
spec_dict = {
251255
'name': 'dataset1',

0 commit comments

Comments
 (0)