]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix missing class attributes when using __class_getitem__
authorKai Mueller <15907922+kasium@users.noreply.github.com>
Fri, 17 Dec 2021 20:10:05 +0000 (20:10 +0000)
committerKai Mueller <15907922+kasium@users.noreply.github.com>
Fri, 17 Dec 2021 20:10:05 +0000 (20:10 +0000)
Closes #7462

doc/build/changelog/unreleased_14/7462.rst [new file with mode: 0644]
lib/sqlalchemy/orm/decl_api.py
test/orm/declarative/test_typing_py3k.py

diff --git a/doc/build/changelog/unreleased_14/7462.rst b/doc/build/changelog/unreleased_14/7462.rst
new file mode 100644 (file)
index 0000000..3361cf2
--- /dev/null
@@ -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.
index ed8617d52fee2716501cb8adf441866c6e5ae8ce..e44ca111a79499d880755f6a6bc716b2fe218baa 100644 (file)
@@ -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)
 
index 823fe54f106bdaa94cd261bd75688267f78abbc8..2e8d78d379f585057a4d73c31add3da7561bd043 100644 (file)
@@ -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