Pydantic Marshals
Library for creating partial pydantic models (automatic converters) from different mappings. Currently, it consists of basic boilerplate parts and functional implementation for sqlalchemy 2.0+ (included via extra)
Base Interface
TBA
Implementations
TBA
SQLAlchemy: Basic usage
# sqlalchemy 2.0+ is required
from sqlalchemy import ForeignKey, String, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from pydantic_marshals.sqlalchemy import MappedModel
class Avatar(Base):
__tablename__ = "avatars"
id: Mapped[int] = mapped_column(primary_key=True)
IdModel = MappedModel.create(columns=[id])
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
description: Mapped[str | None] = mapped_column(Text())
admin: Mapped[bool] = mapped_column() # empty `mapped_column()` is required for models
avatar_id: Mapped[int] = mapped_column(ForeignKey("avatars.id"))
avatar: Mapped[Avatar] = relationship()
@property
def representation(self) -> str:
return f"User #{self.id}: {self.name}"
BaseModel = MappedModel.create(columns=[id])
CreateModel = MappedModel.create(columns=[name, description])
PatchModel = CreateModel.as_patch()
IndexModel = MappedModel.create(properties=[representation])
FullModel = BaseModel.extend(
columns=[admin],
relationships=[(avatar, Avatar.IdModel)],
includes=[CreateModel, IndexModel],
)
with sessionmaker.begin() as session:
user = User(name="alex", description="cool person", avatar=Avatar(), admin=False)
session.add(user)
session.flush()
print(User.BaseModel.model_validate(user).model_dump())
# {"id": 0}
print(User.PatchModel.model_validate({}).model_dump(exclude_defaults=True))
# {}
print(User.PatchModel.model_validate({"description": None}).model_dump(exclude_defaults=True))
# {"description": None}
print(User.CreateModel.model_validate(user).model_dump())
# {"name": "alex", "description": "cool person"}
print(User.IndexModel.model_validate(user).model_dump())
# {"representation": "User #0: alex"}
print(User.FullModel.model_validate(user).model_dump())
# {
# "id": 0,
# "name": "alex",
# "description": "cool person",
# "representation": "User #0: alex",
# "avatar": {"id": 0},
# "admin": False
# }
Assert Contains
The "assert contains" is an interface for validating data, mainly used in testing. Use "assert-contains" extra to install this module:
pip install pydantic-marshals[assert-contains]
Documentation:
Local development
- Clone the repository
- Setup python (the library is made with python 3.10+)
- Install poetry (should work with v1.4.1)
- Install dependencies
- Install pre-commit hooks
Commands to use:
pip install poetry==1.4.1
poetry install
pre-commit install
Metadata
Release files for pydantic-marshals 0.3.19
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pydantic_marshals-0.3.19.tar.gz | 12.8 kB | Without provenance | Details for pydantic_marshals-0.3.19.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pydantic_marshals-0.3.19-py3-none-any.whl | Python 3 | none | any | Details for pydantic_marshals-0.3.19-py3-none-any.whl |
Total release size: 35.0 kB
Release files / pydantic_marshals-0.3.19.tar.gz
| Download URL | pydantic_marshals-0.3.19.tar.gz |
|---|---|
| Size | 12.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f1ca8c257406a9a515a7471a755fbaa864c480378d4ea9aaea973e0fac209860
|
|
BLAKE2b-256 checksum How to use checksums |
c60f686984c11f1a84afb11cd182d0cd4a53948625c77841309160cc88a02b38
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.1 CPython/3.12.2 Windows/10
|
Release files / pydantic_marshals-0.3.19-py3-none-any.whl
| Download URL | pydantic_marshals-0.3.19-py3-none-any.whl |
|---|---|
| Size | 22.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2f1f272cbe98ffd41ad79fb1a5d2709b1016ebe6c56679cad0cf36207972c643
|
|
BLAKE2b-256 checksum How to use checksums |
1fa314f0d2e853db828b52a019978abc97b06733a9ad066a2e0b0f70c0aa7593
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.1 CPython/3.12.2 Windows/10
|
Security for pydantic-marshals 0.3.19
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.