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

Fix DtypeError message for reference validation #1199

Merged
merged 4 commits into from
Oct 31, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
### Enhancements
- Added support for expandable datasets of references for untyped and compound data types. @stephprince [#1188](https://github.com/hdmf-dev/hdmf/pull/1188)

### Bug fixes
- Fixed inaccurate error message when validating reference data types. @stephprince [#1199](https://github.com/hdmf-dev/hdmf/pull/1199)

## HDMF 3.14.5 (October 6, 2024)

### Enhancements
Expand Down
6 changes: 5 additions & 1 deletion src/hdmf/validate/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,11 @@ def validate(self, **kwargs):
try:
dtype, string_format = get_type(data, builder.dtype)
if not check_type(self.spec.dtype, dtype, string_format):
ret.append(DtypeError(self.get_spec_loc(self.spec), self.spec.dtype, dtype,
if isinstance(self.spec.dtype, RefSpec):
expected = f'{self.spec.dtype.reftype} reference'
else:
expected = self.spec.dtype
ret.append(DtypeError(self.get_spec_loc(self.spec), expected, dtype,
location=self.get_builder_loc(builder)))
except EmptyArrayError:
# do not validate dtype of empty array. HDMF does not yet set dtype when writing a list/tuple
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/validator_tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,38 @@ def test_scalar_compound_dtype(self):
results = self.vmap.validate(bar_builder)
self.assertEqual(len(results), 0)

class TestReferenceValidation(ValidatorTestBase):
def getSpecs(self):
qux_spec = DatasetSpec(
doc='a simple scalar dataset',
data_type_def='Qux',
dtype='int',
shape=None
)
bar_spec = GroupSpec('A test group specification with a reference dataset',
data_type_def='Bar',
datasets=[DatasetSpec('an example dataset',
dtype=RefSpec('Qux', reftype='object'),
name='data',
shape=(None, ))],
attributes=[AttributeSpec('attr1',
'an example attribute',
dtype=RefSpec('Qux', reftype='object'),
shape=(None, ))])
return (qux_spec, bar_spec)

def test_invalid_reference(self):
"""Test that validator does not allow another data type where a reference is specified."""
value = np.array([1.0, 2.0, 3.0])
bar_builder = GroupBuilder('my_bar',
attributes={'data_type': 'Bar', 'attr1': value},
datasets=[DatasetBuilder('data', value)])
results = self.vmap.validate(bar_builder)
result_strings = set([str(s) for s in results])
expected_errors = {"Bar/attr1 (my_bar.attr1): incorrect type - expected 'object reference', got 'float64'",
"Bar/data (my_bar/data): incorrect type - expected 'object reference', got 'float64'"}
self.assertEqual(result_strings, expected_errors)

class Test1DArrayValidation(TestCase):

def set_up_spec(self, dtype):
Expand Down