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.2.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.2-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for pydantic_flatten_rootmodel-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5264346b5e98ed0fae553531b2384cb71ee79109a0003c08fb487ddd31a49e0f
MD5 29dcaca4bc131170928f0dc775181fcc
BLAKE2b-256 44ae9d41766704e3c5e0ab537098b303e43ad0eac64735a49b01b866311a525b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_flatten_rootmodel-0.1.2.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.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_flatten_rootmodel-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9a51d36c5eb94ea59045a7a43e6ff663f055dbf0969791b8ca76b1b01d3475ce
MD5 2dd525b8422e90220007f9b975cc8cc5
BLAKE2b-256 2613968e21a63787d7729cedc2f0644a79b2326aade2a7f444735688470a274b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_flatten_rootmodel-0.1.2-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