Skip to main content

Automatically generate two versions of your pydantic models: one with Extra.forbid and one with Extra.ignore

Project description

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


Test Coverage PyPI Supported Python versions

Installation

pip install pydantic-duality

Quickstart

Given the following models:

from pydantic_duality import DualBaseModel


class User(DualBaseModel):
    id: UUID
    name: str

class Auth(DualBaseModel):
    some_field: str
    user: User

Using pydantic-duality is roughly equivalent to making all of the following models by hand:

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, 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 for more details

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_duality-1.1.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

pydantic_duality-1.1.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_duality-1.1.1.tar.gz.

File metadata

  • Download URL: pydantic_duality-1.1.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.11.3 Linux/6.0.19-4-MANJARO

File hashes

Hashes for pydantic_duality-1.1.1.tar.gz
Algorithm Hash digest
SHA256 6d64b074bbb5e242e80f8008f6ab9cd3d1fb018010c5827ecdf35496368913d7
MD5 ba3110f495219a41e9bb64448d2f187f
BLAKE2b-256 e7aa3212aa1bf838bee6ae7ddf7878142486fc9725d40a0535f5e614e822d0c5

See more details on using hashes here.

File details

Details for the file pydantic_duality-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: pydantic_duality-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.11.3 Linux/6.0.19-4-MANJARO

File hashes

Hashes for pydantic_duality-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c528ef71ffb286a01e1b98640dbf0658dddf1733228ad5b86b414ccd56ff0535
MD5 9c31ef9a0e420493a1563064a7c36661
BLAKE2b-256 af9b936279ed1be7f3ce1fde7083d2ef37eefb19c4620f52822124f71dc88a7f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page