]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
set type of type_of to be same as input argument
authorWouter Kayser <wouterkayser@gmail.com>
Sun, 23 Jun 2024 10:18:05 +0000 (06:18 -0400)
committerFederico Caselli <cfederico87@gmail.com>
Tue, 25 Jun 2024 20:08:08 +0000 (22:08 +0200)
Fixes: #11371
Fixes the of_type method so that it does not return a class with unset generic.
See the original issue for a more detailed explanation.

Closes: #11416
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/11416
Pull-request-sha: ed8d0edebb6b84b9dcffcf24c52f113c37e7fedd

Change-Id: I35637491d6d9c573825f6d13299712626dd521c5

lib/sqlalchemy/orm/attributes.py
test/typing/plain_files/orm/relationship.py

index 5b16ce3d6b32bbcd53901a55859098e28f49a481..33cca564927e7d96e3ae001068ef74add5f132b6 100644 (file)
@@ -401,7 +401,7 @@ class QueryableAttribute(
             parententity=adapt_to_entity,
         )
 
-    def of_type(self, entity: _EntityType[Any]) -> QueryableAttribute[_T]:
+    def of_type(self, entity: _EntityType[_T]) -> QueryableAttribute[_T]:
         return QueryableAttribute(
             self.class_,
             self.key,
index 5caf57de7bd69b9115fe5c474f6e04bdf910e66c..683e347f19f153436759140dae4254e2179fa33e 100644 (file)
@@ -106,6 +106,30 @@ class SelfReferential(Base):
     )
 
 
+class Employee(Base):
+    __tablename__ = "employee"
+    id: Mapped[int] = mapped_column(primary_key=True)
+    team_id: Mapped[int] = mapped_column(ForeignKey("team.id"))
+    team: Mapped["Team"] = relationship(back_populates="employees")
+
+    __mapper_args__ = {
+        "polymorphic_on": "type",
+        "polymorphic_identity": "employee",
+    }
+
+
+class Team(Base):
+    __tablename__ = "team"
+    id: Mapped[int] = mapped_column(primary_key=True)
+    employees: Mapped[list[Employee]] = relationship("Employee")
+
+
+class Engineer(Employee):
+    engineer_info: Mapped[str]
+
+    __mapper_args__ = {"polymorphic_identity": "engineer"}
+
+
 if typing.TYPE_CHECKING:
     # EXPECTED_RE_TYPE: sqlalchemy.*.InstrumentedAttribute\[Union\[builtins.str, None\]\]
     reveal_type(User.extra)
@@ -137,6 +161,9 @@ if typing.TYPE_CHECKING:
     # EXPECTED_RE_TYPE: sqlalchemy.*.InstrumentedAttribute\[builtins.set\*?\[relationship.MoreMail\]\]
     reveal_type(Address.rel_style_one_anno_only)
 
+    # EXPECTED_RE_TYPE: sqlalchemy.*.QueryableAttribute\[relationship.Engineer\]
+    reveal_type(Team.employees.of_type(Engineer))
+
 
 mapper_registry: registry = registry()