Skip to content

Raise informative error when path nodes point to directories. #484

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 11 commits 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
2 changes: 2 additions & 0 deletions docs/source/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
## 0.4.3 - 2023-11-xx

- {pull}`483` simplifies the teardown of a task.
- {pull}`484` raises more informative error when directories instead of files are used
with path nodes.
- {pull}`485` adds missing steps to unconfigure pytask after the job is done which
caused flaky tests.

Expand Down
11 changes: 11 additions & 0 deletions src/_pytask/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ def pytask_collect_task(
"""


_TEMPLATE_ERROR_DIRECTORY: str = """\
The path '{path}' points to a directory, although only files are allowed."""


@hookimpl(trylast=True)
def pytask_collect_node(session: Session, path: Path, node_info: NodeInfo) -> PNode:
"""Collect a node of a task as a :class:`pytask.PNode`.
Expand Down Expand Up @@ -348,6 +352,9 @@ def pytask_collect_node(session: Session, path: Path, node_info: NodeInfo) -> PN
node.path, session.config["paths"] or (session.config["root"],)
)

if isinstance(node, PPathNode) and node.path.is_dir():
raise ValueError(_TEMPLATE_ERROR_DIRECTORY.format(path=node.path))

if isinstance(node, PNode):
return node

Expand All @@ -362,6 +369,10 @@ def pytask_collect_node(session: Session, path: Path, node_info: NodeInfo) -> PN
node, session.config["check_casing_of_paths"]
)
name = shorten_path(node, session.config["paths"] or (session.config["root"],))

if isinstance(node, Path) and node.is_dir():
raise ValueError(_TEMPLATE_ERROR_DIRECTORY.format(path=path))

return PathNode(name=name, path=node)

node_name = create_name_of_python_node(node_info)
Expand Down
2 changes: 1 addition & 1 deletion src/_pytask/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def pytask_execute_task_setup(session: Session, task: PTask) -> None:
for dependency in session.dag.predecessors(task.signature):
node = session.dag.nodes[dependency]["node"]
if not node.state():
msg = f"{task.name} requires missing node {node.name}."
msg = f"{task.name!r} requires missing node {node.name!r}."
if IS_FILE_SYSTEM_CASE_SENSITIVE:
msg += (
"\n\n(Hint: Your file-system is case-sensitive. Check the paths' "
Expand Down
44 changes: 44 additions & 0 deletions tests/test_collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,47 @@ def task_example(path: Annotated[Path, Path("file.txt"), Product]) -> None: ...
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.COLLECTION_FAILED
assert "is defined twice" in result.output


@pytest.mark.parametrize(
"node",
[
"Path(__file__).parent",
"PathNode(name='path', path=Path(__file__).parent)",
"PickleNode(name='', path=Path(__file__).parent)",
],
)
def test_error_when_path_dependency_is_directory(runner, tmp_path, node):
source = f"""
from pathlib import Path
from pytask import PickleNode, PathNode

def task_example(path = {node}): ...
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
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",
"PathNode(name='path', path=Path(__file__).parent)",
"PickleNode(name='', path=Path(__file__).parent)",
],
)
def test_error_when_path_product_is_directory(runner, 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(path: Annotated[Any, Product] = {node}): ...
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
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"))
6 changes: 3 additions & 3 deletions tests/test_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ def task_d(produces):
result = runner.invoke(cli, [tmp_path.as_posix()])

assert result.exit_code == ExitCode.FAILED
assert "NodeNotFoundError: task_d.py::task_d requires" in result.output
assert "NodeNotFoundError: 'task_d.py::task_d' requires" in result.output


@pytest.mark.end_to_end()
Expand All @@ -1024,7 +1024,7 @@ def task_e(in1_: Annotated[Path, node1], in2_: Annotated[Any, node2]): ...
result = runner.invoke(cli, [tmp_path.as_posix()])

assert result.exit_code == ExitCode.FAILED
assert "NodeNotFoundError: task_e.py::task_e requires" in result.output
assert "NodeNotFoundError: 'task_e.py::task_e' requires" in result.output
assert "input1" in result.output


Expand All @@ -1045,7 +1045,7 @@ def task_d(produces):
result = runner.invoke(cli, [tmp_path.joinpath("src").as_posix()])

assert result.exit_code == ExitCode.FAILED
assert "NodeNotFoundError: task_d.py::task_d requires" in result.output
assert "NodeNotFoundError: 'task_d.py::task_d' requires" in result.output
assert "bld/in.txt" in result.output


Expand Down