From 74e01ca6b166e32b62f3af828e6c75e08e02243e Mon Sep 17 00:00:00 2001 From: Steph Prince <40640337+stephprince@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:07:54 -0700 Subject: [PATCH 1/4] update dtypeerror inputs for dset of refs --- src/hdmf/validate/validator.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/hdmf/validate/validator.py b/src/hdmf/validate/validator.py index 6ce211f96..daa5adac4 100644 --- a/src/hdmf/validate/validator.py +++ b/src/hdmf/validate/validator.py @@ -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 From 8ef146a8792a4458ed7733c4b6eb45b920e82113 Mon Sep 17 00:00:00 2001 From: Steph Prince <40640337+stephprince@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:08:17 -0700 Subject: [PATCH 2/4] add validation tests for references --- tests/unit/validator_tests/test_validate.py | 33 +++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/unit/validator_tests/test_validate.py b/tests/unit/validator_tests/test_validate.py index dd79cfce5..c8f26d3a4 100644 --- a/tests/unit/validator_tests/test_validate.py +++ b/tests/unit/validator_tests/test_validate.py @@ -524,6 +524,39 @@ 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): + # setup spec + """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): From ba6b0afd36df37c93f76ba9c538ff91f1cfa9cbb Mon Sep 17 00:00:00 2001 From: Steph Prince <40640337+stephprince@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:13:48 -0700 Subject: [PATCH 3/4] remove old comment --- tests/unit/validator_tests/test_validate.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/validator_tests/test_validate.py b/tests/unit/validator_tests/test_validate.py index c8f26d3a4..64667b3e0 100644 --- a/tests/unit/validator_tests/test_validate.py +++ b/tests/unit/validator_tests/test_validate.py @@ -545,7 +545,6 @@ def getSpecs(self): return (qux_spec, bar_spec) def test_invalid_reference(self): - # setup spec """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', From 2253f0e886354426bdba49f824900e989176358f Mon Sep 17 00:00:00 2001 From: Steph Prince <40640337+stephprince@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:17:41 -0700 Subject: [PATCH 4/4] update CHANGELOG --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 245902d5b..c1c490089 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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