examples/pydantic/tasks.py
raw| 1 | from pydantic import BaseModel |
| 2 | |
| 3 | from celery import Celery |
| 4 | |
| 5 | app = Celery('tasks', broker='amqp://') |
| 6 | |
| 7 | |
| 8 | class ArgModel(BaseModel): |
| 9 | value: int |
| 10 | |
| 11 | |
| 12 | class ReturnModel(BaseModel): |
| 13 | value: str |
| 14 | |
| 15 | |
| 16 | @app.task(pydantic=True) |
| 17 | def x(arg: ArgModel) -> ReturnModel: |
| 18 | # args/kwargs type hinted as Pydantic model will be converted |
| 19 | assert isinstance(arg, ArgModel) |
| 20 | # The returned model will be converted to a dict automatically |
| 21 | return ReturnModel(value=f"example: {arg.value}") |