Celery Stubs
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
- gotcha This package provides *only* type stubs (`.pyi` files) for static analysis and does not add any runtime functionality or client-side proxy behavior to Celery. It is purely for enhancing type checking capabilities.
- gotcha The `celery-stubs` package is currently in '3 - Alpha' development status. This indicates that its API might not be stable, and future minor versions could introduce breaking changes.
- gotcha For more comprehensive and actively maintained type stubs covering the broader Celery ecosystem (including `amqp`, `kombu`, `billiard`, etc.), consider using the `celery-types` package by `sbdchd` instead. It appears to be more actively developed and widely used.
Install
-
pip install celery-stubs
Imports
- Celery
from celery import Celery
- Task
from celery import Task
Quickstart
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