Skip to main content

Tools to convert SQLAlchemy models to Pydantic models

Project description

Alchemista

Code style: black codecov

Tools to generate Pydantic models from SQLAlchemy models.

Still experimental.

Installation

Alchemista is available in PyPI. To install it with pip, run:

pip install alchemista

Usage

Simply call the sqlalchemy_to_pydantic function with a SQLAlchemy model. Each Column in its definition will result in an attribute of the generated model via the Pydantic Field function.

For example, a SQLAlchemy model like the following

from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import declarative_base


Base = declarative_base()

class PersonDB(Base):
    __tablename__ = "people"

    id = Column(Integer, primary_key=True)
    age = Column(Integer, default=0, nullable=False, doc="Age in years")
    name = Column(String(128), nullable=False, doc="Full name")

could have a generated Pydantic model via

from alchemista import sqlalchemy_to_pydantic

Person = sqlalchemy_to_pydantic(PersonDB)

and would result in a Pydantic model equivalent to

from pydantic import BaseModel, Field


class Person(BaseModel):
    id: int
    age: int = Field(0, description="Age in years")
    name: str = Field(..., max_length=128, description="Full name")

    class Config:
        orm_mode = True

Note that the string length from the column definition was sufficient to add a max_length constraint. Additionally, by default, the generated model will have orm_mode=True. That can be customized via the config keyword argument. There is also an exclude keyword argument that accepts a set of field names to not include in the generated model.

This example is available in a short executable form in the examples/ directory.

Field arguments and info

Currently, the type, default value (either scalar or callable), and the description (from the doc attribute) are extracted directly from the Column definition. However, except for the type, all of them can be overridden via the info dictionary attribute. All other custom arguments to the Field function are specified there too. The supported keys are listed in alchemista.field.Info.

Everything specified in info is preferred from what has been extracted from Column. This means that the default value and the description can be overridden if so desired. Also, similarly to using Pydantic directly, default and default_factory are mutually-exclusive, so they cannot be used together. Use default_factory if the default value comes from calling a function (without any arguments).

For example, in the case above,

name = Column(String(128), nullable=False, doc="Full name", info=dict(description=None, max_length=64))

would instead result in

name: str = Field(..., max_length=64)

License

This project is licensed under the terms of the MIT license.

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

alchemista-0.1.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

alchemista-0.1.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file alchemista-0.1.1.tar.gz.

File metadata

  • Download URL: alchemista-0.1.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.6.13 Linux/5.4.0-1047-azure

File hashes

Hashes for alchemista-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3d538f1d7968fec82f5c1a716c956f7188cd9d42ebd37ce84d086900b2ef832d
MD5 bdda5d3393dd744ffa53de5738e57743
BLAKE2b-256 ae559c565cbfe172d9820aed3f87ec30c551f8f5b6b70a9cf01fb66d3721f9b7

See more details on using hashes here.

File details

Details for the file alchemista-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: alchemista-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.6.13 Linux/5.4.0-1047-azure

File hashes

Hashes for alchemista-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e97fde3c05eea3d482d9d586a1c6cd570dafb5c5994b75e66bca876819c941bf
MD5 59ffabc641c7b94fd42d36be8e0607e3
BLAKE2b-256 0ba3fdc172e0e49ae3c71e275655ef1fe5884273cb0952b39b825eefef47c5fb

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