Skip to content

Commit

Permalink
Fix convert_dtype for empty list and text/ascii spec (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
rly committed Oct 7, 2020
1 parent 1e790ea commit 2c5a977
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# HDMF Changelog

## HDMF 2.3.0 (Upcoming)

### Bug fixes
- Fix handling of empty lists against a spec with text/bytes dtype. @rly (#434)

## HDMF 2.2.0 (August 14, 2020)

### New features
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exclude =
versioneer.py
src/hdmf/_version.py
per-file-ignores =
docs/gallery/*:E402,
docs/gallery/*:E402,T001
docs/source/tutorials/*:E402
src/hdmf/__init__.py:F401
src/hdmf/backends/__init__.py:F401
Expand Down
8 changes: 7 additions & 1 deletion src/hdmf/build/objectmapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,13 @@ def convert_dtype(cls, spec, value, spec_dtype=None):
ret_dtype = ret.dtype.type
elif isinstance(value, (tuple, list)):
if len(value) == 0:
return value, spec_dtype_type
if spec_dtype_type == _ascii:
ret_dtype = 'ascii'
elif spec_dtype_type == _unicode:
ret_dtype = 'utf8'
else:
ret_dtype = spec_dtype_type
return value, ret_dtype
ret = list()
for elem in value:
tmp, tmp_dtype = cls.convert_dtype(spec, elem, spec_dtype)
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/build_tests/test_io_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,11 @@ def test_text_spec(self):
np.testing.assert_array_equal(ret, np.array(['a', 'b'], dtype='U1'))
self.assertEqual(ret_dtype, 'utf8')

value = []
ret, ret_dtype = ObjectMapper.convert_dtype(spec, value)
self.assertListEqual(ret, value)
self.assertEqual(ret_dtype, 'utf8')

def test_ascii_spec(self):
spec_type = 'ascii'
spec = DatasetSpec('an example dataset', spec_type, name='data')
Expand Down Expand Up @@ -1195,6 +1200,11 @@ def test_ascii_spec(self):
np.testing.assert_array_equal(ret, value)
self.assertEqual(ret_dtype, 'ascii')

value = []
ret, ret_dtype = ObjectMapper.convert_dtype(spec, value)
self.assertListEqual(ret, value)
self.assertEqual(ret_dtype, 'ascii')

def test_no_spec(self):
spec_type = None
spec = DatasetSpec('an example dataset', spec_type, name='data')
Expand Down

0 comments on commit 2c5a977

Please sign in to comment.