Skip to content

Remove depends_on and produces from task function when parsed. #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/source/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ all releases are available on `PyPI <https://pypi.org/project/pytask>`_ and
`Anaconda.org <https://anaconda.org/conda-forge/pytask>`_.


0.1.9 - 2022-xx-xx
------------------

- :gh:`217` enhances the tutorial on how to set up a project.
- :gh:`218` removes ``depends_on`` and ``produces`` from the task function when parsed.


0.1.8 - 2022-02-07
------------------

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


0.1.7 - 2022-01-28
Expand Down
9 changes: 5 additions & 4 deletions src/_pytask/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import attr
from _pytask.exceptions import NodeNotCollectedError
from _pytask.exceptions import NodeNotFoundError
from _pytask.mark_utils import get_marks_from_obj
from _pytask.mark_utils import remove_markers_from_func
from _pytask.session import Session


Expand Down Expand Up @@ -147,7 +147,7 @@ def from_path_name_function_session(
markers = [
marker
for marker in getattr(function, "pytaskmark", [])
if marker.name not in ["depends_on", "produces"]
if marker.name not in ("depends_on", "produces")
]

return cls(
Expand All @@ -174,7 +174,7 @@ def _get_kwargs_from_task_for_function(self) -> dict[str, Any]:
"""Process dependencies and products to pass them as kwargs to the function."""
func_arg_names = set(inspect.signature(self.function).parameters)
kwargs = {}
for arg_name in ["depends_on", "produces"]:
for arg_name in ("depends_on", "produces"):
if arg_name in func_arg_names:
attribute = getattr(self, arg_name)
kwargs[arg_name] = (
Expand Down Expand Up @@ -280,7 +280,8 @@ def _extract_nodes_from_function_markers(

"""
marker_name = parser.__name__
for marker in get_marks_from_obj(function, marker_name):
_, markers = remove_markers_from_func(function, marker_name)
for marker in markers:
parsed = parser(*marker.args, **marker.kwargs)
yield parsed

Expand Down
6 changes: 5 additions & 1 deletion src/_pytask/parametrize.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ def pytask_parametrize_task(
partialed_func = functools.partial(func, **kwargs)
wrapped_func = functools.update_wrapper(partialed_func, func)

# Remove markers from wrapped function since they are not used. See
# https://github.com/pytask-dev/pytask/issues/216.
wrapped_func.func.pytaskmark = [] # type: ignore[attr-defined]

name_ = f"{name}[{'-'.join(itertools.chain.from_iterable(names))}]"
names_and_functions.append((name_, wrapped_func))

Expand Down Expand Up @@ -395,7 +399,7 @@ def _arg_value_to_id_component(
def pytask_parametrize_kwarg_to_marker(obj: Any, kwargs: dict[str, str]) -> None:
"""Add some parametrized keyword arguments as decorator."""
if callable(obj):
for marker_name in ["depends_on", "produces"]:
for marker_name in ("depends_on", "produces"):
if marker_name in kwargs:
mark.__getattr__(marker_name)(kwargs.pop(marker_name))(obj)

Expand Down
22 changes: 22 additions & 0 deletions tests/test_parametrize.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,25 @@ def task_func():
assert result.exit_code == ExitCode.COLLECTION_FAILED
for c in content:
assert c in result.output


@pytest.mark.end_to_end
def test_generators_are_removed_from_depends_on_produces(tmp_path):
source = """
from pathlib import Path
import pytask

@pytask.mark.parametrize("produces", [
((x for x in ["out.txt", "out_2.txt"]),),
["in.txt"],
])
def task_example(produces):
produces = {0: produces} if isinstance(produces, Path) else produces
for p in produces.values():
p.write_text("hihi")
"""
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(source))

session = main({"paths": tmp_path})
assert session.exit_code == 0
assert session.tasks[0].function.__wrapped__.pytaskmark == []