Skip to content

Fix errors when using Task and TaskWithoutPath in task modules. #498

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 16, 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 @@ -22,6 +22,8 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
remains the same, the consecutive tasks are not executed. It remained from when pytask
relied on timestamps.
- {pull}`497` removes unnecessary code in the collection of tasks.
- {pull}`498` fixes an error when using {class}`~pytask.Task` and
{class}`~pytask.TaskWithoutPath` in task modules.

## 0.4.2 - 2023-11-08

Expand Down
2 changes: 1 addition & 1 deletion src/_pytask/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def pytask_collect_task(
produces=products,
markers=markers,
)
if isinstance(obj, PTask):
if isinstance(obj, PTask) and not inspect.isclass(obj):
return obj
return None

Expand Down
3 changes: 2 additions & 1 deletion src/_pytask/mark_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
from __future__ import annotations

import inspect
from typing import Any
from typing import TYPE_CHECKING

Expand All @@ -18,7 +19,7 @@

def get_all_marks(obj_or_task: Any | PTask) -> list[Mark]:
"""Get all marks from a callable or task."""
if isinstance(obj_or_task, PTask):
if isinstance(obj_or_task, PTask) and not inspect.isclass(obj_or_task):
marks = obj_or_task.markers
else:
obj = obj_or_task
Expand Down
11 changes: 11 additions & 0 deletions tests/test_collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,3 +679,14 @@ def task_mixed(): pass
assert result.exit_code == ExitCode.COLLECTION_FAILED
assert "Could not collect" in result.output
assert "The task cannot have" in result.output


@pytest.mark.end_to_end()
def test_module_can_be_collected(runner, tmp_path):
source = """
from pytask import Task, TaskWithoutPath
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))

result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
40 changes: 40 additions & 0 deletions tests/test_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,3 +1008,43 @@ def func(path):
session = build(tasks=[task])
assert session.exit_code == ExitCode.OK
assert tmp_path.joinpath("out.txt").exists()


def test_collect_task(runner, tmp_path):
source = """
from pytask import Task, PathNode
from pathlib import Path

def func(path): path.touch()

task_create_file = Task(
base_name="task",
function=func,
path=Path(__file__),
produces={"path": PathNode(path=Path(__file__).parent / "out.txt")},
)
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("out.txt").exists()


def test_collect_task_without_path(runner, tmp_path):
source = """
from pytask import TaskWithoutPath, PathNode
from pathlib import Path

def func(path): path.touch()

task_create_file = TaskWithoutPath(
name="task",
function=func,
produces={"path": PathNode(path=Path(__file__).parent / "out.txt")},
)
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))

result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("out.txt").exists()