Skip to content
Open
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
7 changes: 5 additions & 2 deletions invoke/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
from inspect import Signature
from .config import Config

T = TypeVar("T", bound=Callable)
T = TypeVar("T", bound=Callable, covariant=True)
R = TypeVar("R")


class Task(Generic[T]):
Expand Down Expand Up @@ -129,7 +130,9 @@ def __hash__(self) -> int:
# this for now.
return hash(self.name) + hash(self.body)

def __call__(self, *args: Any, **kwargs: Any) -> T:
def __call__(
self: "Task[Callable[..., R]]", *args: Any, **kwargs: Any
) -> R:
# Guard against calling tasks with no context.
if not isinstance(args[0], Context):
err = "Task expected a Context as its first arg, got {} instead!"
Expand Down
4 changes: 4 additions & 0 deletions sites/www/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Changelog
=========

- :bug:`1073` Correct the return type of `~invoke.tasks.Task.__call__` to
describe the wrapped body's result, instead of the body callable itself.
Existing ``Task[Callable[...]]`` annotations remain supported. Thanks to
``@saitakarcesme`` for the patch.
- :release:`2.2.1 <2025-10-10>`
- :release:`2.1.4 <2025-10-10>`
- :bug:`1038` (fixed in :issue:`1040`) Python 3.14 tweaked the behavior of
Expand Down
52 changes: 52 additions & 0 deletions tests/_support/task_return_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from typing import Callable, Optional

from invoke import Context, Task


def number(context: Context) -> int:
return 3


def text(context: Context) -> str:
return "value"


def nothing(context: Context) -> None:
pass


def optional(context: Context) -> Optional[int]:
return None


number_task: Task[Callable[[Context], int]] = Task(number)
text_task: Task[Callable[[Context], str]] = Task(text)
nothing_task: Task[Callable[[Context], None]] = Task(nothing)
optional_task: Task[Callable[[Context], Optional[int]]] = Task(optional)

number_result: int = number_task(Context())
text_result: str = text_task(Context())
nothing_result: None = nothing_task(Context())
optional_result: Optional[int] = optional_task(Context())

# These must remain errors: returning Any would hide incorrect result usage.
wrong_number: str = number_task(Context()) # type: ignore[assignment]
wrong_text: int = text_task(Context()) # type: ignore[assignment]
wrong_optional: int = optional_task(Context()) # type: ignore[assignment]


class Body:
def __call__(self, context: Context) -> str:
return "callable object"


def callback(context: Context) -> Callable[[], int]:
return lambda: 7


object_task: Task[Body] = Task(Body())
callback_task: Task[Callable[[Context], Callable[[], int]]] = Task(callback)
object_result: str = object_task(Context())
callback_result: Callable[[], int] = callback_task(Context())
wrong_object: int = object_task(Context()) # type: ignore[assignment]
wrong_callback: int = callback_task(Context()) # type: ignore[assignment]
16 changes: 16 additions & 0 deletions tests/task_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from pathlib import Path

from mypy import api


class TaskReturnTypes:
def returns_the_body_result_type(self):
source = Path(__file__).parent / "_support" / "task_return_types.py"
stdout, stderr, status = api.run(
[
"--follow-imports=silent",
"--warn-unused-ignores",
str(source),
]
)
assert status == 0, stdout + stderr