Skip to main content

Library for creating partial pydantic models (automatic converters) from different mappings

Reason this release was yanked:

Bug in sqlachemy extra

Project description

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

  1. Clone the repository
  2. Setup python (the library is made with python 3.10+)
  3. Install poetry (should work with v1.4.1)
  4. Install dependencies
  5. Install pre-commit hooks

Commands to use:

pip install poetry==1.4.1
poetry install
pre-commit install

Project details


Download files

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

Source Distribution

pydantic_marshals-0.3.17.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_marshals-0.3.17-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_marshals-0.3.17.tar.gz.

File metadata

  • Download URL: pydantic_marshals-0.3.17.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.2 Windows/10

File hashes

Hashes for pydantic_marshals-0.3.17.tar.gz
Algorithm Hash digest
SHA256 de2432500be63ddc20c9e055a4706fa5ef33104c8b577185c32ad6e2db249f54
MD5 0e455e15b962f6ee1aace14873fa6c48
BLAKE2b-256 5ad97793be5c745a4ee13dc114be4d97c5266f8185c904b1c7e83f449cf22f29

See more details on using hashes here.

File details

Details for the file pydantic_marshals-0.3.17-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_marshals-0.3.17-py3-none-any.whl
Algorithm Hash digest
SHA256 d38b4e61661e6d76c56f894d52ce174f5e9aa0b02eeb8419e81937389b51f94a
MD5 c5fcf3f0002cf29606d9d7a6c0cc2d13
BLAKE2b-256 c63995e7a78f54ef21457a43d9a9e32e631661fa9497ea6863442c2c6199c385

See more details on using hashes here.

Supported by

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