Skip to content

Add example if parametrized with incorrect number of arg_values." #102

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

Merged
merged 2 commits into from
Jun 13, 2021
Merged
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
2 changes: 2 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ all releases are available on `PyPI <https://pypi.org/project/pytask>`_ 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
Expand Down
23 changes: 16 additions & 7 deletions src/_pytask/parametrize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import copy
import functools
import itertools
import pprint
import types
from typing import Any
from typing import Callable
Expand Down Expand Up @@ -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}"
)


Expand Down
14 changes: 10 additions & 4 deletions tests/test_parametrize.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,26 +358,32 @@ 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,)",
],
),
(
("i", "j"),
[(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)",
],
),
(
("i", "j"),
[(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)",
],
),
],
Expand Down