Package: pydantic-partial
pydantic-partialInstallationJust usepip install pydantic-partialto install the library.Note:pydantic-partialis compatible withpydanticversions1.9,1.10and even2.x(🥳) on
Python3.9,3.10and3.11. This is also ensured running all tests on all those versions
usingtox.AboutCreate partial models from your normal pydantic models. Partial models will allow
some or all fields to be optional and thus not be required when creating the model
instance.Partial models can be used to support PATCH HTTP requests where the user only wants
to update some fields of the model and normal validation for required fields is not
required. It may also be used to have partial response DTOs where you want to skip
certain fields, this can be useful in combination withexclude_none. It is - like
shown in these examples - intended to be used with API use cases, so when using
pydantic with for example FastAPI.Disclaimer:This is still an early release ofpydantic-partial. Things might
change in the future. PR welcome. ;-)Usage examplepydantic-partialprovides a mixin to generate partial model classes. The mixin can
be used like this:importpydanticfrompydantic_partialimportPartialModelMixin# Something model, then can be used as a partial, too:classSomething(PartialModelMixin,pydantic.BaseModel):name:strage:int# Create a full partial modelFullSomethingPartial=Something.model_as_partial()FullSomethingPartial()# Same as FullSomethingPartial(name=None, age=None)Without using the mixinYou also may create partial models without using the mixin:importpydanticfrompydantic_partialimportcreate_partial_model# Something model, without the mixin:classSomething(pydantic.BaseModel):name:strage:int# Create a full partial modelFullSomethingPartial=create_partial_model(Something)FullSomethingPartial()# Same as FullSomethingPartial(name=None, age=None)Only changing some fields to being optionalpydantic-partialcan be used to create partial models that only change some
of the fields to being optional. Just pass the list of fields to be optional to
theas_partial()orcreate_partial_model()function.importpydanticfrompydantic_partialimportcreate_partial_modelclassSomething(pydantic.BaseModel):name:strage:int# Create a partial model only for the name attributeFullSomethingPartial=create_partial_model(Something,'name')FullSomethingPartial(age=40)# Same as FullSomethingPartial(name=None, age=40)# This would still raise an error: FullSomethingPartial(age=None, ...)Recursive partialsPartial models can be created changing the field of all nested models to being
optional, too.fromtypingimportListimportpydanticfrompydantic_partialimportPartialModelMixin,create_partial_modelclassInnerSomething(PartialModelMixin,pydantic.BaseModel):name:strclassOuterSomething(pydantic.BaseModel):name:strthings:List[InnerSomething]# Create a full partial modelRecursiveOuterSomethingPartial=create_partial_model(OuterSomething,recursive=True)RecursiveOuterSomethingPartial(things=[{},])Note:The inner model MUST extend thePartialModelMixinmixin. Otherwisepydantic-partialwill not be able to detect which fields may allow to being
converted to partial models.Also note:My recommendation would be to always create such recursive
partials by creating partials for all the required models and then override
the fields on you outer partial model class. This is way more explicit.Known limitationspydantic-partialcannot generate new class types that actually are supported by the
Python typing system rules. This means that the partial models will only be recognized
as the same as their original model classes - type checkers will not know about the partial
model changes and thus will think all those partial fields are still required.This is due to the fact that Python itself has no concept of partials.pydantic-partialcould (in theory) provide plugins formypyfor example to "patch" this in, but this would
be a massive amount of work while being kind of a bad hack. The real solution would be to
have a partial type in Python itself, but this is not planned for the near future as far
as I know.My recommendation is to usepydantic-partialonly for API use cases where you do not
need to work with the partial aspects of the models - they are just the DTOs (data transfer
objects) you are using. If you need to use partial models in other cases you might get
errors by your type checker - if you use one. Please be aware of this.Note:Not having a good solution in Python itself for this is the reasonpydanticdoes
not support partial models in the first place.pydantic-partialis just a really good
workaround for this issue.Seeissue 2in this project andissue 1673in thepydanticproject for reference.Having that all said: If anyone wants to get a working plugin formypyor others ready,
I'm going to very much support this.ContributingIf you want to contribute to this project, feel free to just fork the project,
create a dev branch in your fork and then create a pull request (PR). If you
are unsure about whether your changes really suit the project please create an
issue first, to talk about this.