Skip to main content

Metaprogramming with pydantic Models (creating types from types)

Project description

Metantic

Metaprogramming with pydantic Models (creating types from types)

Partial

Create a partial model (all fields optional)

from metantic import Partial

class User(BaseModel):
    id: str
    name: str
    age: int

Partial(User).model_validate(dict(id="id", name="name"))
# PartialUser(id='id', name='name', age=None)

Fields

Obtain a Literal type for the model's fields (which can be validated)

from metantic import Fields

class User(BaseModel):
    id: str
    name: str
    age: int

Fields(User) # typing.Literal['id', 'name', 'age']

class Query(BaseModel):
    fields: Fields(User)

Query(fields=["id", "name"]) # OK
Query(fields=["email"]) # ValidationError: fields Input should be 'id', 'name' or 'age' [...]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

metantic-0.1.0-py3-none-any.whl (2.3 kB view hashes)

Uploaded Python 3

Supported by

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