Skip to content
Open
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
5 changes: 5 additions & 0 deletions src/hdmf/validate/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/validator_tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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([])
Loading