Celery Stubs

0.1.3 · maintenance · verified Wed Apr 15

celery-stubs provides type hints for the popular Celery distributed task queue, allowing static type checkers like MyPy to validate Celery-related code. This package is currently at version 0.1.3, released in February 2023, and exhibits a slow, infrequent release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic Celery application using type hints. With `celery-stubs` installed, a static type checker like MyPy can validate the type annotations within your Celery tasks and interactions with Celery objects, helping catch type-related errors before runtime.

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

# Configure Celery (replace with your actual broker/backend if running)
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')
)

@app.task
def add(x: int, y: int) -> int:
    return x + y

@app.task
def process_data(data: Dict[str, Any]) -> None:
    print(f"Processing: {data['value']}")

# To demonstrate type checking, you would typically run mypy:
# mypy your_script_name.py
# If celery-stubs is installed, mypy will use it to validate types
# related to Celery objects (e.g., app.task decorator, AsyncResult, etc.)

# Example of type-hinted usage (not directly runnable without a Celery worker):
if __name__ == "__main__":
    result = add.delay(1, 2)
    print(f"Task ID: {result.id}")
    # result_value: int = result.get() # This line would be type-checked by mypy

    # Example of incorrect usage that mypy would catch with stubs:
    # bad_result = add.delay('a', 'b') # mypy would flag this as type error

view raw JSON →