Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better shape error message #787

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/hdmf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ def __fmt_str_quotes(x):
return str(x)


def __shape_error_message(argname, valshape, allowable_shapes):
allowable_shapes_str = str(allowable_shapes).replace("None", ":")
return f"incorrect shape for {argname}: got {valshape}, and expected {allowable_shapes_str}"


def __parse_args(validator, args, kwargs, enforce_type=True, enforce_shape=True, allow_extra=False, # noqa: C901
allow_positional=AllowPositional.ALLOWED):
"""
Expand Down Expand Up @@ -300,8 +305,7 @@ def __parse_args(validator, args, kwargs, enforce_type=True, enforce_shape=True,
argval = getattr(argval, argname)
valshape = get_data_shape(argval)
if valshape is not None and not __shape_okay_multi(argval, arg['shape']):
fmt_val = (argname, valshape, arg['shape'])
value_errors.append("incorrect shape for '%s' (got '%s', expected '%s')" % fmt_val)
value_errors.append(__shape_error_message(argname, valshape, arg['shape']))
if 'enum' in arg:
err = __check_enum(argval, arg)
if err:
Expand Down Expand Up @@ -357,8 +361,7 @@ def __parse_args(validator, args, kwargs, enforce_type=True, enforce_shape=True,
argval = getattr(argval, argname)
valshape = get_data_shape(argval)
if valshape is not None and not __shape_okay_multi(argval, arg['shape']):
fmt_val = (argname, valshape, arg['shape'])
value_errors.append("incorrect shape for '%s' (got '%s', expected '%s')" % fmt_val)
value_errors.append(__shape_error_message(argname, valshape, arg['shape']))
if 'enum' in arg and argval is not None:
err = __check_enum(argval, arg)
if err:
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 @@ -859,7 +859,7 @@ def test_shape_invalid_unpack(self):
# shape after an object is initialized
obj2.arg3 = [10, 20, 30]

err_msg = "MyChainClass.__init__: incorrect shape for 'arg3' (got '(3,)', expected '(None, 2)')"
err_msg = "MyChainClass.__init__: incorrect shape for arg3: got (3,), and expected (:, 2)"
with self.assertRaisesWith(ValueError, err_msg):
MyChainClass(self.obj1, obj2, [[100, 200]])

Expand Down Expand Up @@ -896,7 +896,7 @@ def test_shape_invalid_unpack_default(self):
# shape after an object is initialized
obj2.arg4 = [10, 20, 30]

err_msg = "MyChainClass.__init__: incorrect shape for 'arg4' (got '(3,)', expected '(None, 2)')"
err_msg = "MyChainClass.__init__: incorrect shape for arg4: got (3,), and expected (:, 2)"
with self.assertRaisesWith(ValueError, err_msg):
MyChainClass(self.obj1, [[100, 200], [300, 400], [500, 600]], arg4=obj2)

Expand Down
Loading