Skip to content

Commit 9ebd567

Browse files
authored
Add example if parametrized with incorrect number of arg_values." (#102)
1 parent a0cd7d9 commit 9ebd567

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

docs/changes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ all releases are available on `PyPI <https://pypi.org/project/pytask>`_ and
2525
- :gh:`96` implements a spinner to show the progress during the collection.
2626
- :gh:`99` enables color support in WSL and fixes ``show_locals`` during collection.
2727
- :gh:`101` allows to visualize the project's DAG.
28+
- :gh:`102` adds an example if a parametrization provides not the number of arguments
29+
specified in the signature.
2830

2931

3032
0.0.14 - 2021-03-23

src/_pytask/parametrize.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import copy
22
import functools
33
import itertools
4+
import pprint
45
import types
56
from typing import Any
67
from typing import Callable
@@ -259,14 +260,22 @@ def _check_if_n_arg_names_matches_n_arg_values(
259260
) -> None:
260261
"""Check if the number of argument names matches the number of arguments."""
261262
n_names = len(arg_names)
262-
n_values = tuple({len(i) for i in arg_values})
263-
264-
if not all(i == n_names for i in n_values):
265-
pretty_arg_values = f"{n_values[0]}" if len(n_values) == 1 else f"in {n_values}"
263+
n_values = [len(i) for i in arg_values]
264+
unique_n_values = tuple(set(n_values))
265+
266+
if not all(i == n_names for i in unique_n_values):
267+
pretty_arg_values = (
268+
f"{unique_n_values[0]}"
269+
if len(unique_n_values) == 1
270+
else " or ".join(map(str, unique_n_values))
271+
)
272+
idx_example = [i == n_names for i in n_values].index(False)
273+
formatted_example = pprint.pformat(arg_values[idx_example])
266274
raise ValueError(
267-
f"Task '{name}' is parametrized with 'arg_names' {arg_names} with "
268-
f"{n_names} elements, but the number of provided 'arg_values' is "
269-
f"{pretty_arg_values}."
275+
f"Task '{name}' is parametrized with {n_names} 'arg_names', {arg_names}, "
276+
f"but the number of provided 'arg_values' is {pretty_arg_values}. For "
277+
f"example, here are the values of parametrization no. {idx_example}:"
278+
f"\n\n{formatted_example}"
270279
)
271280

272281

tests/test_parametrize.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,26 +358,32 @@ def task_func(i):
358358
[1, 2, 3],
359359
[
360360
"ValueError",
361-
"'arg_names' ('i', 'j') with 2",
361+
"with 2 'arg_names', ('i', 'j'),",
362362
"'arg_values' is 1.",
363+
"parametrization no. 0:",
364+
"(1,)",
363365
],
364366
),
365367
(
366368
("i", "j"),
367369
[(1, 2, 3)],
368370
[
369371
"ValueError",
370-
"'arg_names' ('i', 'j') with 2",
372+
"with 2 'arg_names', ('i', 'j'),",
371373
"'arg_values' is 3.",
374+
"parametrization no. 0:",
375+
"(1, 2, 3)",
372376
],
373377
),
374378
(
375379
("i", "j"),
376380
[(1, 2), (1, 2, 3)],
377381
[
378382
"ValueError",
379-
"'arg_names' ('i', 'j') with 2",
380-
"'arg_values' is in (2, 3).",
383+
"with 2 'arg_names', ('i', 'j'),",
384+
"'arg_values' is 2 or 3.",
385+
"parametrization no. 1:",
386+
"(1, 2, 3)",
381387
],
382388
),
383389
],

0 commit comments

Comments
 (0)