Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zmievsa/pydantic-duality
Automatically and lazily generate three versions of your pydantic models: one with Extra.forbid, one with Extra.ignore, and one with all fields optional
https://github.com/zmievsa/pydantic-duality
hints json-schema pydantic pydantic-models python python310 python311 schemas validation
Last synced: 9 days ago
JSON representation
Automatically and lazily generate three versions of your pydantic models: one with Extra.forbid, one with Extra.ignore, and one with all fields optional
- Host: GitHub
- URL: https://github.com/zmievsa/pydantic-duality
- Owner: zmievsa
- License: mit
- Created: 2023-02-23T00:21:16.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-04-01T09:12:05.000Z (7 months ago)
- Last Synced: 2024-09-25T23:11:53.730Z (about 1 month ago)
- Topics: hints, json-schema, pydantic, pydantic-models, python, python310, python311, schemas, validation
- Language: Python
- Homepage: https://ovsyanka83.github.io/pydantic-duality/
- Size: 886 KB
- Stars: 31
- Watchers: 1
- Forks: 1
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Automatically and lazily generate three versions of your pydantic models: one with Extra.forbid, one with Extra.ignore, and one with all fields optional---
## Installation
```bash
pip install pydantic-duality
```## Quickstart
Given the following models:
```python
from pydantic_duality import DualBaseModel
class User(DualBaseModel):
id: UUID
name: strclass Auth(DualBaseModel):
some_field: str
user: User
```Using pydantic-duality is roughly equivalent to making all of the following models by hand:
```python
from pydantic import BaseModel
# Equivalent to User and User.__request__
class UserRequest(BaseModel, extra=Extra.forbid):
id: UUID
name: str# Rougly equivalent to Auth and Auth.__request__
class AuthRequest(BaseModel, extra=Extra.forbid):
some_field: str
user: UserRequest# Rougly equivalent to User.__response__
class UserResponse(BaseModel, extra=Extra.ignore):
id: UUID
name: str# Rougly equivalent to Auth.__response__
class AuthResponse(BaseModel, extra=Extra.ignore):
some_field: str
user: UserResponse# Rougly equivalent to User.__patch_request__
class UserPatchRequest(BaseModel, extra=Extra.forbid):
id: UUID | None
name: str | None# Rougly equivalent to Auth.__patch_request__
class AuthPatchRequest(BaseModel, extra=Extra.forbid):
some_field: str | None
user: UserPatchRequest | None```
So it takes you up to 3 times less code to write the same thing. Note also that pydantic-duality does everything lazily so you will not notice any significant performance or memory usage difference when using it instead of writing everything by hand. Think of it as using all the customized models as cached properties.
Inheritance, inner models, custom configs, [custom names](https://zmievsa.github.io/pydantic-duality/#/?id=customizing-schema-names), config kwargs, isinstance and subclass checks work intuitively and in the same manner as they would work if you were not using pydantic-duality.
## Help
See [documentation](https://zmievsa.github.io/pydantic-duality/#/) for more details