Skip to content

Add default names to PPathNodes. #486

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 1 commit into from
Nov 9, 2023
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
1 change: 1 addition & 0 deletions docs/source/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/_pytask/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/_pytask/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ class PathNode(PPathNode):

"""

name: str
path: Path
name: str = ""

@property
def signature(self) -> str:
Expand Down Expand Up @@ -298,8 +298,8 @@ class PickleNode:

"""

name: str
path: Path
name: str = ""

@property
def signature(self) -> str:
Expand Down
33 changes: 29 additions & 4 deletions tests/test_collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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"