Skip to content

Commit 46fa675

Browse files
authored
Fix errors when using Task and TaskWithoutPath in task modules. (#498)
1 parent 651d402 commit 46fa675

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

docs/source/changes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
2222
remains the same, the consecutive tasks are not executed. It remained from when pytask
2323
relied on timestamps.
2424
- {pull}`497` removes unnecessary code in the collection of tasks.
25+
- {pull}`498` fixes an error when using {class}`~pytask.Task` and
26+
{class}`~pytask.TaskWithoutPath` in task modules.
2527

2628
## 0.4.2 - 2023-11-08
2729

src/_pytask/collect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def pytask_collect_task(
275275
produces=products,
276276
markers=markers,
277277
)
278-
if isinstance(obj, PTask):
278+
if isinstance(obj, PTask) and not inspect.isclass(obj):
279279
return obj
280280
return None
281281

src/_pytask/mark_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66
from __future__ import annotations
77

8+
import inspect
89
from typing import Any
910
from typing import TYPE_CHECKING
1011

@@ -18,7 +19,7 @@
1819

1920
def get_all_marks(obj_or_task: Any | PTask) -> list[Mark]:
2021
"""Get all marks from a callable or task."""
21-
if isinstance(obj_or_task, PTask):
22+
if isinstance(obj_or_task, PTask) and not inspect.isclass(obj_or_task):
2223
marks = obj_or_task.markers
2324
else:
2425
obj = obj_or_task

tests/test_collect.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,3 +679,14 @@ def task_mixed(): pass
679679
assert result.exit_code == ExitCode.COLLECTION_FAILED
680680
assert "Could not collect" in result.output
681681
assert "The task cannot have" in result.output
682+
683+
684+
@pytest.mark.end_to_end()
685+
def test_module_can_be_collected(runner, tmp_path):
686+
source = """
687+
from pytask import Task, TaskWithoutPath
688+
"""
689+
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
690+
691+
result = runner.invoke(cli, [tmp_path.as_posix()])
692+
assert result.exit_code == ExitCode.OK

tests/test_execute.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,3 +1008,43 @@ def func(path):
10081008
session = build(tasks=[task])
10091009
assert session.exit_code == ExitCode.OK
10101010
assert tmp_path.joinpath("out.txt").exists()
1011+
1012+
1013+
def test_collect_task(runner, tmp_path):
1014+
source = """
1015+
from pytask import Task, PathNode
1016+
from pathlib import Path
1017+
1018+
def func(path): path.touch()
1019+
1020+
task_create_file = Task(
1021+
base_name="task",
1022+
function=func,
1023+
path=Path(__file__),
1024+
produces={"path": PathNode(path=Path(__file__).parent / "out.txt")},
1025+
)
1026+
"""
1027+
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
1028+
result = runner.invoke(cli, [tmp_path.as_posix()])
1029+
assert result.exit_code == ExitCode.OK
1030+
assert tmp_path.joinpath("out.txt").exists()
1031+
1032+
1033+
def test_collect_task_without_path(runner, tmp_path):
1034+
source = """
1035+
from pytask import TaskWithoutPath, PathNode
1036+
from pathlib import Path
1037+
1038+
def func(path): path.touch()
1039+
1040+
task_create_file = TaskWithoutPath(
1041+
name="task",
1042+
function=func,
1043+
produces={"path": PathNode(path=Path(__file__).parent / "out.txt")},
1044+
)
1045+
"""
1046+
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
1047+
1048+
result = runner.invoke(cli, [tmp_path.as_posix()])
1049+
assert result.exit_code == ExitCode.OK
1050+
assert tmp_path.joinpath("out.txt").exists()

0 commit comments

Comments
 (0)