Skip to content

Commit 96d7d98

Browse files
committed
Add test.
1 parent 4dd3472 commit 96d7d98

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

src/_pytask/collect.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,30 @@ def _collect_from_tasks(session: Session) -> None:
102102
obj=raw_task,
103103
)
104104

105-
if not callable(raw_task):
106-
msg = "Not a function."
107-
raise ValueError(msg)
105+
if callable(raw_task):
106+
if not hasattr(raw_task, "pytask_meta"):
107+
raw_task = task_decorator()(raw_task) # noqa: PLW2901
108+
109+
try:
110+
path = get_file(raw_task)
111+
except (TypeError, OSError):
112+
path = None
113+
else:
114+
if path.name == "<stdin>":
115+
path = None # pragma: no cover
108116

109-
if not hasattr(raw_task, "pytask_meta"):
110-
raw_task = task_decorator()(raw_task) # noqa: PLW2901
117+
# Detect whether a path is defined in a Jupyter notebook.
118+
if is_jupyter() and "ipykernel" in path.as_posix() and path.suffix == ".py":
119+
path = None # pragma: no cover
111120

112-
path = get_file(raw_task)
113-
if path.name == "<stdin>":
114-
path = None
121+
name = raw_task.pytask_meta.name
115122

116-
# Detect whether a path is defined in a Jupyter notebook.
117-
if is_jupyter() and "ipykernel" in path.as_posix() and path.suffix == ".py":
123+
# When a task is not a PTask and a callable, set arbitrary values and it will
124+
# pass without errors and not collected.
125+
else:
126+
name = ""
118127
path = None
119128

120-
name = raw_task.pytask_meta.name
121-
122129
report = session.hook.pytask_collect_task_protocol(
123130
session=session,
124131
reports=session.collection_reports,

tests/test_execute.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import textwrap
99
from pathlib import Path
1010

11+
import pytask
1112
import pytest
1213
from _pytask.capture import CaptureMethod
1314
from _pytask.exceptions import NodeNotFoundError
@@ -705,6 +706,7 @@ def task_example() -> Annotated[Dict[str, str], nodes]:
705706
assert result.exit_code == ExitCode.OK
706707

707708

709+
@pytest.mark.end_to_end()
708710
def test_execute_tasks_and_pass_values_only_by_python_nodes(runner, tmp_path):
709711
source = """
710712
from _pytask.nodes import PathNode
@@ -763,3 +765,9 @@ def create_file(text: Annotated[int, node_text]) -> Annotated[str, node_file]:
763765
)
764766
assert result.returncode == ExitCode.OK
765767
assert tmp_path.joinpath("file.txt").read_text() == "This is the text."
768+
769+
770+
@pytest.mark.end_to_end()
771+
def test_pass_non_task_to_functional_api_that_are_ignored():
772+
session = pytask.build(tasks=None)
773+
assert len(session.tasks) == 0

0 commit comments

Comments
 (0)