Make pydantic have a GraphQL-like assembly experience.
Project description
Pydantic-resolve
install
pip install pydantic-resolve
demo
from pydantic_resolve import resolve
class Book(BaseModel):
name: str
class Student(BaseModel):
name: str
intro: str = ''
def resolve_intro(self):
return f'hello {self.name}'
books: tuple[Book, ...] = tuple()
async def resolve_books(self):
return await get_books()
async def get_books():
await asyncio.sleep(1)
return [Book(name="sky"), Book(name="sea")]
class TestResolver(unittest.IsolatedAsyncioTestCase):
async def test_resolver_1(self):
stu = Student(name="boy")
result = await resolve(stu)
expected = {
'name': 'boy',
'intro': 'hello boy',
'books': [{'name': 'sky'}, {'name': 'sea'}]
}
self.assertEqual(result.dict(), expected)
async def test_resolver_2(self):
stu = [Student(name="boy")]
result = await resolve(stu)
expected = {
'name': 'boy',
'intro': 'hello boy',
'books': [{'name': 'sky'}, {'name': 'sea'}]
}
self.assertEqual(result[0].dict(), expected)
async def test_schema(self):
# Student.update_forward_refs(Book=Book)
schema = Student.schema_json()
expected = '''{"title": "Student", "type": "object", "properties": {"name": {"title": "Name", "type": "string"}, "intro": {"title": "Intro", "default": "", "type": "string"}, "books": {"title": "Books", "default": [], "type": "array", "items": {"$ref": "#/definitions/Book"}}}, "required": ["name"], "definitions": {"Book": {"title": "Book", "type": "object", "properties": {"name": {"title": "Name", "type": "string"}}, "required": ["name"]}}}'''
self.assertEqual(schema, expected)
TODO:
play with aiodataloader
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pydantic_resolve-0.2.0.tar.gz
(3.2 kB
view hashes)
Built Distribution
Close
Hashes for pydantic_resolve-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 190d7dc0a263e411c06eee489cfb19e9f2991792d16b2c756512ed0c93262d1d |
|
MD5 | a3c7971577e2c0eaac55368d527bb542 |
|
BLAKE2b-256 | 0f1b175a4abaeedc28dcd0b94b39646508cb673b6cb48772dc7794b915a9ca24 |