-
-
Notifications
You must be signed in to change notification settings - Fork 528
Django 6.0: Add Django Tasks framework #2967
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
Open
federicobond
wants to merge
7
commits into
typeddjango:master
Choose a base branch
from
federicobond:tasks-stubs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2cb03af
Add stubs for Django Tasks framework
federicobond 971fd1c
Implement monkeypatch for task framework classes
federicobond 1c88251
Add enqueue methods from tasks backends to stubtest allowlist
federicobond 9500336
Add task decorator to stubtest allowlist
federicobond 4acc512
Add missing dataclass decorator to tasks classes
federicobond 3ab5de5
Update task tests
federicobond 604fa3a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| from django.utils.connection import BaseConnectionHandler | ||
|
|
||
| from .backends.base import BaseTaskBackend | ||
| from .base import DEFAULT_TASK_BACKEND_ALIAS as DEFAULT_TASK_BACKEND_ALIAS | ||
| from .base import DEFAULT_TASK_QUEUE_NAME as DEFAULT_TASK_QUEUE_NAME | ||
| from .base import Task as Task | ||
| from .base import TaskContext as TaskContext | ||
| from .base import TaskResult as TaskResult | ||
| from .base import TaskResultStatus as TaskResultStatus | ||
| from .base import task as task | ||
|
|
||
| __all__ = [ | ||
| "DEFAULT_TASK_BACKEND_ALIAS", | ||
| "DEFAULT_TASK_QUEUE_NAME", | ||
| "Task", | ||
| "TaskContext", | ||
| "TaskResult", | ||
| "TaskResultStatus", | ||
| "default_task_backend", | ||
| "task", | ||
| "task_backends", | ||
| ] | ||
|
|
||
| class TaskBackendHandler(BaseConnectionHandler[BaseTaskBackend]): ... | ||
|
|
||
| task_backends: TaskBackendHandler | ||
| # Actually ConnectionProxy, but quacks exactly like BaseTaskBackend, it's not worth distinguishing the two. | ||
| default_task_backend: BaseTaskBackend |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from abc import ABCMeta | ||
| from typing import Any | ||
|
|
||
| from django.tasks.base import Task, TaskResult | ||
|
|
||
| class BaseTaskBackend(metaclass=ABCMeta): | ||
| task_class: type[Task] | ||
| supports_defer: bool | ||
| supports_async_task: bool | ||
| supports_get_result: bool | ||
| supports_priority: bool | ||
| alias: str | ||
| queues: list[str] | ||
| options: dict[str, Any] | ||
| def __init__(self, alias: str, params: dict[str, Any]) -> None: ... | ||
| def validate_task(self, task: Task) -> None: ... | ||
| def enqueue(self, task: Task, args: list[Any], kwargs: dict[str, Any]) -> TaskResult: ... | ||
| async def aenqueue(self, task: Task, args: list[Any], kwargs: dict[str, Any]) -> TaskResult: ... | ||
| def get_result(self, result_id: str) -> TaskResult: ... | ||
| async def aget_result(self, result_id: str) -> TaskResult: ... | ||
| def check(self, **kwargs: Any) -> list[Any]: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| from typing import Any | ||
|
|
||
| from django.tasks.base import Task, TaskResult | ||
|
|
||
| from .base import BaseTaskBackend | ||
|
|
||
| class DummyBackend(BaseTaskBackend): | ||
| results: list[TaskResult] | ||
| def __init__(self, alias: str, params: dict[str, Any]) -> None: ... | ||
| def enqueue(self, task: Task, args: list[Any], kwargs: dict[str, Any]) -> TaskResult: ... | ||
| def get_result(self, result_id: str) -> TaskResult: ... | ||
| async def aget_result(self, result_id: str) -> TaskResult: ... | ||
| def clear(self) -> None: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| from logging import Logger | ||
| from typing import Any | ||
|
|
||
| from django.tasks.base import Task, TaskResult | ||
|
|
||
| from .base import BaseTaskBackend as BaseTaskBackend | ||
|
|
||
| logger: Logger | ||
|
|
||
| class ImmediateBackend(BaseTaskBackend): | ||
| worker_id: str | ||
| def __init__(self, alias: str, params: dict[str, Any]) -> None: ... | ||
| def enqueue(self, task: Task, args: list[Any], kwargs: dict[str, Any]) -> TaskResult: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
| from datetime import datetime | ||
| from typing import Any, Generic, TypeVar, overload | ||
|
|
||
| from django.db.models.enums import TextChoices as TextChoices | ||
| from django.utils.module_loading import import_string as import_string | ||
| from django.utils.translation import pgettext_lazy as pgettext_lazy | ||
| from typing_extensions import ParamSpec | ||
|
|
||
| from .backends.base import BaseTaskBackend | ||
| from .exceptions import TaskResultMismatch as TaskResultMismatch | ||
|
|
||
| DEFAULT_TASK_BACKEND_ALIAS: str | ||
| DEFAULT_TASK_PRIORITY: int | ||
| DEFAULT_TASK_QUEUE_NAME: str | ||
| TASK_MAX_PRIORITY: int | ||
| TASK_MIN_PRIORITY: int | ||
| TASK_REFRESH_ATTRS: set[str] | ||
|
|
||
| class TaskResultStatus(TextChoices): | ||
| READY = ... | ||
| RUNNING = ... | ||
| FAILED = ... | ||
| SUCCESSFUL = ... | ||
|
|
||
| _P = ParamSpec("_P") | ||
| _R = TypeVar("_R") | ||
|
|
||
| @dataclass(kw_only=True) | ||
| class Task(Generic[_P, _R]): | ||
| priority: int | ||
| func: Callable[_P, _R] | ||
| backend: str | ||
| queue_name: str | ||
| run_after: datetime | None | ||
| takes_context: bool = ... | ||
| def __post_init__(self) -> None: ... | ||
| @property | ||
| def name(self) -> str: ... | ||
| def using( | ||
| self, | ||
| *, | ||
| priority: int | None = None, | ||
| queue_name: str | None = None, | ||
| run_after: datetime | None = None, | ||
| backend: str | None = None, | ||
| ) -> Task[_P, _R]: ... | ||
| def enqueue(self, *args: _P.args, **kwargs: _P.kwargs) -> TaskResult[_P, _R]: ... | ||
| async def aenqueue(self, *args: _P.args, **kwargs: _P.kwargs) -> TaskResult[_P, _R]: ... | ||
| def get_result(self, result_id: str) -> TaskResult[_P, _R]: ... | ||
| async def aget_result(self, result_id: str) -> TaskResult[_P, _R]: ... | ||
| def call(self, *args: _P.args, **kwargs: _P.kwargs) -> _R: ... | ||
| async def acall(self, *args: _P.args, **kwargs: _P.kwargs) -> _R: ... | ||
| def get_backend(self) -> BaseTaskBackend: ... | ||
| @property | ||
| def module_path(self) -> str: ... | ||
|
|
||
| @overload | ||
| def task( | ||
| function: Callable[_P, _R], | ||
| *, | ||
| priority: int | None = None, | ||
| queue_name: str | None = None, | ||
| backend: str | None = None, | ||
| takes_context: bool = ..., | ||
| ) -> Task[_P, _R]: ... | ||
| @overload | ||
| def task( | ||
| *, | ||
| priority: int | None = None, | ||
| queue_name: str | None = None, | ||
| backend: str | None = None, | ||
| takes_context: bool = ..., | ||
| ) -> Callable[[Callable[_P, _R]], Task[_P, _R]]: ... | ||
| @dataclass(kw_only=True) | ||
| class TaskError: | ||
| exception_class_path: str | ||
| traceback: str | ||
| @property | ||
| def exception_class(self) -> type[BaseException]: ... | ||
|
|
||
| @dataclass(kw_only=True) | ||
| class TaskResult(Generic[_P, _R]): | ||
| task: Task[_P, _R] | ||
| id: str | ||
| status: TaskResultStatus | ||
| enqueued_at: datetime | None | ||
| started_at: datetime | None | ||
| finished_at: datetime | None | ||
| last_attempted_at: datetime | None | ||
| args: list[Any] | ||
| kwargs: dict[str, Any] | ||
| backend: str | ||
| errors: list[TaskError] | ||
| worker_ids: list[str] | ||
| def __post_init__(self) -> None: ... | ||
| @property | ||
| def return_value(self) -> _R: ... | ||
| @property | ||
| def is_finished(self) -> bool: ... | ||
| @property | ||
| def attempts(self) -> int: ... | ||
| def refresh(self) -> None: ... | ||
| async def arefresh(self) -> None: ... | ||
|
|
||
| @dataclass(kw_only=True) | ||
| class TaskContext(Generic[_P, _R]): | ||
| task_result: TaskResult[_P, _R] | ||
| @property | ||
| def attempt(self) -> int: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| from django.core.exceptions import ImproperlyConfigured as ImproperlyConfigured | ||
|
|
||
| class TaskException(Exception): ... | ||
| class InvalidTask(TaskException): ... | ||
| class InvalidTaskBackend(ImproperlyConfigured): ... | ||
| class TaskResultDoesNotExist(TaskException): ... | ||
| class TaskResultMismatch(TaskException): ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from logging import Logger | ||
| from typing import Any | ||
|
|
||
| from django.dispatch import Signal as Signal | ||
| from django.tasks.backends.base import BaseTaskBackend | ||
| from django.tasks.base import TaskResult | ||
|
|
||
| from .base import TaskResultStatus as TaskResultStatus | ||
|
|
||
| logger: Logger | ||
|
|
||
| task_enqueued: Signal | ||
| task_finished: Signal | ||
| task_started: Signal | ||
|
|
||
| def clear_tasks_handlers(*, setting: str, **kwargs: Any) -> None: ... | ||
| def log_task_enqueued(sender: type[BaseTaskBackend], task_result: TaskResult, **kwargs: Any) -> None: ... | ||
| def log_task_started(sender: type[BaseTaskBackend], task_result: TaskResult, **kwargs: Any) -> None: ... | ||
| def log_task_finished(sender: type[BaseTaskBackend], task_result: TaskResult, **kwargs: Any) -> None: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| - case: task | ||
| main: | | ||
| from typing_extensions import reveal_type | ||
| from django.tasks import task | ||
|
|
||
| @task | ||
| def test_task(x: int) -> int: | ||
| return x + 1 | ||
|
|
||
| reveal_type(test_task) # N: Revealed type is "django.tasks.base.Task[[x: builtins.int], builtins.int]" | ||
| reveal_type(test_task.call) # N: Revealed type is "def (x: builtins.int) -> builtins.int" | ||
| reveal_type(test_task.enqueue) # N: Revealed type is "def (x: builtins.int) -> django.tasks.base.TaskResult[[x: builtins.int], builtins.int]" | ||
|
|
||
| - case: task_with_priority | ||
| main: | | ||
| from typing_extensions import reveal_type | ||
| from django.tasks import task | ||
|
|
||
| @task(priority=5) | ||
| def test_task(x: int) -> int: | ||
| return x + 1 | ||
|
|
||
| reveal_type(test_task) # N: Revealed type is "django.tasks.base.Task[[x: builtins.int], builtins.int]" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: why are these items in
todo.txt?allowlist.txt(withouttodo) and comment why they are hereUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly because I wasn't aware of how to use each list, so I just threw those items back to where they came from.
django.tasks.default_task_backendcould be moved to allowlist_todo.txt with the other ConnectionProxy instances, like django.core.cache.cache and django.db.connection.django.tasks.task decoratoremits an error because one of the overloads expects a non-None function value when the decorator accepts None at runtime (for the other overload). Could move it to allowlist.txtdjango.tasks.backends.base.BaseTaskBackend.enqueueshould be typed as an abstract method but not sure if that's going to be problematic since default_task_backend is typed as BaseTaskBackend. Can move it to allowlist.txt but not sure.