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
Literaltype 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
RootModelwith 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)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| metantic-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for metantic-0.2.0-py3-none-any.whl |
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
|
Security for metantic 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.