]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
read from cls.__dict__ so init_subclass works
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 4 Apr 2022 23:01:54 +0000 (19:01 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 13 Apr 2022 13:06:24 +0000 (09:06 -0400)
Modified the :class:`.DeclarativeMeta` metaclass to pass ``cls.__dict__``
into the declarative scanning process to look for attributes, rather than
the separate dictionary passed to the type's ``__init__()`` method. This
allows user-defined base classes that add attributes within an
``__init_subclass__()`` to work as expected, as ``__init_subclass__()`` can
only affect the ``cls.__dict__`` itself and not the other dictionary. This
is technically a regression from 1.3 where ``__dict__`` was being used.

Additionally makes the reference between ClassManager and
the declarative configuration object a weak reference, so that it
can be discarded after mappers are set up.

Fixes: #7900
Change-Id: I3c2fd4e227cc1891aa4bb3d7d5b43d5686f9f27c
(cherry picked from commit 428ea01f00a9cc7f85e435018565eb6da7af1b77)

doc/build/changelog/unreleased_14/7900.rst [new file with mode: 0644]
lib/sqlalchemy/orm/decl_api.py
lib/sqlalchemy/orm/decl_base.py
lib/sqlalchemy/orm/instrumentation.py
test/orm/declarative/test_mixin.py

diff --git a/doc/build/changelog/unreleased_14/7900.rst b/doc/build/changelog/unreleased_14/7900.rst
new file mode 100644 (file)
index 0000000..9d6d507
--- /dev/null
@@ -0,0 +1,14 @@
+.. change::
+    :tags: bug, orm, declarative
+    :tickets: 7900
+
+    Modified the :class:`.DeclarativeMeta` metaclass to pass ``cls.__dict__``
+    into the declarative scanning process to look for attributes, rather than
+    the separate dictionary passed to the type's ``__init__()`` method. This
+    allows user-defined base classes that add attributes within an
+    ``__init_subclass__()`` to work as expected, as ``__init_subclass__()`` can
+    only affect the ``cls.__dict__`` itself and not the other dictionary. This
+    is technically a regression from 1.3 where ``__dict__`` was being used.
+
+
+
index 4b6c710c72d334105dc7bf463e31e2a5f279961b..16f91c69ddb62b24271ed2c528023290d720cd4b 100644 (file)
@@ -54,6 +54,10 @@ def has_inherited_table(cls):
 
 class DeclarativeMeta(type):
     def __init__(cls, classname, bases, dict_, **kw):
+        # use cls.__dict__, which can be modified by an
+        # __init_subclass__() method (#7900)
+        dict_ = cls.__dict__
+
         # early-consume registry from the initial declarative base,
         # assign privately to not conflict with subclass attributes named
         # "registry"
@@ -228,7 +232,8 @@ class declared_attr(interfaces._MappedAttribute, property):
 
         # here, we are inside of the declarative scan.  use the registry
         # that is tracking the values of these attributes.
-        declarative_scan = manager.declarative_scan
+        declarative_scan = manager.declarative_scan()
+        assert declarative_scan is not None
         reg = declarative_scan.declared_attr_reg
 
         if desc in reg:
index 6f02e569774966ac6db70042336ad62579827bd5..ed4ccd19682f341bde760d74799ec71e83763766 100644 (file)
@@ -152,7 +152,13 @@ def _check_declared_props_nocascade(obj, name, cls):
 
 
 class _MapperConfig(object):
-    __slots__ = ("cls", "classname", "properties", "declared_attr_reg")
+    __slots__ = (
+        "cls",
+        "classname",
+        "properties",
+        "declared_attr_reg",
+        "__weakref__",
+    )
 
     @classmethod
     def setup_mapping(cls, registry, cls_, dict_, table, mapper_kw):
@@ -300,9 +306,12 @@ class _ClassScanMapperConfig(_MapperConfig):
         mapper_kw,
     ):
 
+        # grab class dict before the instrumentation manager has been added.
+        # reduces cycles
+        self.dict_ = dict(dict_) if dict_ else {}
+
         super(_ClassScanMapperConfig, self).__init__(registry, cls_, mapper_kw)
 
-        self.dict_ = dict(dict_) if dict_ else {}
         self.persist_selectable = None
         self.declared_columns = set()
         self.column_copies = {}
index 97692b6421cc1968b88f7bb5f0a6f27b1ea5a385..a7023a21d988c25de5b24cdbb9f89970fdee8311 100644 (file)
@@ -30,6 +30,8 @@ alternate instrumentation forms.
 """
 
 
+import weakref
+
 from . import base
 from . import collections
 from . import exc
@@ -131,7 +133,7 @@ class ClassManager(HasMemoized, dict):
         if registry:
             registry._add_manager(self)
         if declarative_scan:
-            self.declarative_scan = declarative_scan
+            self.declarative_scan = weakref.ref(declarative_scan)
         if expired_attribute_loader:
             self.expired_attribute_loader = expired_attribute_loader
 
index 5a4673a23eda92e0e3cc33516b0e9fc4009fc409..78ab4dbfc3e12b830b27fd5d4127025bfeae8396 100644 (file)
@@ -38,7 +38,11 @@ Base = None
 mapper_registry = None
 
 
-class DeclarativeTestBase(fixtures.TestBase, testing.AssertsExecutionResults):
+class DeclarativeTestBase(
+    testing.AssertsCompiledSQL,
+    fixtures.TestBase,
+    testing.AssertsExecutionResults,
+):
     def setup_test(self):
         global Base, mapper_registry
 
@@ -53,6 +57,20 @@ class DeclarativeTestBase(fixtures.TestBase, testing.AssertsExecutionResults):
 
 
 class DeclarativeMixinTest(DeclarativeTestBase):
+    @testing.requires.python3
+    def test_init_subclass_works(self, registry):
+        class Base:
+            def __init_subclass__(cls):
+                cls.id = Column(Integer, primary_key=True)
+
+        Base = registry.generate_base(cls=Base)
+
+        class Foo(Base):
+            __tablename__ = "foo"
+            name = Column(String)
+
+        self.assert_compile(select(Foo), "SELECT foo.name, foo.id FROM foo")
+
     def test_simple_wbase(self):
         class MyMixin(object):