Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mavaylon1 committed Nov 14, 2024
1 parent 1a65ebd commit 43618c3
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 177 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## HDMF 4.0.0 (Upcoming)

### Deprecations
- The following classes have been deprecated and removed: Array, AbstractSortedArray, SortedArray, LinSpace, Query, RegionSlicer, ListSlicer, H5RegionSlicer, DataRegion. The following methods have been deprecated and removed: get_container_cls, add_child, set_dataio (now refactored as set_data_io). We have also removed all early evelopment for region references. @mavaylon1 [#1998](https://github.com/hdmf-dev/hdmf/pull/1198)
- The following classes have been deprecated and removed: Array, AbstractSortedArray, SortedArray, LinSpace, Query, RegionSlicer, ListSlicer, H5RegionSlicer, DataRegion. The following methods have been deprecated and removed: fmt_docval_args, call_docval_func, get_container_cls, add_child, set_dataio (now refactored as set_data_io). We have also removed all early evelopment for region references. @mavaylon1 [#1998](https://github.com/hdmf-dev/hdmf/pull/1198)

### Enhancements
- Added support for expandable datasets of references for untyped and compound data types. @stephprince [#1188](https://github.com/hdmf-dev/hdmf/pull/1188)
Expand Down
9 changes: 0 additions & 9 deletions src/hdmf/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@

@docval_macro('array_data')
class HDMFDataset(metaclass=ExtenderMeta):
__operations__ = (
'__lt__',
'__gt__',
'__le__',
'__ge__',
'__eq__',
'__ne__',
)

def __evaluate_key(self, key):
if isinstance(key, tuple) and len(key) == 0:
return key
Expand Down
91 changes: 0 additions & 91 deletions src/hdmf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,6 @@ def __parse_args(validator, args, kwargs, enforce_type=True, enforce_shape=True,
for key in extras.keys():
type_errors.append("unrecognized argument: '%s'" % key)
else:
# TODO: Extras get stripped out if function arguments are composed with fmt_docval_args.
# allow_extra needs to be tracked on a function so that fmt_docval_args doesn't strip them out
for key in extras.keys():
ret[key] = extras[key]
return {'args': ret, 'future_warnings': future_warnings, 'type_errors': type_errors, 'value_errors': value_errors,
Expand Down Expand Up @@ -414,95 +412,6 @@ def get_docval(func, *args):
return tuple()


# def docval_wrap(func, is_method=True):
# if is_method:
# @docval(*get_docval(func))
# def method(self, **kwargs):
#
# return call_docval_args(func, kwargs)
# return method
# else:
# @docval(*get_docval(func))
# def static_method(**kwargs):
# return call_docval_args(func, kwargs)
# return method


def fmt_docval_args(func, kwargs):
''' Separate positional and keyword arguments
Useful for methods that wrap other methods
'''
warnings.warn("fmt_docval_args will be deprecated in a future version of HDMF. Instead of using fmt_docval_args, "
"call the function directly with the kwargs. Please note that fmt_docval_args "
"removes all arguments not accepted by the function's docval, so if you are passing kwargs that "
"includes extra arguments and the function's docval does not allow extra arguments (allow_extra=True "
"is set), then you will need to pop the extra arguments out of kwargs before calling the function.",
PendingDeprecationWarning, stacklevel=2)
func_docval = getattr(func, docval_attr_name, None)
ret_args = list()
ret_kwargs = dict()
kwargs_copy = _copy.copy(kwargs)
if func_docval:
for arg in func_docval[__docval_args_loc]:
val = kwargs_copy.pop(arg['name'], None)
if 'default' in arg:
if val is not None:
ret_kwargs[arg['name']] = val
else:
ret_args.append(val)
if func_docval['allow_extra']:
ret_kwargs.update(kwargs_copy)
else:
raise ValueError('no docval found on %s' % str(func))
return ret_args, ret_kwargs


# def _remove_extra_args(func, kwargs):
# """Return a dict of only the keyword arguments that are accepted by the function's docval.
#
# If the docval specifies allow_extra=True, then the original kwargs are returned.
# """
# # NOTE: this has the same functionality as the to-be-deprecated fmt_docval_args except that
# # kwargs are kept as kwargs instead of parsed into args and kwargs
# func_docval = getattr(func, docval_attr_name, None)
# if func_docval:
# if func_docval['allow_extra']:
# # if extra args are allowed, return all args
# return kwargs
# else:
# # save only the arguments listed in the function's docval (skip any others present in kwargs)
# ret_kwargs = dict()
# for arg in func_docval[__docval_args_loc]:
# val = kwargs.get(arg['name'], None)
# if val is not None: # do not return arguments that are not present or have value None
# ret_kwargs[arg['name']] = val
# return ret_kwargs
# else:
# raise ValueError('No docval found on %s' % str(func))


def call_docval_func(func, kwargs):
"""Call the function with only the keyword arguments that are accepted by the function's docval.
Extra keyword arguments are not passed to the function unless the function's docval has allow_extra=True.
"""
warnings.warn("call_docval_func will be deprecated in a future version of HDMF. Instead of using call_docval_func, "
"call the function directly with the kwargs. Please note that call_docval_func "
"removes all arguments not accepted by the function's docval, so if you are passing kwargs that "
"includes extra arguments and the function's docval does not allow extra arguments (allow_extra=True "
"is set), then you will need to pop the extra arguments out of kwargs before calling the function.",
PendingDeprecationWarning, stacklevel=2)
with warnings.catch_warnings(record=True):
# catch and ignore only PendingDeprecationWarnings from fmt_docval_args so that two
# PendingDeprecationWarnings saying the same thing are not raised
warnings.simplefilter("ignore", UserWarning)
warnings.simplefilter("always", PendingDeprecationWarning)
fargs, fkwargs = fmt_docval_args(func, kwargs)

return func(*fargs, **fkwargs)


def __resolve_type(t):
if t is None:
return t
Expand Down
78 changes: 2 additions & 76 deletions tests/unit/utils_test/test_docval.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
from hdmf.testing import TestCase
from hdmf.utils import (docval, fmt_docval_args, get_docval, getargs, popargs, AllowPositional, get_docval_macro,
docval_macro, popargs_to_dict, call_docval_func)
from hdmf.utils import (docval, get_docval, getargs, popargs, AllowPositional, get_docval_macro,
docval_macro, popargs_to_dict)


class MyTestClass(object):
Expand Down Expand Up @@ -137,80 +137,6 @@ def method1(self, **kwargs):
with self.assertRaises(ValueError):
method1(self, arg1=[[1, 1, 1]])

fmt_docval_warning_msg = (
"fmt_docval_args will be deprecated in a future version of HDMF. Instead of using fmt_docval_args, "
"call the function directly with the kwargs. Please note that fmt_docval_args "
"removes all arguments not accepted by the function's docval, so if you are passing kwargs that "
"includes extra arguments and the function's docval does not allow extra arguments (allow_extra=True "
"is set), then you will need to pop the extra arguments out of kwargs before calling the function."
)

def test_fmt_docval_args(self):
""" Test that fmt_docval_args parses the args and strips extra args """
test_kwargs = {
'arg1': 'a string',
'arg2': 1,
'arg3': True,
'hello': 'abc',
'list': ['abc', 1, 2, 3]
}
with self.assertWarnsWith(PendingDeprecationWarning, self.fmt_docval_warning_msg):
rec_args, rec_kwargs = fmt_docval_args(self.test_obj.basic_add2_kw, test_kwargs)
exp_args = ['a string', 1]
self.assertListEqual(rec_args, exp_args)
exp_kwargs = {'arg3': True}
self.assertDictEqual(rec_kwargs, exp_kwargs)

def test_fmt_docval_args_no_docval(self):
""" Test that fmt_docval_args raises an error when run on function without docval """
def method1(self, **kwargs):
pass

with self.assertRaisesRegex(ValueError, r"no docval found on .*method1.*"):
with self.assertWarnsWith(PendingDeprecationWarning, self.fmt_docval_warning_msg):
fmt_docval_args(method1, {})

def test_fmt_docval_args_allow_extra(self):
""" Test that fmt_docval_args works """
test_kwargs = {
'arg1': 'a string',
'arg2': 1,
'arg3': True,
'hello': 'abc',
'list': ['abc', 1, 2, 3]
}
with self.assertWarnsWith(PendingDeprecationWarning, self.fmt_docval_warning_msg):
rec_args, rec_kwargs = fmt_docval_args(self.test_obj.basic_add2_kw_allow_extra, test_kwargs)
exp_args = ['a string', 1]
self.assertListEqual(rec_args, exp_args)
exp_kwargs = {'arg3': True, 'hello': 'abc', 'list': ['abc', 1, 2, 3]}
self.assertDictEqual(rec_kwargs, exp_kwargs)

def test_call_docval_func(self):
"""Test that call_docval_func strips extra args and calls the function."""
test_kwargs = {
'arg1': 'a string',
'arg2': 1,
'arg3': True,
'hello': 'abc',
'list': ['abc', 1, 2, 3]
}
msg = (
"call_docval_func will be deprecated in a future version of HDMF. Instead of using call_docval_func, "
"call the function directly with the kwargs. Please note that call_docval_func "
"removes all arguments not accepted by the function's docval, so if you are passing kwargs that "
"includes extra arguments and the function's docval does not allow extra arguments (allow_extra=True "
"is set), then you will need to pop the extra arguments out of kwargs before calling the function."
)
with self.assertWarnsWith(PendingDeprecationWarning, msg):
ret_kwargs = call_docval_func(self.test_obj.basic_add2_kw, test_kwargs)
exp_kwargs = {
'arg1': 'a string',
'arg2': 1,
'arg3': True
}
self.assertDictEqual(ret_kwargs, exp_kwargs)

def test_docval_add(self):
"""Test that docval works with a single positional
argument
Expand Down

0 comments on commit 43618c3

Please sign in to comment.