Type stubs for Celery and its related packages

0.26.0 · active · verified Fri Apr 10

Celery-types provides PEP 561 compliant type stubs for Celery and its core dependencies, including `kombu`, `amqp`, `billiard`, `vine`, and `django-celery-results`. This library enhances type checking for Celery applications, allowing tools like MyPy to verify correct usage of Celery APIs. It is actively maintained with regular updates to support new Python and Celery versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic Celery application with a type-hinted task. With `celery-types` installed, your type checker will validate the arguments and return types of Celery tasks and related objects like `AsyncResult`.

from celery import Celery, Task
from typing import Any, Dict

# Configure Celery app (replace with your broker/backend)
app = Celery(
    'my_app',
    broker=os.environ.get('CELERY_BROKER_URL', 'redis://localhost:6379/0'),
    backend=os.environ.get('CELERY_RESULT_BACKEND', 'redis://localhost:6379/0')
)

# Define a type-hinted task
@app.task
def add(x: int, y: int) -> int:
    return x + y

# Example of calling the task with type hints
if __name__ == '__main__':
    # The type checker (e.g., MyPy) will use celery-types to validate arguments
    result: AsyncResult[int] = add.delay(10, 20)
    print(f"Task ID: {result.id}")
    print(f"Result: {result.get()}")

    # Incorrect usage (type checker would flag this if celery-types is installed)
    # add.delay(10, "20") # Mypy would warn about this

view raw JSON →