Various helpful extensions for working with pydantic
Project description
pydantic-gubbins
Table of Contents
Overview
This project contains various utils for working with pydantic.
Typing
DiscriminatedUnion
A common pattern pydantic users encounter is how to serialise/deserialise a field whose type is a union of BaseModel
types. This improved in pydantic v2 and various ways of implementing a discriminated union are detailed
here.
However, this approach is a bit unsatisfying as:
- One is forced to explicitly implement a literal for each affected type
- The type literal will be serialised regardless of whether it's needed or not. E.g, if there is a field explicitly of such a type (and not a union), the type literal will still be serialised
Stack Overflow and other forums have many long discussions on this topic, without apparently offering a solution, so
I have included my own implementation of DiscriminatedUnion. What it does:
- Creates tagged union of the types:
Union[Annotated[t1, Tag("t1")], Annotated[t1, Tag("t2")], ...] - Adds a
WrapSerializerto include the tag name in the serialised output - Adds a
Discrimatorwith a callable to retrieve the tag name from the serialised form - Uses the type's
__name__by default buttype.TYPE_KEYif present
SubclassOf
SubclassOf takes a single type as a parameter and returns a DiscriminatedUnion of all the (recursive) subclasses
of that type
Union
This can be used in place of typing.Union. It converts unions of BaseModel and dataclass types into a
DiscriminatedUnion. It also separates such "model" types from other types. In the event that both are encounted,
it returns Union[Union[<other types>], DiscriminatedUnion[<model types>]]
FrozenDict
I found this implementation (and I can't remember where!) and included it. This is because I have some upcoming changes which will convert collection types into immutable equivalents, to be combined with frozen models.
Descriptor Support
pydantic does not support using descriptors for model fields. I have raised an
issue for this and submitted PRs for
pydantic (some further discussion on that thread) and
pydantic-core but the maintainers are correct in that the whole
descriptor issue really needs more discussion.
In the interim, this project supplies an implementation of BaseModel, which can be used in
place of the standard pydantic offering and which supports descriptors for model fields. Absent descriptor fields,
it will perform exactly as the original. It is not a large amount of code and the changes are summarised below.
The intent of these changes is the descriptor model fields should behave as closely as possible to descriptors in
dataclasses.
Please note that property or cached_property passed as annotations will be ignored. This is because pydantic
already has special-case logic for them.
- The metaclass adds the descriptors onto the the returned type, calls
__set_name__on them, and populates__pydantic_descriptor_fields__ - The methods on
BaseModelwhich access__dict__directly have been overridden to extend their functionality to include descriptor fields - Access to
__dict__itself is now controlled by a descriptor. This implementation is rather low-level and possibly inadvisable. The same result might be achieved by using a model validator, however, there are many places inpydanticandpydantic-corewhere__dict__is accessed directly and I'm not convinced all would be covered by a validator
Using the BaseModel supplied by this project, the below works:
from pydantic_gubbins import BaseModel
from typing import Any
_field_descriptor_undefined = object()
class FieldDescriptor:
""" Example descriptor, just to show storage somewhere other than __dict__ """
def __init__(self, default: Any = _field_descriptor_undefined):
self.__default = default
self.__name = None
self.__values = {}
def __get__(self, instance, owner):
if instance is not None:
try:
return self.__values[id(instance)][self.__name]
except KeyError:
pass
if self.__default is _field_descriptor_undefined:
raise AttributeError
return self.__default
def __set__(self, instance, value):
self.__values.setdefault(id(instance), {})[self.__name] = value
def __set_name__(self, owner, name):
self.__name = name
class Foo(BaseModel):
s: str
i: int = FieldDescriptor(-1)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_gubbins-1.0.7.tar.gz.
File metadata
- Download URL: pydantic_gubbins-1.0.7.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d386d27c52a1adcaae0c35765fa929881cacbac2949df823b1549c6bdae37c25
|
|
| MD5 |
bfb09cf921d5fc7ad6d321bb89f1086a
|
|
| BLAKE2b-256 |
3ae944a7cd401782942dd36c68e38a7c30176cc5c37119efe7f5a3f80f9001dc
|
Provenance
The following attestation bundles were made for pydantic_gubbins-1.0.7.tar.gz:
Publisher:
python-publish.yml on nickyoung-github/pydantic-gubbins
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_gubbins-1.0.7.tar.gz -
Subject digest:
d386d27c52a1adcaae0c35765fa929881cacbac2949df823b1549c6bdae37c25 - Sigstore transparency entry: 160686014
- Sigstore integration time:
-
Permalink:
nickyoung-github/pydantic-gubbins@b0948a5ced32f0dfbea63f50e20526390ce03313 -
Branch / Tag:
refs/tags/1.0.7 - Owner: https://github.com/nickyoung-github
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@b0948a5ced32f0dfbea63f50e20526390ce03313 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pydantic_gubbins-1.0.7-py3-none-any.whl.
File metadata
- Download URL: pydantic_gubbins-1.0.7-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9f06ac155562230f9d3cf904f488fe9ec3fed4663757f418ed5b2b0193e6738
|
|
| MD5 |
e75c067fb48c682dd127543b03a1785a
|
|
| BLAKE2b-256 |
0d8d4865420f9dceba6cf81ac1f27f50bb73be6951e25a7cc31d7875a0617ae2
|
Provenance
The following attestation bundles were made for pydantic_gubbins-1.0.7-py3-none-any.whl:
Publisher:
python-publish.yml on nickyoung-github/pydantic-gubbins
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_gubbins-1.0.7-py3-none-any.whl -
Subject digest:
a9f06ac155562230f9d3cf904f488fe9ec3fed4663757f418ed5b2b0193e6738 - Sigstore transparency entry: 160686019
- Sigstore integration time:
-
Permalink:
nickyoung-github/pydantic-gubbins@b0948a5ced32f0dfbea63f50e20526390ce03313 -
Branch / Tag:
refs/tags/1.0.7 - Owner: https://github.com/nickyoung-github
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@b0948a5ced32f0dfbea63f50e20526390ce03313 -
Trigger Event:
release
-
Statement type: