From 98cae9e4309647f4e22c59a2192966fa2d4d8e38 Mon Sep 17 00:00:00 2001 From: Kai Mueller <15907922+kasium@users.noreply.github.com> Date: Fri, 17 Dec 2021 20:10:05 +0000 Subject: [PATCH] Fix missing class attributes when using __class_getitem__ Closes #7462 --- doc/build/changelog/unreleased_14/7462.rst | 7 +++++++ lib/sqlalchemy/orm/decl_api.py | 2 +- test/orm/declarative/test_typing_py3k.py | 7 ++++++- 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 doc/build/changelog/unreleased_14/7462.rst diff --git a/doc/build/changelog/unreleased_14/7462.rst b/doc/build/changelog/unreleased_14/7462.rst new file mode 100644 index 0000000000..3361cf2b5a --- /dev/null +++ b/doc/build/changelog/unreleased_14/7462.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: bug, orm, mypy + :tickets: 7368 + + Fixed issue where the ``__class_getitem__()`` method of the generated declarative base class by + :func:`_orm.as_declarative` lead to inaccessible class attributes such as ``__table__``. + Pull request courtesy Kai Mueller. diff --git a/lib/sqlalchemy/orm/decl_api.py b/lib/sqlalchemy/orm/decl_api.py index ed8617d52f..e44ca111a7 100644 --- a/lib/sqlalchemy/orm/decl_api.py +++ b/lib/sqlalchemy/orm/decl_api.py @@ -788,7 +788,7 @@ class registry: if mapper: class_dict["__mapper_cls__"] = mapper if hasattr(cls, "__class_getitem__"): - class_dict["__class_getitem__"] = cls.__class_getitem__ + class_dict["__class_getitem__"] = lambda cls, _: cls return metaclass(name, bases, class_dict) diff --git a/test/orm/declarative/test_typing_py3k.py b/test/orm/declarative/test_typing_py3k.py index 823fe54f10..2e8d78d379 100644 --- a/test/orm/declarative/test_typing_py3k.py +++ b/test/orm/declarative/test_typing_py3k.py @@ -19,7 +19,12 @@ class DeclarativeBaseTest(fixtures.TestBase): @as_declarative() class Base(CommonBase[T]): - pass + foo = 1 class Tab(Base["Tab"]): + __tablename__ = "foo" a = Column(Integer, primary_key=True) + + assert Tab.foo == 1 + assert Tab.__table__ is not None + assert Tab.boring() == Tab -- 2.47.3