{"id":20949843,"url":"https://github.com/team23/pydantic-partial","last_synced_at":"2025-04-04T12:07:46.303Z","repository":{"id":57749238,"uuid":"524355433","full_name":"team23/pydantic-partial","owner":"team23","description":"Create partial models from pydantic models","archived":false,"fork":false,"pushed_at":"2025-03-18T07:52:04.000Z","size":114,"stargazers_count":59,"open_issues_count":5,"forks_count":9,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-28T11:09:10.004Z","etag":null,"topics":["model","partial","pydantic","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/team23.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-13T09:32:11.000Z","updated_at":"2025-03-28T05:53:11.000Z","dependencies_parsed_at":"2024-03-28T18:55:00.000Z","dependency_job_id":"301e4882-bb25-4043-aa98-3c51ddf8577f","html_url":"https://github.com/team23/pydantic-partial","commit_stats":{"total_commits":42,"total_committers":3,"mean_commits":14.0,"dds":"0.16666666666666663","last_synced_commit":"97af3bab82a58b9648ea7c8cc71ba1ccc9691865"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/team23%2Fpydantic-partial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/team23%2Fpydantic-partial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/team23%2Fpydantic-partial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/team23%2Fpydantic-partial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/team23","download_url":"https://codeload.github.com/team23/pydantic-partial/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247174418,"owners_count":20896078,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["model","partial","pydantic","python"],"created_at":"2024-11-19T00:43:42.711Z","updated_at":"2025-04-04T12:07:46.283Z","avatar_url":"https://github.com/team23.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pydantic-partial\n\n## Installation\n\nJust use `pip install pydantic-partial` to install the library.\n\n**Note:** `pydantic-partial` is compatible with `pydantic` versions `1.9`, `1.10` and even `2.x` (🥳) on\nPython `3.9`, `3.10`, `3.11`, `3.12` and `3.13`. This is also ensured running all tests on all those versions\nusing `tox`.\n\n## About\n\nCreate partial models from your normal pydantic models. Partial models will allow\nsome or all fields to be optional and thus not be required when creating the model\ninstance.\n\nPartial models can be used to support PATCH HTTP requests where the user only wants\nto update some fields of the model and normal validation for required fields is not\nrequired. It may also be used to have partial response DTOs where you want to skip\ncertain fields, this can be useful in combination with `exclude_none`. It is - like\nshown in these examples - intended to be used with API use cases, so when using\npydantic with for example FastAPI.\n\n**Disclaimer:** This is still an early release of `pydantic-partial`. Things might\nchange in the future. PR welcome. ;-)\n\n### Usage example\n\n`pydantic-partial` provides a mixin to generate partial model classes. The mixin can\nbe used like this:\n\n```python\nimport pydantic\nfrom pydantic_partial import PartialModelMixin\n\n# Something model, then can be used as a partial, too:\nclass Something(PartialModelMixin, pydantic.BaseModel):\n    name: str\n    age: int\n\n\n# Create a full partial model\nFullSomethingPartial = Something.model_as_partial()\nFullSomethingPartial()  # Same as FullSomethingPartial(name=None, age=None)\n```\n\n### Without using the mixin\n\nYou also may create partial models without using the mixin:\n\n```python\nimport pydantic\nfrom pydantic_partial import create_partial_model\n\n# Something model, without the mixin:\nclass Something(pydantic.BaseModel):\n    name: str\n    age: int\n\n\n# Create a full partial model\nFullSomethingPartial = create_partial_model(Something)\nFullSomethingPartial()  # Same as FullSomethingPartial(name=None, age=None)\n```\n\n### Only changing some fields to being optional\n\n`pydantic-partial` can be used to create partial models that only change some\nof the fields to being optional. Just pass the list of fields to be optional to\nthe `as_partial()` or `create_partial_model()` function.\n\n```python\nimport pydantic\nfrom pydantic_partial import create_partial_model\n\nclass Something(pydantic.BaseModel):\n    name: str\n    age: int\n\n# Create a partial model only for the name attribute\nFullSomethingPartial = create_partial_model(Something, 'name')\nFullSomethingPartial(age=40)  # Same as FullSomethingPartial(name=None, age=40)\n# This would still raise an error: FullSomethingPartial(age=None, ...)\n```\n\n### Recursive partials\n\nPartial models can be created changing the field of all nested models to being\noptional, too.\n\n```python\nfrom typing import List\n\nimport pydantic\nfrom pydantic_partial import PartialModelMixin, create_partial_model\n\nclass InnerSomething(PartialModelMixin, pydantic.BaseModel):\n    name: str\n\nclass OuterSomething(pydantic.BaseModel):\n    name: str\n    things: List[InnerSomething]\n\n# Create a full partial model\nRecursiveOuterSomethingPartial = create_partial_model(OuterSomething, recursive=True)\nRecursiveOuterSomethingPartial(things=[\n    {},\n])\n```\n\n**Note:** The inner model MUST extend the `PartialModelMixin` mixin. Otherwise\n`pydantic-partial` will not be able to detect which fields may allow to being\nconverted to partial models.\n\n**Also note:** My recommendation would be to always create such recursive\npartials by creating partials for all the required models and then override\nthe fields on you outer partial model class. This is way more explicit.\n\n## Known limitations\n\n`pydantic-partial` cannot generate new class types that actually are supported by the\nPython typing system rules. This means that the partial models will only be recognized\nas the same as their original model classes - type checkers will not know about the partial\nmodel changes and thus will think all those partial fields are still required.\n\nThis is due to the fact that Python itself has no concept of partials. `pydantic-partial`\ncould (in theory) provide plugins for `mypy` for example to \"patch\" this in, but this would\nbe a massive amount of work while being kind of a bad hack. The real solution would be to\nhave a partial type in Python itself, but this is not planned for the near future as far\nas I know.\n\nMy recommendation is to use `pydantic-partial` only for API use cases where you do not\nneed to work with the partial aspects of the models - they are just the DTOs (data transfer\nobjects) you are using. If you need to use partial models in other cases you might get\nerrors by your type checker - if you use one. Please be aware of this.\n\n**Note:** Not having a good solution in Python itself for this is the reason `pydantic` does\nnot support partial models in the first place. `pydantic-partial` is just a really good\nworkaround for this issue.  \nSee [issue 2](https://github.com/team23/pydantic-partial/issues/2) in this project and\n[issue 1673](https://github.com/pydantic/pydantic/issues/1673#issuecomment-1557267229)\nin the `pydantic` project for reference.\n\nHaving that all said: If anyone wants to get a working plugin for `mypy` or others ready,\nI'm going to very much support this.\n\n# Contributing\n\nIf you want to contribute to this project, feel free to just fork the project,\ncreate a dev branch in your fork and then create a pull request (PR). If you\nare unsure about whether your changes really suit the project please create an\nissue first, to talk about this.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteam23%2Fpydantic-partial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fteam23%2Fpydantic-partial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteam23%2Fpydantic-partial/lists"}