diff --git a/docs/changes.rst b/docs/changes.rst index 2aea3dff..598d913c 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -25,6 +25,8 @@ all releases are available on `PyPI `_ and - :gh:`96` implements a spinner to show the progress during the collection. - :gh:`99` enables color support in WSL and fixes ``show_locals`` during collection. - :gh:`101` allows to visualize the project's DAG. +- :gh:`102` adds an example if a parametrization provides not the number of arguments + specified in the signature. 0.0.14 - 2021-03-23 diff --git a/src/_pytask/parametrize.py b/src/_pytask/parametrize.py index 830679f3..2c8d9359 100644 --- a/src/_pytask/parametrize.py +++ b/src/_pytask/parametrize.py @@ -1,6 +1,7 @@ import copy import functools import itertools +import pprint import types from typing import Any from typing import Callable @@ -259,14 +260,22 @@ def _check_if_n_arg_names_matches_n_arg_values( ) -> None: """Check if the number of argument names matches the number of arguments.""" n_names = len(arg_names) - n_values = tuple({len(i) for i in arg_values}) - - if not all(i == n_names for i in n_values): - pretty_arg_values = f"{n_values[0]}" if len(n_values) == 1 else f"in {n_values}" + n_values = [len(i) for i in arg_values] + unique_n_values = tuple(set(n_values)) + + if not all(i == n_names for i in unique_n_values): + pretty_arg_values = ( + f"{unique_n_values[0]}" + if len(unique_n_values) == 1 + else " or ".join(map(str, unique_n_values)) + ) + idx_example = [i == n_names for i in n_values].index(False) + formatted_example = pprint.pformat(arg_values[idx_example]) raise ValueError( - f"Task '{name}' is parametrized with 'arg_names' {arg_names} with " - f"{n_names} elements, but the number of provided 'arg_values' is " - f"{pretty_arg_values}." + f"Task '{name}' is parametrized with {n_names} 'arg_names', {arg_names}, " + f"but the number of provided 'arg_values' is {pretty_arg_values}. For " + f"example, here are the values of parametrization no. {idx_example}:" + f"\n\n{formatted_example}" ) diff --git a/tests/test_parametrize.py b/tests/test_parametrize.py index 1829a7ce..fbe65b0f 100644 --- a/tests/test_parametrize.py +++ b/tests/test_parametrize.py @@ -358,8 +358,10 @@ def task_func(i): [1, 2, 3], [ "ValueError", - "'arg_names' ('i', 'j') with 2", + "with 2 'arg_names', ('i', 'j'),", "'arg_values' is 1.", + "parametrization no. 0:", + "(1,)", ], ), ( @@ -367,8 +369,10 @@ def task_func(i): [(1, 2, 3)], [ "ValueError", - "'arg_names' ('i', 'j') with 2", + "with 2 'arg_names', ('i', 'j'),", "'arg_values' is 3.", + "parametrization no. 0:", + "(1, 2, 3)", ], ), ( @@ -376,8 +380,10 @@ def task_func(i): [(1, 2), (1, 2, 3)], [ "ValueError", - "'arg_names' ('i', 'j') with 2", - "'arg_values' is in (2, 3).", + "with 2 'arg_names', ('i', 'j'),", + "'arg_values' is 2 or 3.", + "parametrization no. 1:", + "(1, 2, 3)", ], ), ],