Skip to content
Draft
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
29 changes: 29 additions & 0 deletions invoke/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@

T = TypeVar("T", bound=Callable)

class PreTaskDesc:
def __get__(self, obj, _type):
return obj._pre

def __set__(self, obj, value):
for p in value:
p._is_pre_of = obj
obj._pre = value

class Task(Generic[T]):
"""
Expand All @@ -48,6 +56,7 @@ class Task(Generic[T]):

.. versionadded:: 1.0
"""
pre = PreTaskDesc()

# TODO: store these kwarg defaults central, refer to those values both here
# and in @task.
Expand Down Expand Up @@ -395,6 +404,11 @@ def __init__(
Keyword arguments to call with, if any. Default: ``None``.
"""
self.task = task
if hasattr(task, "_is_pre_of"):
self.make_context = _copy_attrs_to_return_val(
task,
"_is_pre_of"
)(self.make_context)
self.called_as = called_as
self.args = args or tuple()
self.kwargs = kwargs or dict()
Expand Down Expand Up @@ -517,3 +531,18 @@ def clean_build(c):
.. versionadded:: 1.0
"""
return Call(task, args=args, kwargs=kwargs)

def _copy_attrs_to_return_val(source, *attrs):
"""
Copy attributes from a source to the return value of the decorated func
"""
def _wrapper(func):
def _inner(*args, **kwargs):
target = func(*args, **kwargs)
for name in attrs:
value = getattr(source, name)
if value:
setattr(target, name, value)
return target
return _inner
return _wrapper
27 changes: 27 additions & 0 deletions tests/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,33 @@

assert func.pre == [whatever]

def task_and_pre_tasks_binding(self):

@task
def pre_task(c):
pass

Check warning on line 99 in tests/task.py

View check run for this annotation

Codecov / codecov/patch

tests/task.py#L99

Added line #L99 was not covered by tests

@task(pre=[pre_task])
def my_task(c):
pass

Check warning on line 103 in tests/task.py

View check run for this annotation

Codecov / codecov/patch

tests/task.py#L103

Added line #L103 was not covered by tests

assert all([hasattr(p, "_is_pre_of") for p in my_task.pre])
assert pre_task._is_pre_of == my_task

def create_call_and_context_form_pre_task_has_access_to_parent_task(self):

@task
def pre_task(c):
pass

Check warning on line 112 in tests/task.py

View check run for this annotation

Codecov / codecov/patch

tests/task.py#L112

Added line #L112 was not covered by tests

@task(pre=[pre_task])
def my_task(c):
pass

Check warning on line 116 in tests/task.py

View check run for this annotation

Codecov / codecov/patch

tests/task.py#L116

Added line #L116 was not covered by tests

call = Call(pre_task)
c = call.make_context(Config(defaults={}))
assert getattr(c, "_is_pre_of") == my_task

def allows_star_args_as_shortcut_for_pre(self):
@task
def pre1(c):
Expand Down