| 1 |
import numpy as np |
| 2 |
from hdmf.testing import TestCase |
| 3 |
from hdmf.utils import (docval, fmt_docval_args, get_docval, getargs, popargs, AllowPositional, get_docval_macro, |
| 4 |
docval_macro, popargs_to_dict, call_docval_func) |
| 5 |
|
| 6 |
|
| 7 |
class MyTestClass(object): |
| 8 |
|
| 9 |
@docval({'name': 'arg1', 'type': str, 'doc': 'argument1 is a str'}) |
| 10 |
def basic_add(self, **kwargs): |
| 11 |
return kwargs |
| 12 |
|
| 13 |
@docval({'name': 'arg1', 'type': str, 'doc': 'argument1 is a str'}, |
| 14 |
{'name': 'arg2', 'type': int, 'doc': 'argument2 is a int'}) |
| 15 |
def basic_add2(self, **kwargs): |
| 16 |
return kwargs |
| 17 |
|
| 18 |
@docval({'name': 'arg1', 'type': str, 'doc': 'argument1 is a str'}, |
| 19 |
{'name': 'arg2', 'type': 'int', 'doc': 'argument2 is a int'}, |
| 20 |
{'name': 'arg3', 'type': bool, 'doc': 'argument3 is a bool. it defaults to False', 'default': False}) |
| 21 |
def basic_add2_kw(self, **kwargs): |
| 22 |
return kwargs |
| 23 |
|
| 24 |
@docval({'name': 'arg1', 'type': str, 'doc': 'argument1 is a str', 'default': 'a'}, |
| 25 |
{'name': 'arg2', 'type': int, 'doc': 'argument2 is a int', 'default': 1}) |
| 26 |
def basic_only_kw(self, **kwargs): |
| 27 |
return kwargs |
| 28 |
|
| 29 |
@docval({'name': 'arg1', 'type': str, 'doc': 'argument1 is a str'}, |
| 30 |
{'name': 'arg2', 'type': 'int', 'doc': 'argument2 is a int'}, |
| 31 |
{'name': 'arg3', 'type': bool, 'doc': 'argument3 is a bool. it defaults to False', 'default': False}, |
| 32 |
allow_extra=True) |
| 33 |
def basic_add2_kw_allow_extra(self, **kwargs): |
| 34 |
return kwargs |
| 35 |
|
| 36 |
|
| 37 |
class MyTestSubclass(MyTestClass): |
| 38 |
|
| 39 |
@docval({'name': 'arg1', 'type': str, 'doc': 'argument1 is a str'}, |
| 40 |
{'name': 'arg2', 'type': int, 'doc': 'argument2 is a int'}) |
| 41 |
def basic_add(self, **kwargs): |
| 42 |
return kwargs |
| 43 |
|
| 44 |
@docval({'name': 'arg1', 'type': str, 'doc': 'argument1 is a str'}, |
| 45 |
{'name': 'arg2', 'type': int, 'doc': 'argument2 is a int'}, |
| 46 |
{'name': 'arg3', 'type': bool, 'doc': 'argument3 is a bool. it defaults to False', 'default': False}, |
| 47 |
{'name': 'arg4', 'type': str, 'doc': 'argument4 is a str'}, |
| 48 |
{'name': 'arg5', 'type': 'float', 'doc': 'argument5 is a float'}, |
| 49 |
{'name': 'arg6', 'type': bool, 'doc': 'argument6 is a bool. it defaults to None', 'default': None}) |
| 50 |
def basic_add2_kw(self, **kwargs): |
| 51 |
return kwargs |
| 52 |
|
| 53 |
|
| 54 |
class MyChainClass(MyTestClass): |
| 55 |
|
| 56 |
@docval({'name': 'arg1', 'type': (str, 'MyChainClass'), 'doc': 'arg1 is a string or MyChainClass'}, |
| 57 |
{'name': 'arg2', 'type': ('array_data', 'MyChainClass'), |
| 58 |
'doc': 'arg2 is array data or MyChainClass. it defaults to None', 'default': None}, |
| 59 |
{'name': 'arg3', 'type': ('array_data', 'MyChainClass'), 'doc': 'arg3 is array data or MyChainClass', |
| 60 |
'shape': (None, 2)}, |
| 61 |
{'name': 'arg4', 'type': ('array_data', 'MyChainClass'), |
| 62 |
'doc': 'arg3 is array data or MyChainClass. it defaults to None.', 'shape': (None, 2), 'default': None}) |
| 63 |
def __init__(self, **kwargs): |
| 64 |
self._arg1, self._arg2, self._arg3, self._arg4 = popargs('arg1', 'arg2', 'arg3', 'arg4', kwargs) |
| 65 |
|
| 66 |
@property |
| 67 |
def arg1(self): |
| 68 |
if isinstance(self._arg1, MyChainClass): |
| 69 |
return self._arg1.arg1 |
| 70 |
else: |
| 71 |
return self._arg1 |
| 72 |
|
| 73 |
@property |
| 74 |
def arg2(self): |
| 75 |
if isinstance(self._arg2, MyChainClass): |
| 76 |
return self._arg2.arg2 |
| 77 |
else: |
| 78 |
return self._arg2 |
| 79 |
|
| 80 |
@property |
| 81 |
def arg3(self): |
| 82 |
if isinstance(self._arg3, MyChainClass): |
| 83 |
return self._arg3.arg3 |
| 84 |
else: |
| 85 |
return self._arg3 |
| 86 |
|
| 87 |
@arg3.setter |
| 88 |
def arg3(self, val): |
| 89 |
self._arg3 = val |
| 90 |
|
| 91 |
@property |
| 92 |
def arg4(self): |
| 93 |
if isinstance(self._arg4, MyChainClass): |
| 94 |
return self._arg4.arg4 |
| 95 |
else: |
| 96 |
return self._arg4 |
| 97 |
|
| 98 |
@arg4.setter |
| 99 |
def arg4(self, val): |
| 100 |
self._arg4 = val |
| 101 |
|
| 102 |
|
| 103 |
class TestDocValidator(TestCase): |
| 104 |
|
| 105 |
def setUp(self): |
| 106 |
self.test_obj = MyTestClass() |
| 107 |
self.test_obj_sub = MyTestSubclass() |
| 108 |
|
| 109 |
def test_bad_type(self): |
| 110 |
exp_msg = (r"docval for arg1: error parsing argument type: argtype must be a type, " |
| 111 |
r"a str, a list, a tuple, or None - got <class|type 'dict'>") |
| 112 |
with self.assertRaisesRegex(Exception, exp_msg): |
| 113 |
@docval({'name': 'arg1', 'type': {'a': 1}, 'doc': 'this is a bad type'}) |
| 114 |
def method(self, **kwargs): |
| 115 |
pass |
| 116 |
method(self, arg1=1234560) |
| 117 |
|
| 118 |
def test_bad_shape(self): |
| 119 |
@docval({'name': 'arg1', 'type': 'array_data', 'doc': 'this is a bad shape', 'shape': (None, 2)}) |
| 120 |
def method(self, **kwargs): |
| 121 |
pass |
| 122 |
with self.assertRaises(ValueError): |
| 123 |
method(self, arg1=[[1]]) |
| 124 |
with self.assertRaises(ValueError): |
| 125 |
method(self, arg1=[1]) |
| 126 |
# this should work |
| 127 |
method(self, arg1=[[1, 1]]) |
| 128 |
|
| 129 |
def test_multi_shape(self): |
| 130 |
@docval({'name': 'arg1', 'type': 'array_data', 'doc': 'this is a bad shape', |
| 131 |
'shape': ((None,), (None, 2))}) |
| 132 |
def method1(self, **kwargs): |
| 133 |
pass |
| 134 |
|
| 135 |
method1(self, arg1=[[1, 1]]) |
| 136 |
method1(self, arg1=[1, 2]) |
| 137 |
with self.assertRaises(ValueError): |
| 138 |
method1(self, arg1=[[1, 1, 1]]) |
| 139 |
|
| 140 |
fmt_docval_warning_msg = ( |
| 141 |
"fmt_docval_args will be deprecated in a future version of HDMF. Instead of using fmt_docval_args, " |
| 142 |
"call the function directly with the kwargs. Please note that fmt_docval_args " |
| 143 |
"removes all arguments not accepted by the function's docval, so if you are passing kwargs that " |
| 144 |
"includes extra arguments and the function's docval does not allow extra arguments (allow_extra=True " |
| 145 |
"is set), then you will need to pop the extra arguments out of kwargs before calling the function." |
| 146 |
) |
| 147 |
|
| 148 |
def test_fmt_docval_args(self): |
| 149 |
""" Test that fmt_docval_args parses the args and strips extra args """ |
| 150 |
test_kwargs = { |
| 151 |
'arg1': 'a string', |
| 152 |
'arg2': 1, |
| 153 |
'arg3': True, |
| 154 |
'hello': 'abc', |
| 155 |
'list': ['abc', 1, 2, 3] |
| 156 |
} |
| 157 |
with self.assertWarnsWith(PendingDeprecationWarning, self.fmt_docval_warning_msg): |
| 158 |
rec_args, rec_kwargs = fmt_docval_args(self.test_obj.basic_add2_kw, test_kwargs) |
| 159 |
exp_args = ['a string', 1] |
| 160 |
self.assertListEqual(rec_args, exp_args) |
| 161 |
exp_kwargs = {'arg3': True} |
| 162 |
self.assertDictEqual(rec_kwargs, exp_kwargs) |
| 163 |
|
| 164 |
def test_fmt_docval_args_no_docval(self): |
| 165 |
""" Test that fmt_docval_args raises an error when run on function without docval """ |
| 166 |
def method1(self, **kwargs): |
| 167 |
pass |
| 168 |
|
| 169 |
with self.assertRaisesRegex(ValueError, r"no docval found on .*method1.*"): |
| 170 |
with self.assertWarnsWith(PendingDeprecationWarning, self.fmt_docval_warning_msg): |
| 171 |
fmt_docval_args(method1, {}) |
| 172 |
|
| 173 |
def test_fmt_docval_args_allow_extra(self): |
| 174 |
""" Test that fmt_docval_args works """ |
| 175 |
test_kwargs = { |
| 176 |
'arg1': 'a string', |
| 177 |
'arg2': 1, |
| 178 |
'arg3': True, |
| 179 |
'hello': 'abc', |
| 180 |
'list': ['abc', 1, 2, 3] |
| 181 |
} |
| 182 |
with self.assertWarnsWith(PendingDeprecationWarning, self.fmt_docval_warning_msg): |
| 183 |
rec_args, rec_kwargs = fmt_docval_args(self.test_obj.basic_add2_kw_allow_extra, test_kwargs) |
| 184 |
exp_args = ['a string', 1] |
| 185 |
self.assertListEqual(rec_args, exp_args) |
| 186 |
exp_kwargs = {'arg3': True, 'hello': 'abc', 'list': ['abc', 1, 2, 3]} |
| 187 |
self.assertDictEqual(rec_kwargs, exp_kwargs) |
| 188 |
|
| 189 |
def test_call_docval_func(self): |
| 190 |
"""Test that call_docval_func strips extra args and calls the function.""" |
| 191 |
test_kwargs = { |
| 192 |
'arg1': 'a string', |
| 193 |
'arg2': 1, |
| 194 |
'arg3': True, |
| 195 |
'hello': 'abc', |
| 196 |
'list': ['abc', 1, 2, 3] |
| 197 |
} |
| 198 |
msg = ( |
| 199 |
"call_docval_func will be deprecated in a future version of HDMF. Instead of using call_docval_func, " |
| 200 |
"call the function directly with the kwargs. Please note that call_docval_func " |
| 201 |
"removes all arguments not accepted by the function's docval, so if you are passing kwargs that " |
| 202 |
"includes extra arguments and the function's docval does not allow extra arguments (allow_extra=True " |
| 203 |
"is set), then you will need to pop the extra arguments out of kwargs before calling the function." |
| 204 |
) |
| 205 |
with self.assertWarnsWith(PendingDeprecationWarning, msg): |
| 206 |
ret_kwargs = call_docval_func(self.test_obj.basic_add2_kw, test_kwargs) |
| 207 |
exp_kwargs = { |
| 208 |
'arg1': 'a string', |
| 209 |
'arg2': 1, |
| 210 |
'arg3': True |
| 211 |
} |
| 212 |
self.assertDictEqual(ret_kwargs, exp_kwargs) |
| 213 |
|
| 214 |
def test_docval_add(self): |
| 215 |
"""Test that docval works with a single positional |
| 216 |
argument |
| 217 |
""" |
| 218 |
kwargs = self.test_obj.basic_add('a string') |
| 219 |
self.assertDictEqual(kwargs, {'arg1': 'a string'}) |
| 220 |
|
| 221 |
def test_docval_add_kw(self): |
| 222 |
"""Test that docval works with a single positional |
| 223 |
argument passed as key-value |
| 224 |
""" |
| 225 |
kwargs = self.test_obj.basic_add(arg1='a string') |
| 226 |
self.assertDictEqual(kwargs, {'arg1': 'a string'}) |
| 227 |
|
| 228 |
def test_docval_add_missing_args(self): |
| 229 |
"""Test that docval catches missing argument |
| 230 |
with a single positional argument |
| 231 |
""" |
| 232 |
with self.assertRaisesWith(TypeError, "MyTestClass.basic_add: missing argument 'arg1'"): |
| 233 |
self.test_obj.basic_add() |
| 234 |
|
| 235 |
def test_docval_add2(self): |
| 236 |
"""Test that docval works with two positional |
| 237 |
arguments |
| 238 |
""" |
| 239 |
kwargs = self.test_obj.basic_add2('a string', 100) |
| 240 |
self.assertDictEqual(kwargs, {'arg1': 'a string', 'arg2': 100}) |
| 241 |
|
| 242 |
def test_docval_add2_w_unicode(self): |
| 243 |
"""Test that docval works with two positional |
| 244 |
arguments |
| 245 |
""" |
| 246 |
kwargs = self.test_obj.basic_add2(u'a string', 100) |
| 247 |
self.assertDictEqual(kwargs, {'arg1': u'a string', 'arg2': 100}) |
| 248 |
|
| 249 |
def test_docval_add2_kw_default(self): |
| 250 |
"""Test that docval works with two positional |
| 251 |
arguments and a keyword argument when using |
| 252 |
default keyword argument value |
| 253 |
""" |
| 254 |
kwargs = self.test_obj.basic_add2_kw('a string', 100) |
| 255 |
self.assertDictEqual(kwargs, {'arg1': 'a string', 'arg2': 100, 'arg3': False}) |
| 256 |
|
| 257 |
def test_docval_add2_pos_as_kw(self): |
| 258 |
"""Test that docval works with two positional |
| 259 |
arguments and a keyword argument when using |
| 260 |
default keyword argument value, but pass |
| 261 |
positional arguments by key-value |
| 262 |
""" |
| 263 |
kwargs = self.test_obj.basic_add2_kw(arg1='a string', arg2=100) |
| 264 |
self.assertDictEqual(kwargs, {'arg1': 'a string', 'arg2': 100, 'arg3': False}) |
| 265 |
|
| 266 |
def test_docval_add2_kw_kw_syntax(self): |
| 267 |
"""Test that docval works with two positional |
| 268 |
arguments and a keyword argument when specifying |
| 269 |
keyword argument value with keyword syntax |
| 270 |
""" |
| 271 |
kwargs = self.test_obj.basic_add2_kw('a string', 100, arg3=True) |
| 272 |
self.assertDictEqual(kwargs, {'arg1': 'a string', 'arg2': 100, 'arg3': True}) |
| 273 |
|
| 274 |
def test_docval_add2_kw_all_kw_syntax(self): |
| 275 |
"""Test that docval works with two positional |
| 276 |
arguments and a keyword argument when specifying |
| 277 |
all arguments by key-value |
| 278 |
""" |
| 279 |
kwargs = self.test_obj.basic_add2_kw(arg1='a string', arg2=100, arg3=True) |
| 280 |
self.assertDictEqual(kwargs, {'arg1': 'a string', 'arg2': 100, 'arg3': True}) |
| 281 |
|
| 282 |
def test_docval_add2_kw_pos_syntax(self): |
| 283 |
"""Test that docval works with two positional |
| 284 |
arguments and a keyword argument when specifying |
| 285 |
keyword argument value with positional syntax |
| 286 |
""" |
| 287 |
kwargs = self.test_obj.basic_add2_kw('a string', 100, True) |
| 288 |
self.assertDictEqual(kwargs, {'arg1': 'a string', 'arg2': 100, 'arg3': True}) |
| 289 |
|
| 290 |
def test_docval_add2_kw_pos_syntax_missing_args(self): |
| 291 |
"""Test that docval catches incorrect type with two positional |
| 292 |
arguments and a keyword argument when specifying |
| 293 |
keyword argument value with positional syntax |
| 294 |
""" |
| 295 |
msg = "MyTestClass.basic_add2_kw: incorrect type for 'arg2' (got 'str', expected 'int')" |
| 296 |
with self.assertRaisesWith(TypeError, msg): |
| 297 |
self.test_obj.basic_add2_kw('a string', 'bad string') |
| 298 |
|
| 299 |
def test_docval_add_sub(self): |
| 300 |
"""Test that docval works with a two positional arguments, |
| 301 |
where the second is specified by the subclass implementation |
| 302 |
""" |
| 303 |
kwargs = self.test_obj_sub.basic_add('a string', 100) |
| 304 |
expected = {'arg1': 'a string', 'arg2': 100} |
| 305 |
self.assertDictEqual(kwargs, expected) |
| 306 |
|
| 307 |
def test_docval_add2_kw_default_sub(self): |
| 308 |
"""Test that docval works with a four positional arguments and |
| 309 |
two keyword arguments, where two positional and one keyword |
| 310 |
argument is specified in both the parent and subclass implementations |
| 311 |
""" |
| 312 |
kwargs = self.test_obj_sub.basic_add2_kw('a string', 100, 'another string', 200.0) |
| 313 |
expected = {'arg1': 'a string', 'arg2': 100, |
| 314 |
'arg4': 'another string', 'arg5': 200.0, |
| 315 |
'arg3': False, 'arg6': None} |
| 316 |
self.assertDictEqual(kwargs, expected) |
| 317 |
|
| 318 |
def test_docval_add2_kw_default_sub_missing_args(self): |
| 319 |
"""Test that docval catches missing arguments with a four positional arguments |
| 320 |
and two keyword arguments, where two positional and one keyword |
| 321 |
argument is specified in both the parent and subclass implementations, |
| 322 |
when using default values for keyword arguments |
| 323 |
""" |
| 324 |
with self.assertRaisesWith(TypeError, "MyTestSubclass.basic_add2_kw: missing argument 'arg5'"): |
| 325 |
self.test_obj_sub.basic_add2_kw('a string', 100, 'another string') |
| 326 |
|
| 327 |
def test_docval_add2_kw_kwsyntax_sub(self): |
| 328 |
"""Test that docval works when called with a four positional |
| 329 |
arguments and two keyword arguments, where two positional |
| 330 |
and one keyword argument is specified in both the parent |
| 331 |
and subclass implementations |
| 332 |
""" |
| 333 |
kwargs = self.test_obj_sub.basic_add2_kw('a string', 100, 'another string', 200.0, arg6=True) |
| 334 |
expected = {'arg1': 'a string', 'arg2': 100, |
| 335 |
'arg4': 'another string', 'arg5': 200.0, |
| 336 |
'arg3': False, 'arg6': True} |
| 337 |
self.assertDictEqual(kwargs, expected) |
| 338 |
|
| 339 |
def test_docval_add2_kw_kwsyntax_sub_missing_args(self): |
| 340 |
"""Test that docval catches missing arguments when called with a four positional |
| 341 |
arguments and two keyword arguments, where two positional and one keyword |
| 342 |
argument is specified in both the parent and subclass implementations |
| 343 |
""" |
| 344 |
with self.assertRaisesWith(TypeError, "MyTestSubclass.basic_add2_kw: missing argument 'arg5'"): |
| 345 |
self.test_obj_sub.basic_add2_kw('a string', 100, 'another string', arg6=True) |
| 346 |
|
| 347 |
def test_docval_add2_kw_kwsyntax_sub_nonetype_arg(self): |
| 348 |
"""Test that docval catches NoneType when called with a four positional |
| 349 |
arguments and two keyword arguments, where two positional and one keyword |
| 350 |
argument is specified in both the parent and subclass implementations |
| 351 |
""" |
| 352 |
msg = "MyTestSubclass.basic_add2_kw: None is not allowed for 'arg5' (expected 'float', not None)" |
| 353 |
with self.assertRaisesWith(TypeError, msg): |
| 354 |
self.test_obj_sub.basic_add2_kw('a string', 100, 'another string', None, arg6=True) |
| 355 |
|
| 356 |
def test_only_kw_no_args(self): |
| 357 |
"""Test that docval parses arguments when only keyword |
| 358 |
arguments exist, and no arguments are specified |
| 359 |
""" |
| 360 |
kwargs = self.test_obj.basic_only_kw() |
| 361 |
self.assertDictEqual(kwargs, {'arg1': 'a', 'arg2': 1}) |
| 362 |
|
| 363 |
def test_only_kw_arg1_no_arg2(self): |
| 364 |
"""Test that docval parses arguments when only keyword |
| 365 |
arguments exist, and only first argument is specified |
| 366 |
as key-value |
| 367 |
""" |
| 368 |
kwargs = self.test_obj.basic_only_kw(arg1='b') |
| 369 |
self.assertDictEqual(kwargs, {'arg1': 'b', 'arg2': 1}) |
| 370 |
|
| 371 |
def test_only_kw_arg1_pos_no_arg2(self): |
| 372 |
"""Test that docval parses arguments when only keyword |
| 373 |
arguments exist, and only first argument is specified |
| 374 |
as positional argument |
| 375 |
""" |
| 376 |
kwargs = self.test_obj.basic_only_kw('b') |
| 377 |
self.assertDictEqual(kwargs, {'arg1': 'b', 'arg2': 1}) |
| 378 |
|
| 379 |
def test_only_kw_arg2_no_arg1(self): |
| 380 |
"""Test that docval parses arguments when only keyword |
| 381 |
arguments exist, and only second argument is specified |
| 382 |
as key-value |
| 383 |
""" |
| 384 |
kwargs = self.test_obj.basic_only_kw(arg2=2) |
| 385 |
self.assertDictEqual(kwargs, {'arg1': 'a', 'arg2': 2}) |
| 386 |
|
| 387 |
def test_only_kw_arg1_arg2(self): |
| 388 |
"""Test that docval parses arguments when only keyword |
| 389 |
arguments exist, and both arguments are specified |
| 390 |
as key-value |
| 391 |
""" |
| 392 |
kwargs = self.test_obj.basic_only_kw(arg1='b', arg2=2) |
| 393 |
self.assertDictEqual(kwargs, {'arg1': 'b', 'arg2': 2}) |
| 394 |
|
| 395 |
def test_only_kw_arg1_arg2_pos(self): |
| 396 |
"""Test that docval parses arguments when only keyword |
| 397 |
arguments exist, and both arguments are specified |
| 398 |
as positional arguments |
| 399 |
""" |
| 400 |
kwargs = self.test_obj.basic_only_kw('b', 2) |
| 401 |
self.assertDictEqual(kwargs, {'arg1': 'b', 'arg2': 2}) |
| 402 |
|
| 403 |
def test_extra_kwarg(self): |
| 404 |
"""Test that docval parses arguments when only keyword |
| 405 |
arguments exist, and both arguments are specified |
| 406 |
as positional arguments |
| 407 |
""" |
| 408 |
with self.assertRaises(TypeError): |
| 409 |
self.test_obj.basic_add2_kw('a string', 100, bar=1000) |
| 410 |
|
| 411 |
def test_extra_args_pos_only(self): |
| 412 |
"""Test that docval raises an error if too many positional |
| 413 |
arguments are specified |
| 414 |
""" |
| 415 |
msg = ("MyTestClass.basic_add2_kw: Expected at most 3 arguments ['arg1', 'arg2', 'arg3'], got 4: 4 positional " |
| 416 |
"and 0 keyword []") |
| 417 |
with self.assertRaisesWith(TypeError, msg): |
| 418 |
self.test_obj.basic_add2_kw('a string', 100, True, 'extra') |
| 419 |
|
| 420 |
def test_extra_args_pos_kw(self): |
| 421 |
"""Test that docval raises an error if too many positional |
| 422 |
arguments are specified and a keyword arg is specified |
| 423 |
""" |
| 424 |
msg = ("MyTestClass.basic_add2_kw: Expected at most 3 arguments ['arg1', 'arg2', 'arg3'], got 4: 3 positional " |
| 425 |
"and 1 keyword ['arg3']") |
| 426 |
with self.assertRaisesWith(TypeError, msg): |
| 427 |
self.test_obj.basic_add2_kw('a string', 'extra', 100, arg3=True) |
| 428 |
|
| 429 |
def test_extra_kwargs_pos_kw(self): |
| 430 |
"""Test that docval raises an error if extra keyword |
| 431 |
arguments are specified |
| 432 |
""" |
| 433 |
msg = ("MyTestClass.basic_add2_kw: Expected at most 3 arguments ['arg1', 'arg2', 'arg3'], got 4: 2 positional " |
| 434 |
"and 2 keyword ['arg3', 'extra']") |
| 435 |
with self.assertRaisesWith(TypeError, msg): |
| 436 |
self.test_obj.basic_add2_kw('a string', 100, extra='extra', arg3=True) |
| 437 |
|
| 438 |
def test_extra_args_pos_only_ok(self): |
| 439 |
"""Test that docval raises an error if too many positional |
| 440 |
arguments are specified even if allow_extra is True |
| 441 |
""" |
| 442 |
msg = ("MyTestClass.basic_add2_kw_allow_extra: Expected at most 3 arguments ['arg1', 'arg2', 'arg3'], got " |
| 443 |
"4 positional") |
| 444 |
with self.assertRaisesWith(TypeError, msg): |
| 445 |
self.test_obj.basic_add2_kw_allow_extra('a string', 100, True, 'extra', extra='extra') |
| 446 |
|
| 447 |
def test_extra_args_pos_kw_ok(self): |
| 448 |
"""Test that docval does not raise an error if too many |
| 449 |
keyword arguments are specified and allow_extra is True |
| 450 |
""" |
| 451 |
kwargs = self.test_obj.basic_add2_kw_allow_extra('a string', 100, True, extra='extra') |
| 452 |
self.assertDictEqual(kwargs, {'arg1': 'a string', 'arg2': 100, 'arg3': True, 'extra': 'extra'}) |
| 453 |
|
| 454 |
def test_dup_kw(self): |
| 455 |
"""Test that docval raises an error if a keyword argument |
| 456 |
captures a positional argument before all positional |
| 457 |
arguments have been resolved |
| 458 |
""" |
| 459 |
with self.assertRaisesWith(TypeError, "MyTestClass.basic_add2_kw: got multiple values for argument 'arg1'"): |
| 460 |
self.test_obj.basic_add2_kw('a string', 100, arg1='extra') |
| 461 |
|
| 462 |
def test_extra_args_dup_kw(self): |
| 463 |
"""Test that docval raises an error if a keyword argument |
| 464 |
captures a positional argument before all positional |
| 465 |
arguments have been resolved and allow_extra is True |
| 466 |
""" |
| 467 |
msg = "MyTestClass.basic_add2_kw_allow_extra: got multiple values for argument 'arg1'" |
| 468 |
with self.assertRaisesWith(TypeError, msg): |
| 469 |
self.test_obj.basic_add2_kw_allow_extra('a string', 100, True, arg1='extra') |
| 470 |
|
| 471 |
def test_unsupported_docval_term(self): |
| 472 |
"""Test that docval does not allow setting of arguments |
| 473 |
marked as unsupported |
| 474 |
""" |
| 475 |
msg = "docval for arg1: keys ['unsupported'] are not supported by docval" |
| 476 |
with self.assertRaisesWith(Exception, msg): |
| 477 |
@docval({'name': 'arg1', 'type': 'array_data', 'doc': 'this is a bad shape', 'unsupported': 'hi!'}) |
| 478 |
def method(self, **kwargs): |
| 479 |
pass |
| 480 |
|
| 481 |
def test_catch_dup_names(self): |
| 482 |
"""Test that docval does not allow duplicate argument names |
| 483 |
""" |
| 484 |
@docval({'name': 'arg1', 'type': 'array_data', 'doc': 'this is a bad shape'}, |
| 485 |
{'name': 'arg1', 'type': 'array_data', 'doc': 'this is a bad shape2'}) |
| 486 |
def method(self, **kwargs): |
| 487 |
pass |
| 488 |
msg = "TestDocValidator.test_catch_dup_names.<locals>.method: The following names are duplicated: ['arg1']" |
| 489 |
with self.assertRaisesWith(ValueError, msg): |
| 490 |
method(self, arg1=[1]) |
| 491 |
|
| 492 |
def test_get_docval_all(self): |
| 493 |
"""Test that get_docval returns a tuple of the docval arguments |
| 494 |
""" |
| 495 |
args = get_docval(self.test_obj.basic_add2) |
| 496 |
self.assertTupleEqual(args, ({'name': 'arg1', 'type': str, 'doc': 'argument1 is a str'}, |
| 497 |
{'name': 'arg2', 'type': int, 'doc': 'argument2 is a int'})) |
| 498 |
|
| 499 |
def test_get_docval_one_arg(self): |
| 500 |
"""Test that get_docval returns the matching docval argument |
| 501 |
""" |
| 502 |
arg = get_docval(self.test_obj.basic_add2, 'arg2') |
| 503 |
self.assertTupleEqual(arg, ({'name': 'arg2', 'type': int, 'doc': 'argument2 is a int'},)) |
| 504 |
|
| 505 |
def test_get_docval_two_args(self): |
| 506 |
"""Test that get_docval returns the matching docval arguments in order |
| 507 |
""" |
| 508 |
args = get_docval(self.test_obj.basic_add2, 'arg2', 'arg1') |
| 509 |
self.assertTupleEqual(args, ({'name': 'arg2', 'type': int, 'doc': 'argument2 is a int'}, |
| 510 |
{'name': 'arg1', 'type': str, 'doc': 'argument1 is a str'})) |
| 511 |
|
| 512 |
def test_get_docval_missing_arg(self): |
| 513 |
"""Test that get_docval throws error if the matching docval argument is not found |
| 514 |
""" |
| 515 |
with self.assertRaisesWith(ValueError, "Function basic_add2 does not have docval argument 'arg3'"): |
| 516 |
get_docval(self.test_obj.basic_add2, 'arg3') |
| 517 |
|
| 518 |
def test_get_docval_missing_args(self): |
| 519 |
"""Test that get_docval throws error if the matching docval arguments is not found |
| 520 |
""" |
| 521 |
with self.assertRaisesWith(ValueError, "Function basic_add2 does not have docval argument 'arg3'"): |
| 522 |
get_docval(self.test_obj.basic_add2, 'arg3', 'arg4') |
| 523 |
|
| 524 |
def test_get_docval_missing_arg_of_many_ok(self): |
| 525 |
"""Test that get_docval throws error if the matching docval arguments is not found |
| 526 |
""" |
| 527 |
with self.assertRaisesWith(ValueError, "Function basic_add2 does not have docval argument 'arg3'"): |
| 528 |
get_docval(self.test_obj.basic_add2, 'arg2', 'arg3') |
| 529 |
|
| 530 |
def test_get_docval_none(self): |
| 531 |
"""Test that get_docval returns an empty tuple if there is no docval |
| 532 |
""" |
| 533 |
args = get_docval(self.test_obj.__init__) |
| 534 |
self.assertTupleEqual(args, tuple()) |
| 535 |
|
| 536 |
def test_get_docval_none_arg(self): |
| 537 |
"""Test that get_docval throws error if there is no docval and an argument name is passed |
| 538 |
""" |
| 539 |
with self.assertRaisesWith(ValueError, 'Function __init__ has no docval arguments'): |
| 540 |
get_docval(self.test_obj.__init__, 'arg3') |
| 541 |
|
| 542 |
def test_bool_type(self): |
| 543 |
@docval({'name': 'arg1', 'type': bool, 'doc': 'this is a bool'}) |
| 544 |
def method(self, **kwargs): |
| 545 |
return popargs('arg1', kwargs) |
| 546 |
|
| 547 |
res = method(self, arg1=True) |
| 548 |
self.assertEqual(res, True) |
| 549 |
self.assertIsInstance(res, bool) |
| 550 |
|
| 551 |
res = method(self, arg1=np.bool_(True)) |
| 552 |
self.assertEqual(res, np.bool_(True)) |
| 553 |
self.assertIsInstance(res, np.bool_) |
| 554 |
|
| 555 |
def test_bool_string_type(self): |
| 556 |
@docval({'name': 'arg1', 'type': 'bool', 'doc': 'this is a bool'}) |
| 557 |
def method(self, **kwargs): |
| 558 |
return popargs('arg1', kwargs) |
| 559 |
|
| 560 |
res = method(self, arg1=True) |
| 561 |
self.assertEqual(res, True) |
| 562 |
self.assertIsInstance(res, bool) |
| 563 |
|
| 564 |
res = method(self, arg1=np.bool_(True)) |
| 565 |
self.assertEqual(res, np.bool_(True)) |
| 566 |
self.assertIsInstance(res, np.bool_) |
| 567 |
|
| 568 |
def test_uint_type(self): |
| 569 |
"""Test that docval type specification of np.uint32 works as expected.""" |
| 570 |
@docval({'name': 'arg1', 'type': np.uint32, 'doc': 'this is a uint'}) |
| 571 |
def method(self, **kwargs): |
| 572 |
return popargs('arg1', kwargs) |
| 573 |
|
| 574 |
res = method(self, arg1=np.uint32(1)) |
| 575 |
self.assertEqual(res, np.uint32(1)) |
| 576 |
self.assertIsInstance(res, np.uint32) |
| 577 |
|
| 578 |
msg = ("TestDocValidator.test_uint_type.<locals>.method: incorrect type for 'arg1' (got 'uint8', expected " |
| 579 |
"'uint32')") |
| 580 |
with self.assertRaisesWith(TypeError, msg): |
| 581 |
method(self, arg1=np.uint8(1)) |
| 582 |
|
| 583 |
msg = ("TestDocValidator.test_uint_type.<locals>.method: incorrect type for 'arg1' (got 'uint64', expected " |
| 584 |
"'uint32')") |
| 585 |
with self.assertRaisesWith(TypeError, msg): |
| 586 |
method(self, arg1=np.uint64(1)) |
| 587 |
|
| 588 |
def test_uint_string_type(self): |
| 589 |
"""Test that docval type specification of string 'uint' matches np.uint of all available precisions.""" |
| 590 |
@docval({'name': 'arg1', 'type': 'uint', 'doc': 'this is a uint'}) |
| 591 |
def method(self, **kwargs): |
| 592 |
return popargs('arg1', kwargs) |
| 593 |
|
| 594 |
res = method(self, arg1=np.uint(1)) |
| 595 |
self.assertEqual(res, np.uint(1)) |
| 596 |
self.assertIsInstance(res, np.uint) |
| 597 |
|
| 598 |
res = method(self, arg1=np.uint8(1)) |
| 599 |
self.assertEqual(res, np.uint8(1)) |
| 600 |
self.assertIsInstance(res, np.uint8) |
| 601 |
|
| 602 |
res = method(self, arg1=np.uint16(1)) |
| 603 |
self.assertEqual(res, np.uint16(1)) |
| 604 |
self.assertIsInstance(res, np.uint16) |
| 605 |
|
| 606 |
res = method(self, arg1=np.uint32(1)) |
| 607 |
self.assertEqual(res, np.uint32(1)) |
| 608 |
self.assertIsInstance(res, np.uint32) |
| 609 |
|
| 610 |
res = method(self, arg1=np.uint64(1)) |
| 611 |
self.assertEqual(res, np.uint64(1)) |
| 612 |
self.assertIsInstance(res, np.uint64) |
| 613 |
|
| 614 |
def test_allow_positional_warn(self): |
| 615 |
@docval({'name': 'arg1', 'type': bool, 'doc': 'this is a bool'}, allow_positional=AllowPositional.WARNING) |
| 616 |
def method(self, **kwargs): |
| 617 |
return popargs('arg1', kwargs) |
| 618 |
|
| 619 |
# check that supplying a keyword arg is OK |
| 620 |
res = method(self, arg1=True) |
| 621 |
self.assertEqual(res, True) |
| 622 |
self.assertIsInstance(res, bool) |
| 623 |
|
| 624 |
# check that supplying a positional arg raises a warning |
| 625 |
msg = ('TestDocValidator.test_allow_positional_warn.<locals>.method: ' |
| 626 |
'Using positional arguments for this method is discouraged and will be deprecated in a future major ' |
| 627 |
'release. Please use keyword arguments to ensure future compatibility.') |
| 628 |
with self.assertWarnsWith(FutureWarning, msg): |
| 629 |
method(self, True) |
| 630 |
|
| 631 |
def test_allow_positional_error(self): |
| 632 |
@docval({'name': 'arg1', 'type': bool, 'doc': 'this is a bool'}, allow_positional=AllowPositional.ERROR) |
| 633 |
def method(self, **kwargs): |
| 634 |
return popargs('arg1', kwargs) |
| 635 |
|
| 636 |
# check that supplying a keyword arg is OK |
| 637 |
res = method(self, arg1=True) |
| 638 |
self.assertEqual(res, True) |
| 639 |
self.assertIsInstance(res, bool) |
| 640 |
|
| 641 |
# check that supplying a positional arg raises an error |
| 642 |
msg = ('TestDocValidator.test_allow_positional_error.<locals>.method: ' |
| 643 |
'Only keyword arguments (e.g., func(argname=value, ...)) are allowed for this method.') |
| 644 |
with self.assertRaisesWith(SyntaxError, msg): |
| 645 |
method(self, True) |
| 646 |
|
| 647 |
def test_allow_none_false(self): |
| 648 |
"""Test that docval with allow_none=True and non-None default value works""" |
| 649 |
@docval({'name': 'arg1', 'type': bool, 'doc': 'this is a bool or None with a default', 'default': True, |
| 650 |
'allow_none': False}) |
| 651 |
def method(self, **kwargs): |
| 652 |
return popargs('arg1', kwargs) |
| 653 |
|
| 654 |
# if provided, None is not allowed |
| 655 |
msg = ("TestDocValidator.test_allow_none_false.<locals>.method: incorrect type for 'arg1' " |
| 656 |
"(got 'NoneType', expected 'bool')") |
| 657 |
with self.assertRaisesWith(TypeError, msg): |
| 658 |
res = method(self, arg1=None) |
| 659 |
|
| 660 |
# if not provided, the default value is used |
| 661 |
res = method(self) |
| 662 |
self.assertTrue(res) |
| 663 |
|
| 664 |
def test_allow_none(self): |
| 665 |
"""Test that docval with allow_none=True and non-None default value works""" |
| 666 |
@docval({'name': 'arg1', 'type': bool, 'doc': 'this is a bool or None with a default', 'default': True, |
| 667 |
'allow_none': True}) |
| 668 |
def method(self, **kwargs): |
| 669 |
return popargs('arg1', kwargs) |
| 670 |
|
| 671 |
# if provided, None is allowed |
| 672 |
res = method(self, arg1=None) |
| 673 |
self.assertIsNone(res) |
| 674 |
|
| 675 |
# if not provided, the default value is used |
| 676 |
res = method(self) |
| 677 |
self.assertTrue(res) |
| 678 |
|
| 679 |
def test_allow_none_redundant(self): |
| 680 |
"""Test that docval with allow_none=True and default=None works""" |
| 681 |
@docval({'name': 'arg1', 'type': bool, 'doc': 'this is a bool or None with a default', 'default': None, |
| 682 |
'allow_none': True}) |
| 683 |
def method(self, **kwargs): |
| 684 |
return popargs('arg1', kwargs) |
| 685 |
|
| 686 |
# if provided, None is allowed |
| 687 |
res = method(self, arg1=None) |
| 688 |
self.assertIsNone(res) |
| 689 |
|
| 690 |
# if not provided, the default value is used |
| 691 |
res = method(self) |
| 692 |
self.assertIsNone(res) |
| 693 |
|
| 694 |
def test_allow_none_no_default(self): |
| 695 |
"""Test that docval with allow_none=True and no default raises an error""" |
| 696 |
msg = ("docval for arg1: allow_none=True can only be set if a default value is provided.") |
| 697 |
with self.assertRaisesWith(Exception, msg): |
| 698 |
@docval({'name': 'arg1', 'type': bool, 'doc': 'this is a bool or None with a default', 'allow_none': True}) |
| 699 |
def method(self, **kwargs): |
| 700 |
return popargs('arg1', kwargs) |
| 701 |
|
| 702 |
def test_enum_str(self): |
| 703 |
"""Test that the basic usage of an enum check on strings works""" |
| 704 |
@docval({'name': 'arg1', 'type': str, 'doc': 'an arg', 'enum': ['a', 'b']}) # also use enum: list |
| 705 |
def method(self, **kwargs): |
| 706 |
return popargs('arg1', kwargs) |
| 707 |
|
| 708 |
self.assertEqual(method(self, 'a'), 'a') |
| 709 |
self.assertEqual(method(self, 'b'), 'b') |
| 710 |
|
| 711 |
msg = ("TestDocValidator.test_enum_str.<locals>.method: " |
| 712 |
"forbidden value for 'arg1' (got 'c', expected ['a', 'b'])") |
| 713 |
with self.assertRaisesWith(ValueError, msg): |
| 714 |
method(self, 'c') |
| 715 |
|
| 716 |
def test_enum_int(self): |
| 717 |
"""Test that the basic usage of an enum check on ints works""" |
| 718 |
@docval({'name': 'arg1', 'type': int, 'doc': 'an arg', 'enum': (1, 2)}) |
| 719 |
def method(self, **kwargs): |
| 720 |
return popargs('arg1', kwargs) |
| 721 |
|
| 722 |
self.assertEqual(method(self, 1), 1) |
| 723 |
self.assertEqual(method(self, 2), 2) |
| 724 |
|
| 725 |
msg = ("TestDocValidator.test_enum_int.<locals>.method: " |
| 726 |
"forbidden value for 'arg1' (got 3, expected (1, 2))") |
| 727 |
with self.assertRaisesWith(ValueError, msg): |
| 728 |
method(self, 3) |
| 729 |
|
| 730 |
def test_enum_uint(self): |
| 731 |
"""Test that the basic usage of an enum check on uints works""" |
| 732 |
@docval({'name': 'arg1', 'type': np.uint, 'doc': 'an arg', 'enum': (np.uint(1), np.uint(2))}) |
| 733 |
def method(self, **kwargs): |
| 734 |
return popargs('arg1', kwargs) |
| 735 |
|
| 736 |
self.assertEqual(method(self, np.uint(1)), np.uint(1)) |
| 737 |
self.assertEqual(method(self, np.uint(2)), np.uint(2)) |
| 738 |
|
| 739 |
# the string rep of uint changes from numpy 1 to 2 ("1" to "np.uint64(1)"), so do not hardcode the string |
| 740 |
uint_str1 = np.uint(1).__repr__() |
| 741 |
uint_str2 = np.uint(2).__repr__() |
| 742 |
|
| 743 |
msg = ("TestDocValidator.test_enum_uint.<locals>.method: " |
| 744 |
"forbidden value for 'arg1' (got 3, expected (%s, %s))" % (uint_str1, uint_str2)) |
| 745 |
with self.assertRaisesWith(ValueError, msg): |
| 746 |
method(self, np.uint(3)) |
| 747 |
|
| 748 |
def test_enum_float(self): |
| 749 |
"""Test that the basic usage of an enum check on floats works""" |
| 750 |
@docval({'name': 'arg1', 'type': float, 'doc': 'an arg', 'enum': (3.14, )}) |
| 751 |
def method(self, **kwargs): |
| 752 |
return popargs('arg1', kwargs) |
| 753 |
|
| 754 |
self.assertEqual(method(self, 3.14), 3.14) |
| 755 |
|
| 756 |
msg = ("TestDocValidator.test_enum_float.<locals>.method: " |
| 757 |
"forbidden value for 'arg1' (got 3.0, expected (3.14,))") |
| 758 |
with self.assertRaisesWith(ValueError, msg): |
| 759 |
method(self, 3.) |
| 760 |
|
| 761 |
def test_enum_bool_mixed(self): |
| 762 |
"""Test that the basic usage of an enum check on a tuple of bool, int, float, and string works""" |
| 763 |
@docval({'name': 'arg1', 'type': (bool, int, float, str, np.uint), 'doc': 'an arg', |
| 764 |
'enum': (True, 1, 1.0, 'true', np.uint(1))}) |
| 765 |
def method(self, **kwargs): |
| 766 |
return popargs('arg1', kwargs) |
| 767 |
|
| 768 |
self.assertEqual(method(self, True), True) |
| 769 |
self.assertEqual(method(self, 1), 1) |
| 770 |
self.assertEqual(method(self, 1.0), 1.0) |
| 771 |
self.assertEqual(method(self, 'true'), 'true') |
| 772 |
self.assertEqual(method(self, np.uint(1)), np.uint(1)) |
| 773 |
|
| 774 |
# the string rep of uint changes from numpy 1 to 2 ("1" to "np.uint64(1)"), so do not hardcode the string |
| 775 |
uint_str = np.uint(1).__repr__() |
| 776 |
|
| 777 |
msg = ("TestDocValidator.test_enum_bool_mixed.<locals>.method: " |
| 778 |
"forbidden value for 'arg1' (got 0, expected (True, 1, 1.0, 'true', %s))" % uint_str) |
| 779 |
with self.assertRaisesWith(ValueError, msg): |
| 780 |
method(self, 0) |
| 781 |
|
| 782 |
def test_enum_bad_type(self): |
| 783 |
"""Test that docval with an enum check where the arg type includes an invalid enum type fails""" |
| 784 |
msg = ("docval for arg1: enum checking cannot be used with arg type (<class 'bool'>, <class 'int'>, " |
| 785 |
"<class 'str'>, <class 'numpy.float64'>, <class 'object'>)") |
| 786 |
with self.assertRaisesWith(Exception, msg): |
| 787 |
@docval({'name': 'arg1', 'type': (bool, int, str, np.float64, object), 'doc': 'an arg', 'enum': (1, 2)}) |
| 788 |
def method(self, **kwargs): |
| 789 |
return popargs('arg1', kwargs) |
| 790 |
|
| 791 |
def test_enum_none_type(self): |
| 792 |
"""Test that the basic usage of an enum check on None works""" |
| 793 |
msg = ("docval for arg1: enum checking cannot be used with arg type None") |
| 794 |
with self.assertRaisesWith(Exception, msg): |
| 795 |
@docval({'name': 'arg1', 'type': None, 'doc': 'an arg', 'enum': (True, 1, 'true')}) |
| 796 |
def method(self, **kwargs): |
| 797 |
pass |
| 798 |
|
| 799 |
def test_enum_single_allowed(self): |
| 800 |
"""Test that docval with an enum check on a single value fails""" |
| 801 |
msg = ("docval for arg1: enum value must be a list or tuple (received <class 'str'>)") |
| 802 |
with self.assertRaisesWith(Exception, msg): |
| 803 |
@docval({'name': 'arg1', 'type': str, 'doc': 'an arg', 'enum': 'only one value'}) |
| 804 |
def method(self, **kwargs): |
| 805 |
pass |
| 806 |
|
| 807 |
def test_enum_str_default(self): |
| 808 |
"""Test that docval with an enum check on strings and a default value works""" |
| 809 |
@docval({'name': 'arg1', 'type': str, 'doc': 'an arg', 'default': 'a', 'enum': ['a', 'b']}) |
| 810 |
def method(self, **kwargs): |
| 811 |
return popargs('arg1', kwargs) |
| 812 |
|
| 813 |
self.assertEqual(method(self), 'a') |
| 814 |
|
| 815 |
msg = ("TestDocValidator.test_enum_str_default.<locals>.method: " |
| 816 |
"forbidden value for 'arg1' (got 'c', expected ['a', 'b'])") |
| 817 |
with self.assertRaisesWith(ValueError, msg): |
| 818 |
method(self, 'c') |
| 819 |
|
| 820 |
def test_enum_str_none_default(self): |
| 821 |
"""Test that docval with an enum check on strings and a None default value works""" |
| 822 |
@docval({'name': 'arg1', 'type': str, 'doc': 'an arg', 'default': None, 'enum': ['a', 'b']}) |
| 823 |
def method(self, **kwargs): |
| 824 |
return popargs('arg1', kwargs) |
| 825 |
|
| 826 |
self.assertIsNone(method(self)) |
| 827 |
|
| 828 |
def test_enum_forbidden_values(self): |
| 829 |
"""Test that docval with enum values that include a forbidden type fails""" |
| 830 |
msg = ("docval for arg1: enum values are of types not allowed by arg type " |
| 831 |
"(got [<class 'bool'>, <class 'list'>], expected <class 'bool'>)") |
| 832 |
with self.assertRaisesWith(Exception, msg): |
| 833 |
@docval({'name': 'arg1', 'type': bool, 'doc': 'an arg', 'enum': (True, [])}) |
| 834 |
def method(self, **kwargs): |
| 835 |
pass |
| 836 |
|
| 837 |
def test_nested_return_types(self): |
| 838 |
"""Test that having nested tuple rtype creates valid sphinx references""" |
| 839 |
@docval({'name': 'arg1', 'type': int, 'doc': 'an arg'}, |
| 840 |
returns='output', rtype=(list, (list, bool), (list, 'Test'))) |
| 841 |
def method(self, **kwargs): |
| 842 |
return [] |
| 843 |
|
| 844 |
doc = ('method(arg1)\n\n\n\nArgs:\n arg1 (:py:class:`~int`): an arg\n\nReturns:\n ' |
| 845 |
':py:class:`~list` or :py:class:`~list`, :py:class:`~bool` or :py:class:`~list`, ``Test``: output') |
| 846 |
self.assertEqual(method.__doc__, doc) |
| 847 |
|
| 848 |
|
| 849 |
class TestDocValidatorChain(TestCase): |
| 850 |
|
| 851 |
def setUp(self): |
| 852 |
self.obj1 = MyChainClass('base', [[1, 2], [3, 4], [5, 6]], [[10, 20]]) |
| 853 |
# note that self.obj1.arg3 == [[1, 2], [3, 4], [5, 6]] |
| 854 |
|
| 855 |
def test_type_arg(self): |
| 856 |
"""Test that passing an object for an argument that allows a specific type works""" |
| 857 |
obj2 = MyChainClass(self.obj1, [[10, 20], [30, 40], [50, 60]], [[10, 20]]) |
| 858 |
self.assertEqual(obj2.arg1, 'base') |
| 859 |
|
| 860 |
def test_type_arg_wrong_type(self): |
| 861 |
"""Test that passing an object for an argument that does not match a specific type raises an error""" |
| 862 |
err_msg = "MyChainClass.__init__: incorrect type for 'arg1' (got 'object', expected 'str or MyChainClass')" |
| 863 |
with self.assertRaisesWith(TypeError, err_msg): |
| 864 |
MyChainClass(object(), [[10, 20], [30, 40], [50, 60]], [[10, 20]]) |
| 865 |
|
| 866 |
def test_shape_valid_unpack(self): |
| 867 |
"""Test that passing an object for an argument with required shape tests the shape of object.argument""" |
| 868 |
obj2 = MyChainClass(self.obj1, [[10, 20], [30, 40], [50, 60]], [[10, 20]]) |
| 869 |
obj3 = MyChainClass(self.obj1, obj2, [[100, 200]]) |
| 870 |
self.assertListEqual(obj3.arg3, obj2.arg3) |
| 871 |
|
| 872 |
def test_shape_invalid_unpack(self): |
| 873 |
"""Test that passing an object for an argument with required shape and object.argument has an invalid shape |
| 874 |
raises an error""" |
| 875 |
obj2 = MyChainClass(self.obj1, [[10, 20], [30, 40], [50, 60]], [[10, 20]]) |
| 876 |
# change arg3 of obj2 to fail the required shape - contrived, but could happen because datasets can change |
| 877 |
# shape after an object is initialized |
| 878 |
obj2.arg3 = [10, 20, 30] |
| 879 |
|
| 880 |
err_msg = "MyChainClass.__init__: incorrect shape for 'arg3' (got '(3,)', expected '(None, 2)')" |
| 881 |
with self.assertRaisesWith(ValueError, err_msg): |
| 882 |
MyChainClass(self.obj1, obj2, [[100, 200]]) |
| 883 |
|
| 884 |
def test_shape_none_unpack(self): |
| 885 |
"""Test that passing an object for an argument with required shape and object.argument is None is OK""" |
| 886 |
obj2 = MyChainClass(self.obj1, [[10, 20], [30, 40], [50, 60]], [[10, 20]]) |
| 887 |
obj2.arg3 = None |
| 888 |
obj3 = MyChainClass(self.obj1, obj2, [[100, 200]]) |
| 889 |
self.assertIsNone(obj3.arg3) |
| 890 |
|
| 891 |
def test_shape_other_unpack(self): |
| 892 |
"""Test that passing an object for an argument with required shape and object.argument is an object without |
| 893 |
an argument attribute raises an error""" |
| 894 |
obj2 = MyChainClass(self.obj1, [[10, 20], [30, 40], [50, 60]], [[10, 20]]) |
| 895 |
obj2.arg3 = object() |
| 896 |
|
| 897 |
err_msg = (r"cannot check shape of object '<object object at .*>' for argument 'arg3' " |
| 898 |
r"\(expected shape '\(None, 2\)'\)") |
| 899 |
with self.assertRaisesRegex(ValueError, err_msg): |
| 900 |
MyChainClass(self.obj1, obj2, [[100, 200]]) |
| 901 |
|
| 902 |
def test_shape_valid_unpack_default(self): |
| 903 |
"""Test that passing an object for an argument with required shape and a default value tests the shape of |
| 904 |
object.argument""" |
| 905 |
obj2 = MyChainClass(self.obj1, [[10, 20], [30, 40], [50, 60]], arg4=[[10, 20]]) |
| 906 |
obj3 = MyChainClass(self.obj1, [[100, 200], [300, 400], [500, 600]], arg4=obj2) |
| 907 |
self.assertListEqual(obj3.arg4, obj2.arg4) |
| 908 |
|
| 909 |
def test_shape_invalid_unpack_default(self): |
| 910 |
"""Test that passing an object for an argument with required shape and a default value and object.argument has |
| 911 |
an invalid shape raises an error""" |
| 912 |
obj2 = MyChainClass(self.obj1, [[10, 20], [30, 40], [50, 60]], arg4=[[10, 20]]) |
| 913 |
# change arg3 of obj2 to fail the required shape - contrived, but could happen because datasets can change |
| 914 |
# shape after an object is initialized |
| 915 |
obj2.arg4 = [10, 20, 30] |
| 916 |
|
| 917 |
err_msg = "MyChainClass.__init__: incorrect shape for 'arg4' (got '(3,)', expected '(None, 2)')" |
| 918 |
with self.assertRaisesWith(ValueError, err_msg): |
| 919 |
MyChainClass(self.obj1, [[100, 200], [300, 400], [500, 600]], arg4=obj2) |
| 920 |
|
| 921 |
def test_shape_none_unpack_default(self): |
| 922 |
"""Test that passing an object for an argument with required shape and a default value and object.argument is |
| 923 |
an object without an argument attribute raises an error""" |
| 924 |
obj2 = MyChainClass(self.obj1, [[10, 20], [30, 40], [50, 60]], arg4=[[10, 20]]) |
| 925 |
# change arg3 of obj2 to fail the required shape - contrived, but could happen because datasets can change |
| 926 |
# shape after an object is initialized |
| 927 |
obj2.arg4 = None |
| 928 |
obj3 = MyChainClass(self.obj1, [[100, 200], [300, 400], [500, 600]], arg4=obj2) |
| 929 |
self.assertIsNone(obj3.arg4) |
| 930 |
|
| 931 |
def test_shape_other_unpack_default(self): |
| 932 |
"""Test that passing an object for an argument with required shape and a default value and object.argument is |
| 933 |
None is OK""" |
| 934 |
obj2 = MyChainClass(self.obj1, [[10, 20], [30, 40], [50, 60]], arg4=[[10, 20]]) |
| 935 |
# change arg3 of obj2 to fail the required shape - contrived, but could happen because datasets can change |
| 936 |
# shape after an object is initialized |
| 937 |
obj2.arg4 = object() |
| 938 |
|
| 939 |
err_msg = (r"cannot check shape of object '<object object at .*>' for argument 'arg4' " |
| 940 |
r"\(expected shape '\(None, 2\)'\)") |
| 941 |
with self.assertRaisesRegex(ValueError, err_msg): |
| 942 |
MyChainClass(self.obj1, [[100, 200], [300, 400], [500, 600]], arg4=obj2) |
| 943 |
|
| 944 |
|
| 945 |
class TestGetargs(TestCase): |
| 946 |
"""Test the getargs function and its error conditions.""" |
| 947 |
|
| 948 |
def test_one_arg_first(self): |
| 949 |
kwargs = {'a': 1, 'b': None} |
| 950 |
expected_kwargs = kwargs.copy() |
| 951 |
res = getargs('a', kwargs) |
| 952 |
self.assertEqual(res, 1) |
| 953 |
self.assertDictEqual(kwargs, expected_kwargs) |
| 954 |
|
| 955 |
def test_one_arg_second(self): |
| 956 |
kwargs = {'a': 1, 'b': None} |
| 957 |
expected_kwargs = kwargs.copy() |
| 958 |
res = getargs('b', kwargs) |
| 959 |
self.assertEqual(res, None) |
| 960 |
self.assertDictEqual(kwargs, expected_kwargs) |
| 961 |
|
| 962 |
def test_many_args_get_some(self): |
| 963 |
kwargs = {'a': 1, 'b': None, 'c': 3} |
| 964 |
expected_kwargs = kwargs.copy() |
| 965 |
|
| 966 |
res = getargs('a', 'c', kwargs) |
| 967 |
self.assertListEqual(res, [1, 3]) |
| 968 |
self.assertDictEqual(kwargs, expected_kwargs) |
| 969 |
|
| 970 |
def test_many_args_get_all(self): |
| 971 |
kwargs = {'a': 1, 'b': None, 'c': 3} |
| 972 |
expected_kwargs = kwargs.copy() |
| 973 |
|
| 974 |
res = getargs('a', 'b', 'c', kwargs) |
| 975 |
self.assertListEqual(res, [1, None, 3]) |
| 976 |
self.assertDictEqual(kwargs, expected_kwargs) |
| 977 |
|
| 978 |
def test_many_args_reverse(self): |
| 979 |
kwargs = {'a': 1, 'b': None, 'c': 3} |
| 980 |
expected_kwargs = kwargs.copy() |
| 981 |
res = getargs('c', 'b', 'a', kwargs) |
| 982 |
self.assertListEqual(res, [3, None, 1]) |
| 983 |
self.assertDictEqual(kwargs, expected_kwargs) |
| 984 |
|
| 985 |
def test_many_args_unpack(self): |
| 986 |
kwargs = {'a': 1, 'b': None, 'c': 3} |
| 987 |
expected_kwargs = kwargs.copy() |
| 988 |
res1, res2, res3 = getargs('a', 'b', 'c', kwargs) |
| 989 |
self.assertEqual(res1, 1) |
| 990 |
self.assertEqual(res2, None) |
| 991 |
self.assertEqual(res3, 3) |
| 992 |
self.assertDictEqual(kwargs, expected_kwargs) |
| 993 |
|
| 994 |
def test_too_few_args(self): |
| 995 |
kwargs = {'a': 1, 'b': None} |
| 996 |
msg = 'Must supply at least one key and a dict' |
| 997 |
with self.assertRaisesWith(ValueError, msg): |
| 998 |
getargs(kwargs) |
| 999 |
|
| 1000 |
def test_last_arg_not_dict(self): |
| 1001 |
kwargs = {'a': 1, 'b': None} |
| 1002 |
msg = 'Last argument must be a dict' |
| 1003 |
with self.assertRaisesWith(ValueError, msg): |
| 1004 |
getargs(kwargs, 'a') |
| 1005 |
|
| 1006 |
def test_arg_not_found_one_arg(self): |
| 1007 |
kwargs = {'a': 1, 'b': None} |
| 1008 |
msg = "Argument not found in dict: 'c'" |
| 1009 |
with self.assertRaisesWith(ValueError, msg): |
| 1010 |
getargs('c', kwargs) |
| 1011 |
|
| 1012 |
def test_arg_not_found_many_args(self): |
| 1013 |
kwargs = {'a': 1, 'b': None} |
| 1014 |
msg = "Argument not found in dict: 'c'" |
| 1015 |
with self.assertRaisesWith(ValueError, msg): |
| 1016 |
getargs('a', 'c', kwargs) |
| 1017 |
|
| 1018 |
|
| 1019 |
class TestPopargs(TestCase): |
| 1020 |
"""Test the popargs function and its error conditions.""" |
| 1021 |
|
| 1022 |
def test_one_arg_first(self): |
| 1023 |
kwargs = {'a': 1, 'b': None} |
| 1024 |
res = popargs('a', kwargs) |
| 1025 |
self.assertEqual(res, 1) |
| 1026 |
self.assertDictEqual(kwargs, {'b': None}) |
| 1027 |
|
| 1028 |
def test_one_arg_second(self): |
| 1029 |
kwargs = {'a': 1, 'b': None} |
| 1030 |
res = popargs('b', kwargs) |
| 1031 |
self.assertEqual(res, None) |
| 1032 |
self.assertDictEqual(kwargs, {'a': 1}) |
| 1033 |
|
| 1034 |
def test_many_args_pop_some(self): |
| 1035 |
kwargs = {'a': 1, 'b': None, 'c': 3} |
| 1036 |
res = popargs('a', 'c', kwargs) |
| 1037 |
self.assertListEqual(res, [1, 3]) |
| 1038 |
self.assertDictEqual(kwargs, {'b': None}) |
| 1039 |
|
| 1040 |
def test_many_args_pop_all(self): |
| 1041 |
kwargs = {'a': 1, 'b': None, 'c': 3} |
| 1042 |
res = popargs('a', 'b', 'c', kwargs) |
| 1043 |
self.assertListEqual(res, [1, None, 3]) |
| 1044 |
self.assertDictEqual(kwargs, {}) |
| 1045 |
|
| 1046 |
def test_many_args_reverse(self): |
| 1047 |
kwargs = {'a': 1, 'b': None, 'c': 3} |
| 1048 |
res = popargs('c', 'b', 'a', kwargs) |
| 1049 |
self.assertListEqual(res, [3, None, 1]) |
| 1050 |
self.assertDictEqual(kwargs, {}) |
| 1051 |
|
| 1052 |
def test_many_args_unpack(self): |
| 1053 |
kwargs = {'a': 1, 'b': None, 'c': 3} |
| 1054 |
res1, res2, res3 = popargs('a', 'b', 'c', kwargs) |
| 1055 |
self.assertEqual(res1, 1) |
| 1056 |
self.assertEqual(res2, None) |
| 1057 |
self.assertEqual(res3, 3) |
| 1058 |
self.assertDictEqual(kwargs, {}) |
| 1059 |
|
| 1060 |
def test_too_few_args(self): |
| 1061 |
kwargs = {'a': 1, 'b': None} |
| 1062 |
msg = 'Must supply at least one key and a dict' |
| 1063 |
with self.assertRaisesWith(ValueError, msg): |
| 1064 |
popargs(kwargs) |
| 1065 |
|
| 1066 |
def test_last_arg_not_dict(self): |
| 1067 |
kwargs = {'a': 1, 'b': None} |
| 1068 |
msg = 'Last argument must be a dict' |
| 1069 |
with self.assertRaisesWith(ValueError, msg): |
| 1070 |
popargs(kwargs, 'a') |
| 1071 |
|
| 1072 |
def test_arg_not_found_one_arg(self): |
| 1073 |
kwargs = {'a': 1, 'b': None} |
| 1074 |
msg = "Argument not found in dict: 'c'" |
| 1075 |
with self.assertRaisesWith(ValueError, msg): |
| 1076 |
popargs('c', kwargs) |
| 1077 |
|
| 1078 |
def test_arg_not_found_many_args(self): |
| 1079 |
kwargs = {'a': 1, 'b': None} |
| 1080 |
msg = "Argument not found in dict: 'c'" |
| 1081 |
with self.assertRaisesWith(ValueError, msg): |
| 1082 |
popargs('a', 'c', kwargs) |
| 1083 |
|
| 1084 |
|
| 1085 |
class TestPopargsToDict(TestCase): |
| 1086 |
"""Test the popargs_to_dict function and its error conditions.""" |
| 1087 |
|
| 1088 |
def test_one_arg_first(self): |
| 1089 |
kwargs = {'a': 1, 'b': None} |
| 1090 |
res = popargs_to_dict(['a'], kwargs) |
| 1091 |
self.assertDictEqual(res, {'a': 1}) |
| 1092 |
self.assertDictEqual(kwargs, {'b': None}) |
| 1093 |
|
| 1094 |
def test_one_arg_second(self): |
| 1095 |
kwargs = {'a': 1, 'b': None} |
| 1096 |
res = popargs_to_dict(['b'], kwargs) |
| 1097 |
self.assertDictEqual(res, {'b': None}) |
| 1098 |
self.assertDictEqual(kwargs, {'a': 1}) |
| 1099 |
|
| 1100 |
def test_many_args_pop_some(self): |
| 1101 |
kwargs = {'a': 1, 'b': None, 'c': 3} |
| 1102 |
res = popargs_to_dict(['a', 'c'], kwargs) |
| 1103 |
self.assertDictEqual(res, {'a': 1, 'c': 3}) |
| 1104 |
self.assertDictEqual(kwargs, {'b': None}) |
| 1105 |
|
| 1106 |
def test_many_args_pop_all(self): |
| 1107 |
kwargs = {'a': 1, 'b': None, 'c': 3} |
| 1108 |
res = popargs_to_dict(['a', 'b', 'c'], kwargs) |
| 1109 |
self.assertDictEqual(res, {'a': 1, 'b': None, 'c': 3}) |
| 1110 |
self.assertDictEqual(kwargs, {}) |
| 1111 |
|
| 1112 |
def test_arg_not_found_one_arg(self): |
| 1113 |
kwargs = {'a': 1, 'b': None} |
| 1114 |
msg = "Argument not found in dict: 'c'" |
| 1115 |
with self.assertRaisesWith(ValueError, msg): |
| 1116 |
popargs_to_dict(['c'], kwargs) |
| 1117 |
|
| 1118 |
|
| 1119 |
class TestMacro(TestCase): |
| 1120 |
|
| 1121 |
def test_macro(self): |
| 1122 |
self.assertTrue(isinstance(get_docval_macro(), dict)) |
| 1123 |
self.assertSetEqual(set(get_docval_macro().keys()), {'array_data', 'scalar_data', 'data'}) |
| 1124 |
|
| 1125 |
self.assertTupleEqual(get_docval_macro('scalar_data'), (str, int, float, bytes, bool)) |
| 1126 |
|
| 1127 |
@docval_macro('scalar_data') |
| 1128 |
class Dummy1: |
| 1129 |
pass |
| 1130 |
|
| 1131 |
self.assertTupleEqual(get_docval_macro('scalar_data'), (str, int, float, bytes, bool, Dummy1)) |
| 1132 |
|
| 1133 |
@docval_macro('dummy') |
| 1134 |
class Dummy2: |
| 1135 |
pass |
| 1136 |
|
| 1137 |
self.assertTupleEqual(get_docval_macro('dummy'), (Dummy2, )) |
| 1138 |
|
| 1139 |
|
| 1140 |
class TestStringType(TestCase): |
| 1141 |
|
| 1142 |
class Dummy1: |
| 1143 |
pass |
| 1144 |
|
| 1145 |
class Dummy2: |
| 1146 |
pass |
| 1147 |
|
| 1148 |
def test_check_type(self): |
| 1149 |
@docval( |
| 1150 |
{ |
| 1151 |
"name": "arg1", |
| 1152 |
"type": (int, np.ndarray, "Dummy1", "tests.unit.utils_test.test_docval.TestStringType.Dummy2"), |
| 1153 |
"doc": "doc" |
| 1154 |
}, |
| 1155 |
is_method=False, |
| 1156 |
) |
| 1157 |
def myfunc(**kwargs): |
| 1158 |
return kwargs["arg1"] |
| 1159 |
|
| 1160 |
dummy1 = TestStringType.Dummy1() |
| 1161 |
assert dummy1 is myfunc(dummy1) |
| 1162 |
|
| 1163 |
dummy2 = TestStringType.Dummy2() |
| 1164 |
assert dummy2 is myfunc(dummy2) |
| 1165 |
|
| 1166 |
def test_docstring(self): |
| 1167 |
@docval( |
| 1168 |
{ |
| 1169 |
"name": "arg1", |
| 1170 |
"type": (int, np.ndarray, "Dummy1", "tests.unit.utils_test.test_docval.TestStringType.Dummy2"), |
| 1171 |
"doc": "doc" |
| 1172 |
}, |
| 1173 |
is_method=False, |
| 1174 |
) |
| 1175 |
def myfunc(**kwargs): |
| 1176 |
return kwargs["arg1"] |
| 1177 |
|
| 1178 |
expected = """myfunc(arg1) |
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
Args: |
| 1183 |
arg1 (:py:class:`~int` or :py:class:`~numpy.ndarray` or ``Dummy1`` or :py:class:`~tests.unit.utils_test.test_docval.TestStringType.Dummy2`): doc |
| 1184 |
""" # noqa: E501 |
| 1185 |
assert myfunc.__doc__ == expected |