pydantic-resolve
pydantic-resolve copied to clipboard
pydantic-resolve turns pydantic from a static data container into a powerful composable computing container.
Pydantic-resolve is a schema based, hierarchical solution for fetching and crafting data.
Features:
- By providing your pydantic schema and instance(s), resolver will recursively resolve uncertain nodes and their descendants.
- You can modify resolved nodes, or compute new nodes based on resolved nodes, no need of iteration.
- Schemas are pluggable, easy to combine and reuse.
Install
User of pydantic v2, please use pydantic2-resolve instead.
pip install pydantic-resolve
For user from GraphQL
This is how we make queries in GraphQL, dive by describing schema and field names.
query {
MyBlogSite {
name
blogs {
id
title
comments {
id
content
}
# comment_count
}
# comment_count
}
}
using query statement is flexible, but for scenarios like
API integrationit can be difficult to maintain.
In client, field comment_count is the length of each blog and also the total count (just for demo, pls ignore pagination staff), so client side need to iterate over the blogs to get the length and the sum, which is boring (things getting worse if the structure is deeper).
With pydantic-resolve, we can handle these calculations at server side. Let see how it works.
By transforming the query into pydantic schemas and attach some resolve, post methods, it looks like:
from __future__ import annotations
import comment_service as cs
import blog_service as bs
class MySite(BaseModel):
blogs: list[MySiteBlog] = []
async def resolve_blogs(self):
return await bs.get_blogs()
comment_count: int = 0
def post_comment_count(self):
return sum([b.comment_count for b in self.blogs]) # for total
class MySiteBlog(bs.Blog): # >>> inherit and extend <<<
comments: list[cs.Comment] = []
def resolve_comments(self, loader=LoaderDepend(cs.blog_to_comments_loader)):
return loader.load(self.id)
comment_count: int = 0
def post_comment_count(self):
return len(self.comments) # for each blog
async def main():
my_blog_site = MyBlogSite(name: "tangkikodo's blog")
my_blog_site = await Resolver().resolve(my_blog_site)
schemas , query functions and loader functions are provided by entity's service modules.
And then we can simpily inherit and extend from these base schemas to declare the schemas you want.
This just like columns of values (inherit) and of foreign keys (extend) in concept of relational database.
After transforming GraphQL query into pydantic schemas, post calculation become dead easy, and no more iterations.
API Reference
for more details, please refer to: https://allmonday.github.io/pydantic-resolve/reference_api/
Composition oriented development-pattern (wip)
This repo introduce a pattern that balance the speed of development and the maintaince, readability of your project, based on pydantic-resolve.
https://github.com/allmonday/composition-oriented-development-pattern
Unittest
poetry run python -m unittest # or
poetry run pytest # or
poetry run tox
Coverage
poetry run coverage run -m pytest
poetry run coverage report -m
Sponsor
If this code helps and you wish to support me
Paypal: https://www.paypal.me/tangkikodo