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' [...]

Omit

Create a model with a subset of fields

from metantic import Fields

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

# the ID is unchangeable
def update(patch: Omit(User, ["id"])):
    ...

Take

Create a model with a subset of fields (recursively)

class FullName(BaseModel):
    first: str; middle: str; family: str

class User(BaseModel):
    id: str
    full_name: FullName
    friends: list[str]
    
# field -> take the field
# (field, subfields) -> take field but with subfields only
UserInfo = Take(User, ['id', ('full_name': ['first', 'family'])])

# UserInfo is equivalent to
class UserInfo(BaseModel):
    id: str
    full_name: TakeFullName

class TakeFullName(BaseModel):
    first: str; family: str

Paths

Type-safe paths

Validate

from metantic import paths

class FullName(BaseModel):
    first: str; middle: str; family: str

class User(BaseModel):
    id: str
    full_name: FullName
    friends: list[str]
    
paths.validate(['full_name', 'first'], User.model_json_schema()) # None
paths.validate(['bad_name', 'first'], User.model_json_schema()) # ValueError("Key 'bad_name' doesn't exist in ['id', 'full_name', 'friends'])"

Type

Create a path RootModel with validation

from metantic.paths import Path

UserPath = Path(User)

UserPath(root=['full_name', 'first']) # PathT(root=['full_name', 'first'])
UserPath(root=['bad_name', 'first']) # raises ValueError(...)

Builder

Currently under construction. May fail for some generic/union types

from metantic import paths
    
ps = paths.builder(User) # PathBuilder object
ps['id'].path # ['id']
ps['full_name']['first'].path # ['full_name', 'first']
ps['friends'][69].path # ['friends', 69]

ps['full_name']['second'].path # ERROR
  • Works with tuples, lists, dicts, classes and pydantic models
  • Type unions stop path generation

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

Uploaded Python 3

File details

Details for the file metantic-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: metantic-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for metantic-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f7867d8045adf646dd70595b6b5a87b138a1956e8a9a3e89eff2938ebe84947
MD5 047e347b92ab58057e6d6bc95194eb73
BLAKE2b-256 c11af41b45afeb0f3465e68c69a09f0cd398b41dfb1bea1b33ca834bc04c316a

See more details on using hashes here.

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