Skip to content
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

Add support for Pydantic model validation/serialization (fixes #8751) #9023

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mathiasertl
Copy link
Contributor

@mathiasertl mathiasertl commented May 14, 2024

Allow validation of arguments using Pydantic, as well as result serialization. This PR would fix (IMHO) #8751.

With this PR, you can pass pydantic=True to a task decorator. If set, any argument type hinted as a Pydantic model will be validated (note: this means data validation and loading into the model class Pydantic speak) to that model, and any result matching a return type annotation will automatically be converted to a dict (using model_dump()).

With this feature, tasks look a lot more like FastAPI endpoints, where query arguments, request and response bodies are validated/serialized using Pydantic. The advantage is of course automatic data validation and safer typing. mypy will catch type hinting errors, and IDEs auto-complete values in the models.

Copy link

codecov bot commented May 14, 2024

Codecov Report

Attention: Patch coverage is 76.66667% with 7 lines in your changes are missing coverage. Please review.

Project coverage is 77.81%. Comparing base (4cf6ba3) to head (774e677).

Files Patch % Lines
celery/app/base.py 76.66% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9023      +/-   ##
==========================================
+ Coverage   77.80%   77.81%   +0.01%     
==========================================
  Files         150      150              
  Lines       18682    18711      +29     
  Branches     3194     3201       +7     
==========================================
+ Hits        14535    14560      +25     
- Misses       3854     3860       +6     
+ Partials      293      291       -2     
Flag Coverage Δ
unittests 77.79% <76.66%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mathiasertl
Copy link
Contributor Author

Note: I force-pushed a few minor changes to make sure that test coverage does not decrease.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant