From 190e0aa7320b0243efe45eaa4c8ac735b699e466 Mon Sep 17 00:00:00 2001 From: bendichter Date: Thu, 10 Nov 2022 13:34:47 -0500 Subject: [PATCH] better shape error message --- src/hdmf/utils.py | 11 +++++++---- tests/unit/utils_test/test_docval.py | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/hdmf/utils.py b/src/hdmf/utils.py index 90b52b706..49b08501c 100644 --- a/src/hdmf/utils.py +++ b/src/hdmf/utils.py @@ -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): """ @@ -294,8 +299,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: @@ -340,8 +344,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: diff --git a/tests/unit/utils_test/test_docval.py b/tests/unit/utils_test/test_docval.py index 6c911e64a..4b1d6e54f 100644 --- a/tests/unit/utils_test/test_docval.py +++ b/tests/unit/utils_test/test_docval.py @@ -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]]) @@ -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)