diff --git a/docs/source/changes.md b/docs/source/changes.md index ecbe2459..3202775e 100644 --- a/docs/source/changes.md +++ b/docs/source/changes.md @@ -12,6 +12,7 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and with path nodes. - {pull}`485` adds missing steps to unconfigure pytask after the job is done which caused flaky tests. +- {pull}`486` adds default names to {class}`~pytask.PPathNode`. ## 0.4.2 - 2023-11-8 diff --git a/src/_pytask/collect.py b/src/_pytask/collect.py index be9d4d3d..719ef174 100644 --- a/src/_pytask/collect.py +++ b/src/_pytask/collect.py @@ -344,7 +344,7 @@ def pytask_collect_node(session: Session, path: Path, node_info: NodeInfo) -> PN node.path, session.config["check_casing_of_paths"] ) - if isinstance(node, PathNode) and ( + if isinstance(node, PPathNode) and ( not node.name or node.name == node.path.as_posix() ): # Shorten name of PathNodes. diff --git a/src/_pytask/nodes.py b/src/_pytask/nodes.py index a296fd47..5897af9d 100644 --- a/src/_pytask/nodes.py +++ b/src/_pytask/nodes.py @@ -157,8 +157,8 @@ class PathNode(PPathNode): """ - name: str path: Path + name: str = "" @property def signature(self) -> str: @@ -298,8 +298,8 @@ class PickleNode: """ - name: str path: Path + name: str = "" @property def signature(self) -> str: diff --git a/tests/test_collect.py b/tests/test_collect.py index c10b493d..7cb78ce5 100644 --- a/tests/test_collect.py +++ b/tests/test_collect.py @@ -578,8 +578,8 @@ def task_example(path: Annotated[Path, Path("file.txt"), Product]) -> None: ... "node", [ "Path(__file__).parent", - "PathNode(name='path', path=Path(__file__).parent)", - "PickleNode(name='', path=Path(__file__).parent)", + "PathNode(path=Path(__file__).parent)", + "PickleNode(path=Path(__file__).parent)", ], ) def test_error_when_path_dependency_is_directory(runner, tmp_path, node): @@ -599,8 +599,8 @@ def task_example(path = {node}): ... "node", [ "Path(__file__).parent", - "PathNode(name='path', path=Path(__file__).parent)", - "PickleNode(name='', path=Path(__file__).parent)", + "PathNode(path=Path(__file__).parent)", + "PickleNode(path=Path(__file__).parent)", ], ) def test_error_when_path_product_is_directory(runner, tmp_path, node): @@ -616,3 +616,28 @@ def task_example(path: Annotated[Any, Product] = {node}): ... result = runner.invoke(cli, [tmp_path.as_posix()]) assert result.exit_code == ExitCode.COLLECTION_FAILED assert all(i in result.output for i in ("only", "files", "are", "allowed")) + + +@pytest.mark.parametrize( + "node", + [ + "Path(__file__).parent / 'file.txt'", + "PathNode(path=Path(__file__).parent / 'file.txt')", + "PickleNode(path=Path(__file__).parent / 'file.txt')", + ], +) +def test_default_name_of_path_nodes(tmp_path, node): + source = f""" + from pathlib import Path + from pytask import PickleNode, Product, PathNode + from typing_extensions import Annotated + from typing import Any + + def task_example() -> Annotated[str, {node}]: + return "Hello, World!" + """ + tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source)) + session = build(paths=tmp_path) + assert session.exit_code == ExitCode.OK + assert tmp_path.joinpath("file.txt").exists() + assert session.tasks[0].produces["return"].name == tmp_path.name + "/file.txt"