diff --git a/docs/changes.rst b/docs/changes.rst index 4fb26e8c..61ea9432 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -20,6 +20,7 @@ all releases are available on `PyPI `_ and - :gh:`87` fixes that dirty versions are displayed in the documentation. - :gh:`88` adds a ``profile`` command to show information on tasks like duration and file size of products. +- :gh:`93` fixes the display of parametrized arguments in the console. 0.0.14 - 2021-03-23 diff --git a/src/_pytask/console.py b/src/_pytask/console.py index 26ab2fa2..23a60997 100644 --- a/src/_pytask/console.py +++ b/src/_pytask/console.py @@ -40,3 +40,20 @@ def format_strings_as_flat_tree(strings: List[str], title: str, icon: str) -> st ) return text + + +def escape_squared_brackets(string: str) -> str: + """Escape squared brackets which would be accidentally parsed by rich. + + An example are the ids of parametrized tasks which are suffixed with squared + brackets surrounding string representations of the parametrized arguments. + + Example + ------- + >>> escape_squared_brackets("Hello!") + 'Hello!' + >>> escape_squared_brackets("task_dummy[arg1-arg2]") + 'task_dummy\\\\[arg1-arg2]' + + """ + return string.replace("[", "\\[") diff --git a/src/_pytask/nodes.py b/src/_pytask/nodes.py index 39a6b00d..c87ac475 100644 --- a/src/_pytask/nodes.py +++ b/src/_pytask/nodes.py @@ -14,6 +14,7 @@ from typing import Union import attr +from _pytask.console import escape_squared_brackets from _pytask.exceptions import NodeNotCollectedError from _pytask.exceptions import NodeNotFoundError from _pytask.mark import get_marks_from_obj @@ -389,7 +390,8 @@ def reduce_node_name(node, paths: List[Path]): if isinstance(node, MetaTask): shortened_path = relative_to(node.path, ancestor) - name = create_task_name(shortened_path, node.base_name) + raw_name = create_task_name(shortened_path, node.base_name) + name = escape_squared_brackets(raw_name) elif isinstance(node, MetaNode): name = relative_to(node.path, ancestor).as_posix() else: diff --git a/tests/test_collect_command.py b/tests/test_collect_command.py index 08477c7b..33fcb97c 100644 --- a/tests/test_collect_command.py +++ b/tests/test_collect_command.py @@ -42,6 +42,28 @@ def task_dummy(): assert "out.txt>" in captured +@pytest.mark.end_to_end +def test_collect_parametrized_tasks(runner, tmp_path): + source = """ + import pytask + + @pytask.mark.depends_on("in.txt") + @pytask.mark.parametrize("arg, produces", [(0, "out_0.txt"), (1, "out_1.txt")]) + def task_dummy(arg): + pass + """ + tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(source)) + + result = runner.invoke(cli, ["collect", tmp_path.as_posix()]) + + captured = result.output.replace("\n", "").replace(" ", "").replace("\u2502", "") + assert "" in captured + assert "" in captured + assert "[1-out_1.txt]>" in captured + + @pytest.mark.end_to_end def test_collect_task_with_expressions(runner, tmp_path): source = """