pydantic-duality


Namepydantic-duality JSON
Version 1.2.0 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-04-01 09:10:26
maintainerNone
docs_urlNone
authorStanislav Zmiev
requires_python<4.0,>=3.8
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.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Stanislav Zmiev",
    "author_email": "zmievsa@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/91/b8/629ca5f981cb91c86fffad1872c35695857dc4e34a512c0e60e3412e85b3/pydantic_duality-1.2.0.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": "1.2.0",
    "project_urls": {
        "Homepage": "https://github.com/zmievsa/pydantic-duality",
        "Repository": "https://github.com/zmievsa/pydantic-duality"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "db74dc92ecc0c60c3a55496bcffca080d3b4076350ed3d748cf0e494021d9b11",
                "md5": "c7e4d5f37d4f99d3319c72ad806fe4d3",
                "sha256": "0162dcfaea6cc5440d18283bbb4b488a61716f2fe4bcbc1d40020b1f96fe19bd"
            },
            "downloads": -1,
            "filename": "pydantic_duality-1.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c7e4d5f37d4f99d3319c72ad806fe4d3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.8",
            "size": 5812,
            "upload_time": "2024-04-01T09:10:25",
            "upload_time_iso_8601": "2024-04-01T09:10:25.151230Z",
            "url": "https://files.pythonhosted.org/packages/db/74/dc92ecc0c60c3a55496bcffca080d3b4076350ed3d748cf0e494021d9b11/pydantic_duality-1.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "91b8629ca5f981cb91c86fffad1872c35695857dc4e34a512c0e60e3412e85b3",
                "md5": "77644b570e71a47b127c38f67ca912f7",
                "sha256": "0202c47ebdeba755d946c90397c2d954d4bf31e408ee4e03d69006848278b844"
            },
            "downloads": -1,
            "filename": "pydantic_duality-1.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "77644b570e71a47b127c38f67ca912f7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.8",
            "size": 5749,
            "upload_time": "2024-04-01T09:10:26",
            "upload_time_iso_8601": "2024-04-01T09:10:26.704572Z",
            "url": "https://files.pythonhosted.org/packages/91/b8/629ca5f981cb91c86fffad1872c35695857dc4e34a512c0e60e3412e85b3/pydantic_duality-1.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-01 09:10:26",
    "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.21117s