]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add **kw support to DeclarativeMeta.__init__
authorEwenGillies <elg112@ic.ac.uk>
Sun, 5 Jul 2020 14:50:50 +0000 (10:50 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 6 Jul 2020 20:46:33 +0000 (16:46 -0400)
Added a ``**kw`` argument to the :meth:`.DeclarativeMeta.__init__` method.
This allows a class to support the :pep:`487` metaclass hook
``__init_subclass__``.  Pull request courtesy Ewen Gillies.

Fixes: #5357
Closes: #5363
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5363
Pull-request-sha: 0ad05a768316cba03a4d312ab39d3e8fbca7ac54

Change-Id: I1654befe9eb1c8b8e7fc0784bdbe64284614f0ea
(cherry picked from commit 67504137e96547664754691bdd3269b473a488d1)

doc/build/changelog/unreleased_13/5357.rst [new file with mode: 0644]
lib/sqlalchemy/ext/declarative/api.py
test/ext/declarative/test_basic.py

diff --git a/doc/build/changelog/unreleased_13/5357.rst b/doc/build/changelog/unreleased_13/5357.rst
new file mode 100644 (file)
index 0000000..4503b13
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: usecase, py3k
+    :tickets: #5357
+
+    Added a ``**kw`` argument to the :meth:`.DeclarativeMeta.__init__` method.
+    This allows a class to support the :pep:`487` metaclass hook
+    ``__init_subclass__``.  Pull request courtesy Ewen Gillies.
+
index b59a03618158859b1eb749e13cfcd9352f50ad1c..e900a3beab4934a79074a3f578e7f049a68b103d 100644 (file)
@@ -70,7 +70,7 @@ def has_inherited_table(cls):
 
 
 class DeclarativeMeta(type):
-    def __init__(cls, classname, bases, dict_):
+    def __init__(cls, classname, bases, dict_, **kw):
         if "_decl_class_registry" not in cls.__dict__:
             _as_declarative(cls, classname, cls.__dict__)
         type.__init__(cls, classname, bases, dict_)
index 336c919a887fe60c27ce574ef9c573d6e0df9294..ff4b2398f942a4ad60af94e91ea47e6230414307 100644 (file)
@@ -13,6 +13,7 @@ from sqlalchemy import testing
 from sqlalchemy import UniqueConstraint
 from sqlalchemy import util
 from sqlalchemy.ext import declarative as decl
+from sqlalchemy.ext.declarative import DeclarativeMeta
 from sqlalchemy.ext.declarative import declared_attr
 from sqlalchemy.ext.declarative import synonym_for
 from sqlalchemy.ext.declarative.base import _DeferredMapperConfig
@@ -2127,6 +2128,33 @@ class DeclarativeTest(DeclarativeTestBase):
 
         assert not hasattr(Foo, "data_hybrid")
 
+    @testing.requires.python3
+    def test_kw_support_in_declarative_meta_init(self):
+        # This will not fail if DeclarativeMeta __init__ supports **kw
+
+        class BaseUser(Base):
+            __tablename__ = "base"
+            id_ = Column(Integer, primary_key=True)
+
+            @classmethod
+            def __init_subclass__(cls, random_keyword=False, **kw):
+                super().__init_subclass__(**kw)
+                cls._set_random_keyword_used_here = random_keyword
+
+        class User(BaseUser):
+            __tablename__ = "user"
+            id_ = Column(Integer, ForeignKey("base.id_"), primary_key=True)
+
+        # Check the default option
+        eq_(User._set_random_keyword_used_here, False)
+
+        # Build the metaclass with a keyword!
+        bases = (BaseUser,)
+        UserType = DeclarativeMeta("UserType", bases, {}, random_keyword=True)
+
+        # Check to see if __init_subclass__ works in supported versions
+        eq_(UserType._set_random_keyword_used_here, True)
+
 
 def _produce_test(inline, stringbased):
     class ExplicitJoinTest(fixtures.MappedTest):