Skip to main content

Library to transform a Pydantic RootModel into a flattened BaseModel

Project description

pydantic-flatten-rootmodel

Library to transform a Pydantic RootModel with discriminated unions into a flattened BaseModel.

from pydantic-flatten-rootmodel import flatten_root_model

class Cat(BaseModel):
    pet_type: Annotated[Literal["cat"], Field()]
    meow: str


class Dog(BaseModel):
    pet_type: Annotated[Literal["dog"], Field()]
    bark: str

class Pet(RootModel[Cat | Dog]):
    root: Annotated[Cat | Dog, Field(discriminator="pet_type")]


FlattenedPet = flatten_root_model(Pet)

would result in FlattenedPet to have this shape:

class FlattenedPet(BaseModel):
   pet_type: Annotated[Union[Literal["cat"], Literal["dog"]]]
   bark: Union[str, None]
   meow: Union[str, None]

This can for example be leveraged by dlt for it's schema definition. Without flattening it, the discriminated union is not recognized correctly when setting up the table schema.

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

pydantic_flatten_rootmodel-0.1.1.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_flatten_rootmodel-0.1.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_flatten_rootmodel-0.1.1.tar.gz.

File metadata

File hashes

Hashes for pydantic_flatten_rootmodel-0.1.1.tar.gz
Algorithm Hash digest
SHA256 626222cd5a30f96c319aceea7407ff8a6b855e3ae27cba45a13e3d04e991bc77
MD5 cd2cf09735f4f1d878278b497dcdc209
BLAKE2b-256 5419b3602f57427219fe00f094d16ead7280734ef7bd1e3132176388f1ad69a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_flatten_rootmodel-0.1.1.tar.gz:

Publisher: test.yml on planet-a-ventures/pydantic-flatten-rootmodel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pydantic_flatten_rootmodel-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_flatten_rootmodel-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 99259a6b2d9cf230b7cc5c91417b1e5ea940300e968d235e6bf21a5361e931d1
MD5 967a946ef098c9be5df089029798a522
BLAKE2b-256 0867b288973a93783f9aeb5a42a6e97d47a897f481062bd3a43873c26a72293c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_flatten_rootmodel-0.1.1-py3-none-any.whl:

Publisher: test.yml on planet-a-ventures/pydantic-flatten-rootmodel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page