Skip to main content

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

Metadata

Release files for metantic 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for metantic 0.2.0
File Interpreter ABI Platform
metantic-0.2.0-py3-none-any.whl Python 3 none any Details

Release files / metantic-0.2.0-py3-none-any.whl

Download URL metantic-0.2.0-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7f7867d8045adf646dd70595b6b5a87b138a1956e8a9a3e89eff2938ebe84947
BLAKE2b-256 checksum
How to use checksums
c11af41b45afeb0f3465e68c69a09f0cd398b41dfb1bea1b33ca834bc04c316a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release history Release notifications | RSS feed

This release

0.2.0 This release

1 release file

0.1.16

1 release file

0.1.15

1 release file

0.1.14

1 release file

0.1.13

1 release file

0.1.12

1 release file

0.1.11

1 release file

0.1.10

1 release file

0.1.9

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page