Skip to content

Commit

Permalink
Add get_docval_macro method (#446)
Browse files Browse the repository at this point in the history
* Add get_docval_macro method

* Update changelog

* Update get_docval_macro, add 'data' to macros

* Update test
  • Loading branch information
rly committed Oct 31, 2020
1 parent 33f79f2 commit cc8fa35
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
- Add support for creating and specifying multi-index columns in a `DynamicTable` using `add_column(...)`.
@bendichter, @rly (#430)
- Add capability to add a row to a column after IO. @bendichter (#426)
- Add method `hdmf.utils.get_docval_macro` to get a tuple of the current values for a docval_macro, e.g., 'array_data'
and 'scalar_data'. @rly (#456)

### Internal improvements
- Refactor `HDF5IO.write_dataset` to be more readable. @rly (#428)
Expand Down
14 changes: 14 additions & 0 deletions src/hdmf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
__macros = {
'array_data': [np.ndarray, list, tuple, h5py.Dataset],
'scalar_data': [str, int, float, bytes],
'data': []
}

# code to signify how to handle positional arguments in docval
Expand Down Expand Up @@ -41,6 +42,19 @@ def _dec(cls):
return _dec


def get_docval_macro(key=None):
"""
Return a deepcopy of the docval macros, i.e., strings that represent a customizable list of types for use in docval.
:param key: Name of the macro. If key=None, then a dictionary of all macros is returned. Otherwise, a tuple of
the types associated with the key is returned.
"""
if key is None:
return _copy.deepcopy(__macros)
else:
return tuple(__macros[key])


def __type_okay(value, argtype, allow_none=False):
"""Check a value against a type
Expand Down
24 changes: 23 additions & 1 deletion tests/unit/utils_test/test_docval.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np

from hdmf.utils import docval, fmt_docval_args, get_docval, getargs, popargs, AllowPositional
from hdmf.utils import (docval, fmt_docval_args, get_docval, getargs, popargs, AllowPositional, get_docval_macro,
docval_macro)
from hdmf.testing import TestCase


Expand Down Expand Up @@ -968,3 +969,24 @@ def test_arg_not_found_many_args(self):
msg = "Argument not found in dict: 'c'"
with self.assertRaisesWith(ValueError, msg):
popargs('a', 'c', kwargs)


class TestMacro(TestCase):

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))

@docval_macro('scalar_data')
class Dummy1:
pass

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

@docval_macro('dummy')
class Dummy2:
pass

self.assertTupleEqual(get_docval_macro('dummy'), (Dummy2, ))

0 comments on commit cc8fa35

Please sign in to comment.