Skip to content

Commit 9e4658e

Browse files
committed
Fix dependency collection and add test.
1 parent fd63a48 commit 9e4658e

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/_pytask/collect_utils.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,13 @@ def _merge_dictionaries(list_of_dicts: list[dict[Any, Any]]) -> dict[Any, Any]:
216216
return out
217217

218218

219-
_ERROR_MULTIPLE_DEPENDENCY_DEFINITIONS = """"Dependencies are defined via \
220-
'@pytask.mark.depends_on' and as default arguments for the argument 'depends_on'. Use \
221-
only one way and not both.
219+
_ERROR_MULTIPLE_DEPENDENCY_DEFINITIONS = """The task uses multiple ways to define \
220+
dependencies. Dependencies should be defined with either
221+
222+
- '@pytask.mark.depends_on'
223+
- as default arguments for the argument 'depends_on'
224+
225+
Use only one of the two ways.
222226
223227
Hint: You do not need to use 'depends_on' since pytask v0.4. Every function argument \
224228
that is not a product is treated as a dependency. Read more about dependencies in the \
@@ -232,17 +236,18 @@ def parse_dependencies_from_task_function( # noqa: C901
232236
"""Parse dependencies from task function."""
233237
has_depends_on_decorator = False
234238
has_depends_on_argument = False
239+
dependencies = {}
235240

236241
if has_mark(obj, "depends_on"):
242+
has_depends_on_decorator = True
237243
nodes = parse_nodes(session, path, name, obj, depends_on)
238-
return {"depends_on": nodes}
244+
dependencies["depends_on"] = nodes
239245

240246
task_kwargs = obj.pytask_meta.kwargs if hasattr(obj, "pytask_meta") else {}
241247
signature_defaults = parse_keyword_arguments_from_signature_defaults(obj)
242248
kwargs = {**signature_defaults, **task_kwargs}
243249
kwargs.pop("produces", None)
244250

245-
dependencies = {}
246251
# Parse products from task decorated with @task and that uses produces.
247252
if "depends_on" in kwargs:
248253
has_depends_on_argument = True
@@ -254,7 +259,7 @@ def parse_dependencies_from_task_function( # noqa: C901
254259
)
255260

256261
if has_depends_on_decorator and has_depends_on_argument:
257-
raise NodeNotCollectedError(_ERROR_MULTIPLE_PRODUCT_DEFINITIONS)
262+
raise NodeNotCollectedError(_ERROR_MULTIPLE_DEPENDENCY_DEFINITIONS)
258263

259264
parameters_with_product_annot = _find_args_with_product_annotation(obj)
260265
parameters_with_node_annot = _find_args_with_node_annotation(obj)

tests/test_collect.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,30 @@ def task_example(
362362
report = session.collection_reports[0]
363363
assert report.outcome == CollectionOutcome.FAIL
364364
assert "The task uses multiple ways" in str(report.exc_info[1])
365+
366+
367+
@pytest.mark.end_to_end()
368+
def test_depends_on_cannot_mix_different_definitions(tmp_path):
369+
source = """
370+
import pytask
371+
from typing_extensions import Annotated
372+
from pytask import Product
373+
from pathlib import Path
374+
375+
@pytask.mark.depends_on("input_1.txt")
376+
def task_example(
377+
depends_on: Path = "input_2.txt",
378+
path: Annotated[Path, Product] = Path("out.txt")
379+
):
380+
...
381+
"""
382+
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
383+
tmp_path.joinpath("input_1.txt").touch()
384+
tmp_path.joinpath("input_2.txt").touch()
385+
session = main({"paths": tmp_path})
386+
387+
assert session.exit_code == ExitCode.COLLECTION_FAILED
388+
assert len(session.tasks) == 0
389+
report = session.collection_reports[0]
390+
assert report.outcome == CollectionOutcome.FAIL
391+
assert "The task uses multiple" in str(report.exc_info[1])

0 commit comments

Comments
 (0)