Skip to content

Commit

Permalink
Allow np.bool_ as a valid type when bool is specified in spec
Browse files Browse the repository at this point in the history
* Fix hdmf-dev#504
* Ref NeurodataWithoutBorders/pynwb#1298
* Include bool as a scalar type
  • Loading branch information
dsleiter committed Jan 4, 2021
1 parent bd18338 commit b7fac86
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/hdmf/build/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ def load_namespaces(self, **kwargs):
'utf-8': str,
'ascii': bytes,
'bytes': bytes,
'bool': bool,
'bool': (bool, np.bool_),
'isodatetime': datetime,
'datetime': datetime
}
Expand Down
2 changes: 1 addition & 1 deletion src/hdmf/spec/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DtypeHelper:
'long': ["int64", "long"],
'utf': ["text", "utf", "utf8", "utf-8"],
'ascii': ["ascii", "bytes"],
'bool': ["bool"],
'bool': ["bool", "bool_"],
'int8': ["int8"],
'uint8': ["uint8"],
'uint16': ["uint16"],
Expand Down
2 changes: 1 addition & 1 deletion src/hdmf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

__macros = {
'array_data': [np.ndarray, list, tuple, h5py.Dataset],
'scalar_data': [str, int, float, bytes],
'scalar_data': [str, int, float, bytes, bool],
'data': []
}

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/utils_test/test_docval.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,13 +976,13 @@ def test_macro(self):
self.assertTrue(isinstance(get_docval_macro(), dict))
self.assertSetEqual(set(get_docval_macro().keys()), {'array_data', 'scalar_data', 'data'})

self.assertTupleEqual(get_docval_macro('scalar_data'), (str, int, float, bytes))
self.assertTupleEqual(get_docval_macro('scalar_data'), (str, int, float, bytes, bool))

@docval_macro('scalar_data')
class Dummy1:
pass

self.assertTupleEqual(get_docval_macro('scalar_data'), (str, int, float, bytes, Dummy1))
self.assertTupleEqual(get_docval_macro('scalar_data'), (str, int, float, bytes, bool, Dummy1))

@docval_macro('dummy')
class Dummy2:
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/validator_tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,16 @@ def test_bool_for_numeric(self):
"Bar/data (my_bar/data): incorrect type - expected 'numeric', got 'bool'"}
self.assertEqual(result_strings, expected_errors)

def test_np_bool_for_bool(self):
"""Test that validator allows np.bool_ data where bool is specified."""
self.set_up_spec('bool')
value = np.bool_(True)
bar_builder = GroupBuilder('my_bar',
attributes={'data_type': 'Bar', 'attr1': value},
datasets=[DatasetBuilder('data', value)])
results = self.vmap.validate(bar_builder)
self.assertEqual(len(results), 0)


class Test1DArrayValidation(TestCase):

Expand Down

0 comments on commit b7fac86

Please sign in to comment.