--- /dev/null
+.. change::
+ :tags: bug, orm, mypy
+ :tickets: 7368
+
+ Fixed issue where the :func:`_orm.as_declarative` decorator and similar
+ functions used to generate the declarative base class would not copy the
+ ``__class_getitem__()`` method from a given superclass, which prevented the
+ use of pep-484 generics in conjunction with the ``Base`` class. Pull
+ request courtesy Kai Mueller.
class_dict["__abstract__"] = True
if mapper:
class_dict["__mapper_cls__"] = mapper
+ if hasattr(cls, "__class_getitem__"):
+ class_dict["__class_getitem__"] = cls.__class_getitem__
return metaclass(name, bases, class_dict)
--- /dev/null
+from typing import Generic
+from typing import Type
+from typing import TypeVar
+
+from sqlalchemy import Column
+from sqlalchemy import Integer
+from sqlalchemy.orm import as_declarative
+from sqlalchemy.testing import fixtures
+
+
+class DeclarativeBaseTest(fixtures.TestBase):
+ def test_class_getitem(self):
+ T = TypeVar("T", bound="CommonBase") # noqa
+
+ class CommonBase(Generic[T]):
+ @classmethod
+ def boring(cls: Type[T]) -> Type[T]:
+ return cls
+
+ @as_declarative()
+ class Base(CommonBase[T]):
+ pass
+
+ class Tab(Base["Tab"]):
+ a = Column(Integer, primary_key=True)