Skip to main content

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

Release files for pydantic-duality 2.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-duality 2.0.2
File Size Uploaded
pydantic_duality-2.0.2.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-duality 2.0.2
File Interpreter ABI Platform
pydantic_duality-2.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 12.3 kB

Release files / pydantic_duality-2.0.2.tar.gz

Download URL pydantic_duality-2.0.2.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
7416010fa52ced397dfec9324a4ea6d759e0efd1b3a37b18dc5596552709edda
BLAKE2b-256 checksum
How to use checksums
4c61d5dbb0b45243710106c53a67086fea93e7ed98ebb12dbfb4a35eaaf7d876
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.6 Darwin/24.1.0

Release files / pydantic_duality-2.0.2-py3-none-any.whl

Download URL pydantic_duality-2.0.2-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5ca6028cdcc03509a0204c3b3d1a2073784c055f8d939320cab8a1e31df08266
BLAKE2b-256 checksum
How to use checksums
b504cd2c47ba56496cfcaeb1c809ea3cec683fcfcb2e050f397f183c668032e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.6 Darwin/24.1.0

Release history Release notifications | RSS feed

This release

2.0.2 This release

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page