### Defining Python Project Dependencies and Development Tools Source: https://github.com/tiangolo/pydantic-sqlalchemy/blob/master/requirements.txt This snippet illustrates common entries in a Python requirements file. It includes installing the current project in editable mode, referencing another requirements file for test dependencies, and specifying a version range for the 'pre-commit' development tool. ```Python Requirements -e . -r requirements-tests.txt pre-commit >=2.17.0,<5.0.0 ``` -------------------------------- ### Defining Python Project Dependencies Source: https://github.com/tiangolo/pydantic-sqlalchemy/blob/master/requirements-tests.txt This snippet specifies the direct and development dependencies for a Python project. It includes tools for testing (pytest, pytest-cov, coverage), static analysis (mypy, ruff), and specific libraries (typing-extensions, sqlalchemy-utc) with their version constraints, ensuring a consistent development environment. ```Python -e . pytest >=7.0.1,<9.0.0 coverage[toml] >=6.2,<8.0 mypy ==1.4.1 ruff ==0.9.8 typing-extensions ==4.6.1 pytest-cov ==4.1.0 sqlalchemy-utc >=0.14.0,<0.15.0 ``` -------------------------------- ### Generating Pydantic Models from SQLAlchemy ORM in Python Source: https://github.com/tiangolo/pydantic-sqlalchemy/blob/master/README.md This snippet demonstrates how to define SQLAlchemy ORM models (`User`, `Address`), establish a database connection, create tables, and persist data. It then uses `pydantic_sqlalchemy.sqlalchemy_to_pydantic` to automatically generate Pydantic models (`PydanticUser`, `PydanticAddress`) from these SQLAlchemy models. It also shows how to extend a generated Pydantic model to include related objects (`PydanticUserWithAddresses`) and how to validate and serialize SQLAlchemy ORM instances into Pydantic-compatible dictionaries, including nested relationships. ```Python from typing import List from pydantic_sqlalchemy import sqlalchemy_to_pydantic from sqlalchemy import Column, ForeignKey, Integer, String, create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import Session, relationship, sessionmaker Base = declarative_base() engine = create_engine("sqlite://", echo=True) class User(Base): __tablename__ = "users" id = Column(Integer, primary_key=True) name = Column(String) fullname = Column(String) nickname = Column(String) addresses = relationship( "Address", back_populates="user", cascade="all, delete, delete-orphan" ) class Address(Base): __tablename__ = "addresses" id = Column(Integer, primary_key=True) email_address = Column(String, nullable=False) user_id = Column(Integer, ForeignKey("users.id")) user = relationship("User", back_populates="addresses") PydanticUser = sqlalchemy_to_pydantic(User) PydanticAddress = sqlalchemy_to_pydantic(Address) class PydanticUserWithAddresses(PydanticUser): addresses: List[PydanticAddress] = [] Base.metadata.create_all(engine) LocalSession = sessionmaker(bind=engine) db: Session = LocalSession() ed_user = User(name="ed", fullname="Ed Jones", nickname="edsnickname") address = Address(email_address="ed@example.com") address2 = Address(email_address="eddy@example.com") ed_user.addresses = [address, address2] db.add(ed_user) db.commit() def test_pydantic_sqlalchemy(): user = db.query(User).first() pydantic_user = PydanticUser.from_orm(user) data = pydantic_user.dict() assert data == { "fullname": "Ed Jones", "id": 1, "name": "ed", "nickname": "edsnickname", } pydantic_user_with_addresses = PydanticUserWithAddresses.from_orm(user) data = pydantic_user_with_addresses.dict() assert data == { "fullname": "Ed Jones", "id": 1, "name": "ed", "nickname": "edsnickname", "addresses": [ {"email_address": "ed@example.com", "id": 1, "user_id": 1}, {"email_address": "eddy@example.com", "id": 2, "user_id": 1} ] } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.