From 6e5534cee02c421f7c992471785f1aec5be8f419 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 31 Oct 2024 10:15:54 -0700 Subject: [PATCH 1/2] [pre-commit.ci] pre-commit autoupdate (#1197) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c84bfaffc..4122f041f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,7 @@ # NOTE: run `pre-commit autoupdate` to update hooks to latest version repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.6.0 + rev: v5.0.0 hooks: - id: check-yaml - id: end-of-file-fixer @@ -18,7 +18,7 @@ repos: # hooks: # - id: black - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.6.8 + rev: v0.7.1 hooks: - id: ruff # - repo: https://github.com/econchick/interrogate From 06a62b9b3dbdae30391b54a8d51d7d87e550ca02 Mon Sep 17 00:00:00 2001 From: Steph Prince <40640337+stephprince@users.noreply.github.com> Date: Thu, 31 Oct 2024 13:24:28 -0700 Subject: [PATCH 2/2] Fix DtypeError message for reference validation (#1199) * update dtypeerror inputs for dset of refs * add validation tests for references * remove old comment * update CHANGELOG --- CHANGELOG.md | 3 ++ src/hdmf/validate/validator.py | 6 +++- tests/unit/validator_tests/test_validate.py | 32 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) 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 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 diff --git a/tests/unit/validator_tests/test_validate.py b/tests/unit/validator_tests/test_validate.py index dd79cfce5..64667b3e0 100644 --- a/tests/unit/validator_tests/test_validate.py +++ b/tests/unit/validator_tests/test_validate.py @@ -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):