From e21dff8723ac218a625b343d2652d841b5b09c6f Mon Sep 17 00:00:00 2001 From: Harry Lees Date: Tue, 31 Jan 2023 12:58:19 +0000 Subject: [PATCH] moved all fixed instances of list[] -> List[] --- doc/build/changelog/migration_20.rst | 4 +- doc/build/orm/basic_relationships.rst | 57 ++++++++++--------- doc/build/orm/collection_api.rst | 2 +- doc/build/orm/declarative_styles.rst | 5 +- doc/build/orm/extensions/associationproxy.rst | 12 ++-- doc/build/orm/extensions/asyncio.rst | 3 +- doc/build/orm/inheritance.rst | 8 +-- doc/build/orm/large_collections.rst | 2 +- .../orm/queryguide/_inheritance_setup.rst | 5 +- doc/build/orm/queryguide/relationships.rst | 4 +- doc/build/orm/quickstart.rst | 3 +- doc/build/tutorial/orm_related_objects.rst | 6 +- 12 files changed, 59 insertions(+), 52 deletions(-) diff --git a/doc/build/changelog/migration_20.rst b/doc/build/changelog/migration_20.rst index 51e34955ee..604d59e083 100644 --- a/doc/build/changelog/migration_20.rst +++ b/doc/build/changelog/migration_20.rst @@ -471,7 +471,7 @@ are subject to these errors, as would occur in the example below:: id: int = Column(Integer, primary_key=True) # will raise - bars: list["Bar"] = relationship("Bar", back_populates="foo") + bars: List["Bar"] = relationship("Bar", back_populates="foo") class Bar(Base): @@ -519,7 +519,7 @@ that descend from ``Base``:: id: int = Column(Integer, primary_key=True) - bars: list["Bar"] = relationship("Bar", back_populates="foo") + bars: List["Bar"] = relationship("Bar", back_populates="foo") class Bar(Base): diff --git a/doc/build/orm/basic_relationships.rst b/doc/build/orm/basic_relationships.rst index 28c1c65bfd..116c6b6d4b 100644 --- a/doc/build/orm/basic_relationships.rst +++ b/doc/build/orm/basic_relationships.rst @@ -10,6 +10,7 @@ based on the use of the :class:`_orm.Mapped` annotation type. The setup for each of the following sections is as follows:: from __future__ import annotations + from typing import List from sqlalchemy import ForeignKey from sqlalchemy import Integer @@ -40,7 +41,7 @@ which is the most modern form of SQLAlchemy Declarative mapping:: __tablename__ = "parent_table" id: Mapped[int] = mapped_column(primary_key=True) - children: Mapped[list["Child"]] = relationship(back_populates="parent") + children: Mapped[List["Child"]] = relationship(back_populates="parent") class Child(Base): @@ -114,7 +115,7 @@ a collection of items represented by the child:: __tablename__ = "parent_table" id: Mapped[int] = mapped_column(primary_key=True) - children: Mapped[list["Child"]] = relationship() + children: Mapped[List["Child"]] = relationship() class Child(Base): @@ -134,7 +135,7 @@ as the value for :paramref:`_orm.relationship.back_populates` on the other:: __tablename__ = "parent_table" id: Mapped[int] = mapped_column(primary_key=True) - children: Mapped[list["Child"]] = relationship(back_populates="parent") + children: Mapped[List["Child"]] = relationship(back_populates="parent") class Child(Base): @@ -236,7 +237,7 @@ as the value for :paramref:`_orm.relationship.back_populates` on the other:: __tablename__ = "child_table" id: Mapped[int] = mapped_column(primary_key=True) - parents: Mapped[list["Parent"]] = relationship(back_populates="child") + parents: Mapped[List["Parent"]] = relationship(back_populates="child") .. _relationship_patterns_nullable_m2o: @@ -265,7 +266,7 @@ case the configuration would look like:: __tablename__ = "child_table" id: Mapped[int] = mapped_column(primary_key=True) - parents: Mapped[list["Parent"]] = relationship(back_populates="child") + parents: Mapped[List["Parent"]] = relationship(back_populates="child") Above, the column for ``Parent.child_id`` will be created in DDL to allow ``NULL`` values. When using :func:`_orm.mapped_column` with explicit typing @@ -297,7 +298,7 @@ for background on this behavior. __tablename__ = "child_table" id: Mapped[int] = mapped_column(primary_key=True) - parents: Mapped[list[Parent]] = relationship(back_populates="child") + parents: Mapped[List[Parent]] = relationship(back_populates="child") .. _relationships_one_to_one: @@ -423,7 +424,7 @@ with which to link:: __tablename__ = "left_table" id: Mapped[int] = mapped_column(primary_key=True) - children: Mapped[list[Child]] = relationship(secondary=association_table) + children: Mapped[List[Child]] = relationship(secondary=association_table) class Child(Base): @@ -485,7 +486,7 @@ for each :func:`_orm.relationship` specify the common association table:: __tablename__ = "left_table" id: Mapped[int] = mapped_column(primary_key=True) - children: Mapped[list[Child]] = relationship( + children: Mapped[List[Child]] = relationship( secondary=association_table, back_populates="parents" ) @@ -494,7 +495,7 @@ for each :func:`_orm.relationship` specify the common association table:: __tablename__ = "right_table" id: Mapped[int] = mapped_column(primary_key=True) - parents: Mapped[list[Parent]] = relationship( + parents: Mapped[List[Parent]] = relationship( secondary=association_table, back_populates="children" ) @@ -650,7 +651,7 @@ from ``Parent`` to ``Child`` makes explicit use of ``Association``:: class Parent(Base): __tablename__ = "left_table" id: Mapped[int] = mapped_column(primary_key=True) - children: Mapped[list["Association"]] = relationship() + children: Mapped[List["Association"]] = relationship() class Child(Base): @@ -688,13 +689,13 @@ constructs, linked to the existing ones using :paramref:`_orm.relationship.back_ class Parent(Base): __tablename__ = "left_table" id: Mapped[int] = mapped_column(primary_key=True) - children: Mapped[list["Association"]] = relationship(back_populates="parent") + children: Mapped[List["Association"]] = relationship(back_populates="parent") class Child(Base): __tablename__ = "right_table" id: Mapped[int] = mapped_column(primary_key=True) - parents: Mapped[list["Association"]] = relationship(back_populates="child") + parents: Mapped[List["Association"]] = relationship(back_populates="child") Working with the association pattern in its direct form requires that child objects are associated with an association instance before being appended to @@ -789,12 +790,12 @@ and ``Child.parent_associations -> Association.parent``:: id: Mapped[int] = mapped_column(primary_key=True) # many-to-many relationship to Child, bypassing the `Association` class - children: Mapped[list["Child"]] = relationship( + children: Mapped[List["Child"]] = relationship( secondary="association_table", back_populates="parents" ) # association between Parent -> Association -> Child - child_associations: Mapped[list["Association"]] = relationship( + child_associations: Mapped[List["Association"]] = relationship( back_populates="parent" ) @@ -805,12 +806,12 @@ and ``Child.parent_associations -> Association.parent``:: id: Mapped[int] = mapped_column(primary_key=True) # many-to-many relationship to Parent, bypassing the `Association` class - parents: Mapped[list["Parent"]] = relationship( + parents: Mapped[List["Parent"]] = relationship( secondary="association_table", back_populates="children" ) # association between Child -> Association -> Parent - parent_associations: Mapped[list["Association"]] = relationship( + parent_associations: Mapped[List["Association"]] = relationship( back_populates="child" ) @@ -858,12 +859,12 @@ additional association columns, as below:: id: Mapped[int] = mapped_column(primary_key=True) # many-to-many relationship to Child, bypassing the `Association` class - children: Mapped[list["Child"]] = relationship( + children: Mapped[List["Child"]] = relationship( secondary="association_table", back_populates="parents", viewonly=True ) # association between Parent -> Association -> Child - child_associations: Mapped[list["Association"]] = relationship( + child_associations: Mapped[List["Association"]] = relationship( back_populates="parent" ) @@ -874,12 +875,12 @@ additional association columns, as below:: id: Mapped[int] = mapped_column(primary_key=True) # many-to-many relationship to Parent, bypassing the `Association` class - parents: Mapped[list["Parent"]] = relationship( + parents: Mapped[List["Parent"]] = relationship( secondary="association_table", back_populates="children", viewonly=True ) # association between Child -> Association -> Parent - parent_associations: Mapped[list["Association"]] = relationship( + parent_associations: Mapped[List["Association"]] = relationship( back_populates="child" ) @@ -919,7 +920,7 @@ at runtime only as a string:: class Parent(Base): # ... - children: Mapped[list["Child"]] = relationship(back_populates="parent") + children: Mapped[List["Child"]] = relationship(back_populates="parent") class Child(Base): @@ -971,7 +972,7 @@ package, including expression functions like :func:`_sql.desc` and class Parent(Base): # ... - children: Mapped[list["Child"]] = relationship( + children: Mapped[List["Child"]] = relationship( order_by="desc(Child.email_address)", primaryjoin="Parent.id == Child.parent_id", ) @@ -983,7 +984,7 @@ within any of these string expressions:: class Parent(Base): # ... - children: Mapped[list["myapp.mymodel.Child"]] = relationship( + children: Mapped[List["myapp.mymodel.Child"]] = relationship( order_by="desc(myapp.mymodel.Child.email_address)", primaryjoin="myapp.mymodel.Parent.id == myapp.mymodel.Child.parent_id", ) @@ -1004,7 +1005,7 @@ name within the :class:`_orm.registry`:: class Parent(Base): # ... - children: Mapped[list["Child"]] = relationship( + children: Mapped[List["Child"]] = relationship( "myapp.mymodel.Child", order_by="desc(myapp.mymodel.Child.email_address)", primaryjoin="myapp.mymodel.Parent.id == myapp.mymodel.Child.parent_id", @@ -1018,7 +1019,7 @@ we can specify ``model1.Child`` or ``model2.Child``:: class Parent(Base): # ... - children: Mapped[list["Child"]] = relationship( + children: Mapped[List["Child"]] = relationship( "model1.Child", order_by="desc(mymodel1.Child.email_address)", primaryjoin="Parent.id == model1.Child.parent_id", @@ -1045,7 +1046,7 @@ like the following:: class Parent(Base): # ... - children: Mapped[list["Child"]] = relationship( + children: Mapped[List["Child"]] = relationship( _resolve_child_model(), order_by=lambda: desc(_resolve_child_model().email_address), primaryjoin=lambda: Parent.id == _resolve_child_model().parent_id, @@ -1151,7 +1152,7 @@ using a lambda as:: __tablename__ = "left_table" id: Mapped[int] = mapped_column(primary_key=True) - children: Mapped[list["Child"]] = relationship( + children: Mapped[List["Child"]] = relationship( "Child", secondary=lambda: association_table ) @@ -1165,7 +1166,7 @@ the :class:`.MetaData` collection:: __tablename__ = "left_table" id: Mapped[int] = mapped_column(primary_key=True) - children: Mapped[list["Child"]] = relationship(secondary="association_table") + children: Mapped[List["Child"]] = relationship(secondary="association_table") .. warning:: When passed as a string, :paramref:`_orm.relationship.secondary` argument is interpreted using Python's diff --git a/doc/build/orm/collection_api.rst b/doc/build/orm/collection_api.rst index aba0ca104c..f580b26c93 100644 --- a/doc/build/orm/collection_api.rst +++ b/doc/build/orm/collection_api.rst @@ -47,7 +47,7 @@ below where ``list`` is used:: parent_id: Mapped[int] = mapped_column(primary_key=True) # use a list - children: Mapped[list["Child"]] = relationship() + children: Mapped[List["Child"]] = relationship() class Child(Base): diff --git a/doc/build/orm/declarative_styles.rst b/doc/build/orm/declarative_styles.rst index b644ac4181..a02391004b 100644 --- a/doc/build/orm/declarative_styles.rst +++ b/doc/build/orm/declarative_styles.rst @@ -75,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): @@ -126,6 +126,7 @@ previous section, using the :meth:`_orm.registry.mapped` decorator rather than using the :class:`_orm.DeclarativeBase` superclass:: from datetime import datetime + from typing import List from typing import Optional from sqlalchemy import ForeignKey @@ -150,7 +151,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 diff --git a/doc/build/orm/extensions/associationproxy.rst b/doc/build/orm/extensions/associationproxy.rst index 0f96c955d6..54e18c55a4 100644 --- a/doc/build/orm/extensions/associationproxy.rst +++ b/doc/build/orm/extensions/associationproxy.rst @@ -52,13 +52,13 @@ exception of an extra attribute added to the ``User`` class called __tablename__ = "user" id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] = mapped_column(String(64)) - kw: Mapped[list[Keyword]] = relationship(secondary=lambda: user_keyword_table) + kw: Mapped[List[Keyword]] = relationship(secondary=lambda: user_keyword_table) def __init__(self, name: str): self.name = name # proxy the 'keyword' attribute from the 'kw' relationship - keywords: AssociationProxy[list[str]] = association_proxy("kw", "keyword") + keywords: AssociationProxy[List[str]] = association_proxy("kw", "keyword") class Keyword(Base): @@ -150,7 +150,7 @@ using a lambda as is typical:: ... # use Keyword(keyword=kw) on append() events - keywords: AssociationProxy[list[str]] = association_proxy( + keywords: AssociationProxy[List[str]] = association_proxy( "kw", "keyword", creator=lambda kw: Keyword(keyword=kw) ) @@ -203,14 +203,14 @@ collection of ``User`` to the ``.keyword`` attribute present on each id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] = mapped_column(String(64)) - user_keyword_associations: Mapped[list[UserKeywordAssociation]] = relationship( + user_keyword_associations: Mapped[List[UserKeywordAssociation]] = relationship( back_populates="user", cascade="all, delete-orphan", ) # association proxy of "user_keyword_associations" collection # to "keyword" attribute - keywords: AssociationProxy[list[Keyword]] = association_proxy( + keywords: AssociationProxy[List[Keyword]] = association_proxy( "user_keyword_associations", "keyword", creator=lambda keyword: UserKeywordAssociation(keyword=Keyword(keyword)), @@ -547,7 +547,7 @@ to a related object, as in the example mapping below:: ) # column-targeted association proxy - special_keys: AssociationProxy[list[str]] = association_proxy( + special_keys: AssociationProxy[List[str]] = association_proxy( "user_keyword_associations", "special_key" ) diff --git a/doc/build/orm/extensions/asyncio.rst b/doc/build/orm/extensions/asyncio.rst index 45da8ef36e..94aa609145 100644 --- a/doc/build/orm/extensions/asyncio.rst +++ b/doc/build/orm/extensions/asyncio.rst @@ -150,6 +150,7 @@ illustrates a complete example including mapper and session configuration:: import asyncio import datetime + from typing import List from sqlalchemy import ForeignKey from sqlalchemy import func @@ -174,7 +175,7 @@ illustrates a complete example including mapper and session configuration:: id: Mapped[int] = mapped_column(primary_key=True) data: Mapped[str] create_date: Mapped[datetime.datetime] = mapped_column(server_default=func.now()) - bs: Mapped[list[B]] = relationship() + bs: Mapped[List[B]] = relationship() class B(Base): diff --git a/doc/build/orm/inheritance.rst b/doc/build/orm/inheritance.rst index d35967e4ca..e99bc1df62 100644 --- a/doc/build/orm/inheritance.rst +++ b/doc/build/orm/inheritance.rst @@ -186,7 +186,7 @@ and ``Employee``:: __tablename__ = "company" id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] - employees: Mapped[list[Employee]] = relationship(back_populates="company") + employees: Mapped[List[Employee]] = relationship(back_populates="company") class Employee(Base): @@ -220,7 +220,7 @@ established between the ``Manager`` and ``Company`` classes:: __tablename__ = "company" id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] - managers: Mapped[list[Manager]] = relationship(back_populates="company") + managers: Mapped[List[Manager]] = relationship(back_populates="company") class Employee(Base): @@ -481,7 +481,7 @@ relationship:: __tablename__ = "company" id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] - employees: Mapped[list[Employee]] = relationship(back_populates="company") + employees: Mapped[List[Employee]] = relationship(back_populates="company") class Employee(Base): @@ -522,7 +522,7 @@ or subclasses:: __tablename__ = "company" id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] - managers: Mapped[list[Manager]] = relationship(back_populates="company") + managers: Mapped[List[Manager]] = relationship(back_populates="company") class Employee(Base): diff --git a/doc/build/orm/large_collections.rst b/doc/build/orm/large_collections.rst index 8553fae42e..afe3c506c9 100644 --- a/doc/build/orm/large_collections.rst +++ b/doc/build/orm/large_collections.rst @@ -631,7 +631,7 @@ emit a lazy load:: # ... - children: Mapped[list[MyRelatedClass]] = relationship(lazy="raise") + children: Mapped[List[MyRelatedClass]] = relationship(lazy="raise") Above, attribute access on the ``children`` collection will raise an exception if it was not previously populated. This includes read access but for diff --git a/doc/build/orm/queryguide/_inheritance_setup.rst b/doc/build/orm/queryguide/_inheritance_setup.rst index c98a83035a..e429b179f2 100644 --- a/doc/build/orm/queryguide/_inheritance_setup.rst +++ b/doc/build/orm/queryguide/_inheritance_setup.rst @@ -11,6 +11,7 @@ the :ref:`queryguide_toplevel`. .. sourcecode:: python + >>> from typing import List >>> from sqlalchemy import create_engine >>> from sqlalchemy import ForeignKey >>> from sqlalchemy.orm import DeclarativeBase @@ -26,7 +27,7 @@ the :ref:`queryguide_toplevel`. ... __tablename__ = "company" ... id: Mapped[int] = mapped_column(primary_key=True) ... name: Mapped[str] - ... employees: Mapped[list["Employee"]] = relationship(back_populates="company") + ... employees: Mapped[List["Employee"]] = relationship(back_populates="company") >>> >>> class Employee(Base): ... __tablename__ = "employee" @@ -48,7 +49,7 @@ the :ref:`queryguide_toplevel`. ... __tablename__ = "manager" ... id: Mapped[int] = mapped_column(ForeignKey("employee.id"), primary_key=True) ... manager_name: Mapped[str] - ... paperwork: Mapped[list["Paperwork"]] = relationship() + ... paperwork: Mapped[List["Paperwork"]] = relationship() ... __mapper_args__ = { ... "polymorphic_identity": "manager", ... } diff --git a/doc/build/orm/queryguide/relationships.rst b/doc/build/orm/queryguide/relationships.rst index 593fe995b2..9c621ea9ac 100644 --- a/doc/build/orm/queryguide/relationships.rst +++ b/doc/build/orm/queryguide/relationships.rst @@ -124,6 +124,8 @@ The example below illustrates the relationship example at relationship to use :ref:`selectin_eager_loading` when a SELECT statement for ``Parent`` objects is emitted:: + from typing import List + from sqlalchemy import ForeignKey from sqlalchemy.orm import DeclarativeBase from sqlalchemy.orm import Mapped @@ -139,7 +141,7 @@ statement for ``Parent`` objects is emitted:: __tablename__ = "parent" id: Mapped[int] = mapped_column(primary_key=True) - children: Mapped[list["Child"]] = relationship(lazy="selectin") + children: Mapped[List["Child"]] = relationship(lazy="selectin") class Child(Base): diff --git a/doc/build/orm/quickstart.rst b/doc/build/orm/quickstart.rst index a4531e8d34..eee9135880 100644 --- a/doc/build/orm/quickstart.rst +++ b/doc/build/orm/quickstart.rst @@ -27,6 +27,7 @@ which we will be querying from the database. This structure, known as a Python object model, as well as :term:`database metadata` that describes real SQL tables that exist, or will exist, in a particular database:: + >>> from typing import List >>> from typing import Optional >>> from sqlalchemy import ForeignKey >>> from sqlalchemy import String @@ -45,7 +46,7 @@ real SQL tables that exist, or will exist, in a particular database:: ... name: Mapped[str] = mapped_column(String(30)) ... fullname: Mapped[Optional[str]] ... - ... addresses: Mapped[list["Address"]] = relationship( + ... addresses: Mapped[List["Address"]] = relationship( ... back_populates="user", cascade="all, delete-orphan" ... ) ... diff --git a/doc/build/tutorial/orm_related_objects.rst b/doc/build/tutorial/orm_related_objects.rst index bd1fae131c..902881c782 100644 --- a/doc/build/tutorial/orm_related_objects.rst +++ b/doc/build/tutorial/orm_related_objects.rst @@ -34,7 +34,7 @@ and other directives: # ... mapped_column() mappings - addresses: Mapped[list["Address"]] = relationship(back_populates="user") + addresses: Mapped[List["Address"]] = relationship(back_populates="user") class Address(Base): @@ -400,7 +400,7 @@ the :paramref:`_orm.relationship.lazy` option, e.g.: class User(Base): __tablename__ = "user_account" - addresses: Mapped[list["Address"]] = relationship( + addresses: Mapped[List["Address"]] = relationship( back_populates="user", lazy="selectin" ) @@ -629,7 +629,7 @@ relationship will never try to emit SQL: >>> class User(Base): ... __tablename__ = "user_account" ... id: Mapped[int] = mapped_column(primary_key=True) - ... addresses: Mapped[list["Address"]] = relationship( + ... addresses: Mapped[List["Address"]] = relationship( ... back_populates="user", lazy="raise_on_sql" ... ) -- 2.47.3