From: Mike Bayer Date: Fri, 14 Oct 2022 12:41:03 +0000 (-0400) Subject: fix declarative styles examples re: imports X-Git-Tag: rel_2_0_0b2~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d78685b9ce3e88b14d48db8d3a9beac3825b8d17;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git fix declarative styles examples re: imports many missing imports and mis-imports, also use list[] for typing. Change-Id: Idd9140a89eb421c4b80503639a582b96ba66c601 References: #8634 --- diff --git a/doc/build/orm/declarative_styles.rst b/doc/build/orm/declarative_styles.rst index 10d14e4ebc..b644ac4181 100644 --- a/doc/build/orm/declarative_styles.rst +++ b/doc/build/orm/declarative_styles.rst @@ -50,14 +50,16 @@ With the declarative base class, new mapped classes are declared as subclasses of the base:: from datetime import datetime - from typing import List from typing import Optional - from sqlalchemy import Integer, String + from sqlalchemy import ForeignKey from sqlalchemy import func + from sqlalchemy import Integer + from sqlalchemy import String from sqlalchemy.orm import DeclarativeBase from sqlalchemy.orm import Mapped from sqlalchemy.orm import mapped_column + from sqlalchemy.orm import relationship class Base(DeclarativeBase): @@ -73,7 +75,7 @@ of the base:: nickname: Mapped[Optional[str]] = mapped_column(String(64)) create_date: Mapped[datetime] = mapped_column(insert_default=func.now()) - addresses: Mapped[List["Address"]] = relationship(back_populates="user") + addresses: Mapped[list["Address"]] = relationship(back_populates="user") class Address(Base): @@ -123,8 +125,17 @@ The example below sets up the identical mapping as seen in the previous section, using the :meth:`_orm.registry.mapped` decorator rather than using the :class:`_orm.DeclarativeBase` superclass:: - from sqlalchemy import mapped_column, ForeignKey, Integer, String, Text - from sqlalchemy.orm import registry, relationship + from datetime import datetime + from typing import Optional + + from sqlalchemy import ForeignKey + from sqlalchemy import func + from sqlalchemy import Integer + from sqlalchemy import String + from sqlalchemy.orm import Mapped + from sqlalchemy.orm import mapped_column + from sqlalchemy.orm import registry + from sqlalchemy.orm import relationship mapper_registry = registry() @@ -139,7 +150,7 @@ decorator rather than using the :class:`_orm.DeclarativeBase` superclass:: nickname: Mapped[Optional[str]] = mapped_column(String(64)) create_date: Mapped[datetime] = mapped_column(insert_default=func.now()) - addresses: Mapped[List["Address"]] = relationship(back_populates="user") + addresses: Mapped[list["Address"]] = relationship(back_populates="user") @mapper_registry.mapped