Skip to content

Commit cbe79d1

Browse files
authored
Remove depends_on and produces from task function when parsed. (#218)
1 parent 0d7be2d commit cbe79d1

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

docs/source/changes.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@ all releases are available on `PyPI <https://pypi.org/project/pytask>`_ and
77
`Anaconda.org <https://anaconda.org/conda-forge/pytask>`_.
88

99

10+
0.1.9 - 2022-xx-xx
11+
------------------
12+
13+
- :gh:`217` enhances the tutorial on how to set up a project.
14+
- :gh:`218` removes ``depends_on`` and ``produces`` from the task function when parsed.
15+
16+
1017
0.1.8 - 2022-02-07
1118
------------------
1219

1320
- :gh:`210` allows ``__tracebackhide__`` to be a callable which accepts the current
1421
exception as an input. Closes :gh:`145`.
1522
- :gh:`213` improves coverage and reporting.
1623
- :gh:`215` makes the help pages of the CLI prettier.
17-
- :gh:`217` enhances the tutorial on how to set up a project.
1824

1925

2026
0.1.7 - 2022-01-28

src/_pytask/nodes.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import attr
2121
from _pytask.exceptions import NodeNotCollectedError
2222
from _pytask.exceptions import NodeNotFoundError
23-
from _pytask.mark_utils import get_marks_from_obj
23+
from _pytask.mark_utils import remove_markers_from_func
2424
from _pytask.session import Session
2525

2626

@@ -147,7 +147,7 @@ def from_path_name_function_session(
147147
markers = [
148148
marker
149149
for marker in getattr(function, "pytaskmark", [])
150-
if marker.name not in ["depends_on", "produces"]
150+
if marker.name not in ("depends_on", "produces")
151151
]
152152

153153
return cls(
@@ -174,7 +174,7 @@ def _get_kwargs_from_task_for_function(self) -> dict[str, Any]:
174174
"""Process dependencies and products to pass them as kwargs to the function."""
175175
func_arg_names = set(inspect.signature(self.function).parameters)
176176
kwargs = {}
177-
for arg_name in ["depends_on", "produces"]:
177+
for arg_name in ("depends_on", "produces"):
178178
if arg_name in func_arg_names:
179179
attribute = getattr(self, arg_name)
180180
kwargs[arg_name] = (
@@ -280,7 +280,8 @@ def _extract_nodes_from_function_markers(
280280
281281
"""
282282
marker_name = parser.__name__
283-
for marker in get_marks_from_obj(function, marker_name):
283+
_, markers = remove_markers_from_func(function, marker_name)
284+
for marker in markers:
284285
parsed = parser(*marker.args, **marker.kwargs)
285286
yield parsed
286287

src/_pytask/parametrize.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ def pytask_parametrize_task(
132132
partialed_func = functools.partial(func, **kwargs)
133133
wrapped_func = functools.update_wrapper(partialed_func, func)
134134

135+
# Remove markers from wrapped function since they are not used. See
136+
# https://github.com/pytask-dev/pytask/issues/216.
137+
wrapped_func.func.pytaskmark = [] # type: ignore[attr-defined]
138+
135139
name_ = f"{name}[{'-'.join(itertools.chain.from_iterable(names))}]"
136140
names_and_functions.append((name_, wrapped_func))
137141

@@ -395,7 +399,7 @@ def _arg_value_to_id_component(
395399
def pytask_parametrize_kwarg_to_marker(obj: Any, kwargs: dict[str, str]) -> None:
396400
"""Add some parametrized keyword arguments as decorator."""
397401
if callable(obj):
398-
for marker_name in ["depends_on", "produces"]:
402+
for marker_name in ("depends_on", "produces"):
399403
if marker_name in kwargs:
400404
mark.__getattr__(marker_name)(kwargs.pop(marker_name))(obj)
401405

tests/test_parametrize.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,25 @@ def task_func():
408408
assert result.exit_code == ExitCode.COLLECTION_FAILED
409409
for c in content:
410410
assert c in result.output
411+
412+
413+
@pytest.mark.end_to_end
414+
def test_generators_are_removed_from_depends_on_produces(tmp_path):
415+
source = """
416+
from pathlib import Path
417+
import pytask
418+
419+
@pytask.mark.parametrize("produces", [
420+
((x for x in ["out.txt", "out_2.txt"]),),
421+
["in.txt"],
422+
])
423+
def task_example(produces):
424+
produces = {0: produces} if isinstance(produces, Path) else produces
425+
for p in produces.values():
426+
p.write_text("hihi")
427+
"""
428+
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(source))
429+
430+
session = main({"paths": tmp_path})
431+
assert session.exit_code == 0
432+
assert session.tasks[0].function.__wrapped__.pytaskmark == []

0 commit comments

Comments
 (0)