diff --git a/src/hdmf/validate/validator.py b/src/hdmf/validate/validator.py index c774a8bf1..625207186 100644 --- a/src/hdmf/validate/validator.py +++ b/src/hdmf/validate/validator.py @@ -175,6 +175,11 @@ def _get_type_from_dtype_attr(data: Any, builder_dtype: list | None) -> tuple[st def get_type(data, builder_dtype=None): """Return a tuple of (the string representation of the type, the format of the string data) for the given data.""" + if data is None: + return None, None + # Check for empty data safely + if hasattr(data, "__len__") and getattr(data, 'shape', (1,)) != () and len(data) == 0: + raise EmptyArrayError("Dataset is empty; cannot determine type.") # String data if isinstance(data, str): return 'utf', get_string_format(data) diff --git a/tests/unit/validator_tests/test_validate.py b/tests/unit/validator_tests/test_validate.py index 8cdfb953e..cbb6915c1 100644 --- a/tests/unit/validator_tests/test_validate.py +++ b/tests/unit/validator_tests/test_validate.py @@ -4,6 +4,8 @@ import h5py import numpy as np +import pytest +from hdmf.validate.validator import get_type, EmptyArrayError from dateutil.tz import tzlocal from hdmf.build import GroupBuilder, DatasetBuilder, LinkBuilder, ReferenceBuilder, TypeMap, BuildManager from hdmf.spec import (GroupSpec, AttributeSpec, DatasetSpec, SpecCatalog, SpecNamespace, @@ -1907,3 +1909,9 @@ def test_isodatetime_no_time_component_fails(self): # This confirms it fails because it lacks the 'T' and timezone self.assertEqual(len(result), 1) self.assertIsInstance(result[0], Error) + +def test_get_type_empty_data(): + """Test that get_type handles empty data with an informative error (Issue #775).""" + assert get_type(None) == (None, None) + with pytest.raises(EmptyArrayError, match="Dataset is empty; cannot determine type."): + get_type([])