Skip to content

Commit 46169f2

Browse files
committed
Reject annotated generator products
1 parent 72df735 commit 46169f2

3 files changed

Lines changed: 16 additions & 13 deletions

File tree

‎docs/source/how_to_guides/provisional_nodes_and_task_generators.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,6 @@ content to a `.txt` file.
9696

9797
The generated tasks need to be decorated with `@task` to be collected.
9898

99-
Task generators are only used to define other tasks. Their return values are ignored
100-
and cannot be used to define products. Task generators also cannot define a `produces`
101-
function argument. Define products on the generated tasks instead.
99+
Task generators are only used to define other tasks. Their return values are ignored.
100+
Task generators cannot define products with a `produces` function argument or a return
101+
annotation.

‎src/_pytask/collect_utils.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,11 @@ def task_example() -> str:
157157
"""
158158

159159
_ERROR_TASK_GENERATOR_PRODUCTS = """Task generators cannot define products with a
160-
'produces' function argument. Define products on the generated tasks instead.
160+
'produces' function argument.
161+
"""
162+
163+
_ERROR_TASK_GENERATOR_RETURN_ANNOTATION = """Task generators cannot define products
164+
with a return annotation.
161165
"""
162166

163167

@@ -191,6 +195,9 @@ def parse_products_from_task_function( # noqa: C901
191195
raise NodeNotCollectedError(_ERROR_TASK_GENERATOR_PRODUCTS)
192196

193197
if is_generator:
198+
if "return" in parameters_with_node_annot:
199+
raise NodeNotCollectedError(_ERROR_TASK_GENERATOR_RETURN_ANNOTATION)
200+
194201
parameters_with_product_annot = [
195202
name for name in parameters_with_product_annot if name != "return"
196203
]

‎tests/test_provisional.py‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def task_copy(
197197
assert tmp_path.joinpath("b-copy.txt").exists()
198198

199199

200-
def test_task_generator_return_value_is_ignored(tmp_path):
200+
def test_task_generator_return_annotation_is_rejected(runner, tmp_path):
201201
source = """
202202
from pathlib import Path
203203
from typing import Annotated
@@ -215,15 +215,11 @@ def task_child(produces=Path("child.txt")):
215215
"""
216216
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
217217

218-
session = build(paths=tmp_path)
219-
generator = next(
220-
task for task in session.tasks if task.name.endswith("task_generator")
221-
)
218+
result = runner.invoke(cli, [tmp_path.as_posix()])
222219

223-
assert session.exit_code == ExitCode.OK
224-
assert tmp_path.joinpath("child.txt").read_text() == "child"
225-
assert not tmp_path.joinpath("returned.txt").exists()
226-
assert "return" not in generator.produces
220+
assert result.exit_code == ExitCode.COLLECTION_FAILED
221+
assert "cannot define products" in result.output
222+
assert "return annotation" in result.output
227223

228224

229225
def test_task_generator_decorator_return_product_is_ignored(tmp_path):

0 commit comments

Comments
 (0)