pydantic-duality


Namepydantic-duality JSON
Version 2.0.1 PyPI version JSON
download
home_pagehttps://github.com/zmievsa/pydantic-duality
SummaryAutomatically generate two versions of your pydantic models: one with Extra.forbid and one with Extra.ignore
upload_time2024-12-14 19:14:20
maintainerNone
docs_urlNone
authorStanislav Zmiev
requires_python<4.0,>=3.9
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
<p align="center">
  <a href="https://zmievsa.github.io/pydantic-duality/"><img src="https://raw.githubusercontent.com/zmievsa/pydantic-duality/main/docs/_media/logo_with_text.svg" alt="Pydantic Duality"></a>
</p>
<p align="center">
  <b>Automatically and lazily generate three versions of your pydantic models: one with Extra.forbid, one with Extra.ignore, and one with all fields optional</b>
</p>

---

<p align="center">
<a href="https://github.com/zmievsa/pydantic-duality/actions?query=workflow%3ATests+event%3Apush+branch%3Amain" target="_blank">
    <img src="https://github.com/zmievsa/pydantic-duality/actions/workflows/test.yaml/badge.svg?branch=main&event=push" alt="Test">
</a>
<a href="https://codecov.io/gh/zmievsa/pydantic-duality" target="_blank">
    <img src="https://img.shields.io/codecov/c/github/zmievsa/pydantic-duality?color=%2334D058" alt="Coverage">
</a>
<a href="https://pypi.org/project/pydantic-duality/" target="_blank">
    <img alt="PyPI" src="https://img.shields.io/pypi/v/pydantic-duality?color=%2334D058&label=pypi%20package" alt="Package version">
</a>
<a href="https://pypi.org/project/pydantic-duality/" target="_blank">
    <img src="https://img.shields.io/pypi/pyversions/pydantic-duality?color=%2334D058" alt="Supported Python versions">
</a>
</p>

## Installation

```bash
pip install pydantic-duality
```

## Quickstart

Given the following models:

```python

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:

```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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/zmievsa/pydantic-duality",
    "name": "pydantic-duality",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": "Stanislav Zmiev",
    "author_email": "zmievsa@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/fc/0e/b6f7465ac433b6c433f4db1bd071680e697ae516b421423fb1245acd57d9/pydantic_duality-2.0.1.tar.gz",
    "platform": null,
    "description": "\n<p align=\"center\">\n  <a href=\"https://zmievsa.github.io/pydantic-duality/\"><img src=\"https://raw.githubusercontent.com/zmievsa/pydantic-duality/main/docs/_media/logo_with_text.svg\" alt=\"Pydantic Duality\"></a>\n</p>\n<p align=\"center\">\n  <b>Automatically and lazily generate three versions of your pydantic models: one with Extra.forbid, one with Extra.ignore, and one with all fields optional</b>\n</p>\n\n---\n\n<p align=\"center\">\n<a href=\"https://github.com/zmievsa/pydantic-duality/actions?query=workflow%3ATests+event%3Apush+branch%3Amain\" target=\"_blank\">\n    <img src=\"https://github.com/zmievsa/pydantic-duality/actions/workflows/test.yaml/badge.svg?branch=main&event=push\" alt=\"Test\">\n</a>\n<a href=\"https://codecov.io/gh/zmievsa/pydantic-duality\" target=\"_blank\">\n    <img src=\"https://img.shields.io/codecov/c/github/zmievsa/pydantic-duality?color=%2334D058\" alt=\"Coverage\">\n</a>\n<a href=\"https://pypi.org/project/pydantic-duality/\" target=\"_blank\">\n    <img alt=\"PyPI\" src=\"https://img.shields.io/pypi/v/pydantic-duality?color=%2334D058&label=pypi%20package\" alt=\"Package version\">\n</a>\n<a href=\"https://pypi.org/project/pydantic-duality/\" target=\"_blank\">\n    <img src=\"https://img.shields.io/pypi/pyversions/pydantic-duality?color=%2334D058\" alt=\"Supported Python versions\">\n</a>\n</p>\n\n## Installation\n\n```bash\npip install pydantic-duality\n```\n\n## Quickstart\n\nGiven the following models:\n\n```python\n\nfrom pydantic_duality import DualBaseModel\n\n\nclass User(DualBaseModel):\n    id: UUID\n    name: str\n\nclass Auth(DualBaseModel):\n    some_field: str\n    user: User\n```\n\nUsing pydantic-duality is roughly equivalent to making all of the following models by hand:\n\n```python\n\nfrom pydantic import BaseModel\n\n# Equivalent to User and User.__request__\nclass UserRequest(BaseModel, extra=Extra.forbid):\n    id: UUID\n    name: str\n\n# Rougly equivalent to Auth and Auth.__request__\nclass AuthRequest(BaseModel, extra=Extra.forbid):\n    some_field: str\n    user: UserRequest\n\n\n# Rougly equivalent to User.__response__\nclass UserResponse(BaseModel, extra=Extra.ignore):\n    id: UUID\n    name: str\n\n# Rougly equivalent to Auth.__response__\nclass AuthResponse(BaseModel, extra=Extra.ignore):\n    some_field: str\n    user: UserResponse\n\n\n# Rougly equivalent to User.__patch_request__\nclass UserPatchRequest(BaseModel, extra=Extra.forbid):\n    id: UUID | None\n    name: str | None\n\n# Rougly equivalent to Auth.__patch_request__\nclass AuthPatchRequest(BaseModel, extra=Extra.forbid):\n    some_field: str | None\n    user: UserPatchRequest | None\n\n```\n\nSo 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.\n\nInheritance, 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.\n\n## Help\n\nSee [documentation](https://zmievsa.github.io/pydantic-duality/#/) for more details\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Automatically generate two versions of your pydantic models: one with Extra.forbid and one with Extra.ignore",
    "version": "2.0.1",
    "project_urls": {
        "Homepage": "https://github.com/zmievsa/pydantic-duality",
        "Repository": "https://github.com/zmievsa/pydantic-duality"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ab3a51fe86bf575df4d85080c9a7836c376498bf04508fbbee86ef581624d4ae",
                "md5": "b629b5cfb003e35c86dded4c86bd0b45",
                "sha256": "8d9f3f11f4e5aa0b505965a848929459b94d6e1bb4ce29f7e4c208f6365663d6"
            },
            "downloads": -1,
            "filename": "pydantic_duality-2.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b629b5cfb003e35c86dded4c86bd0b45",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 6173,
            "upload_time": "2024-12-14T19:14:18",
            "upload_time_iso_8601": "2024-12-14T19:14:18.793914Z",
            "url": "https://files.pythonhosted.org/packages/ab/3a/51fe86bf575df4d85080c9a7836c376498bf04508fbbee86ef581624d4ae/pydantic_duality-2.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fc0eb6f7465ac433b6c433f4db1bd071680e697ae516b421423fb1245acd57d9",
                "md5": "fa5c223e4a4c9af4faed5c2c9d743762",
                "sha256": "9f9239fd1c7916b4c1cedacca8a327108baed2320c4eb5bb10ce50d1918d6db7"
            },
            "downloads": -1,
            "filename": "pydantic_duality-2.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "fa5c223e4a4c9af4faed5c2c9d743762",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 6114,
            "upload_time": "2024-12-14T19:14:20",
            "upload_time_iso_8601": "2024-12-14T19:14:20.967272Z",
            "url": "https://files.pythonhosted.org/packages/fc/0e/b6f7465ac433b6c433f4db1bd071680e697ae516b421423fb1245acd57d9/pydantic_duality-2.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-14 19:14:20",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "zmievsa",
    "github_project": "pydantic-duality",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pydantic-duality"
}
        
Elapsed time: 0.46126s