]> 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:43:06 +0000 (16:43 -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

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 31b5e492c4d957c14e8c1e9057a86e75e6af1d85..cb3ec3cdbdf210cfecde2e210e145b0575cdc726 100644 (file)
@@ -69,7 +69,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 7dfcc70bbb5cf1a1896cb87ea5163d7f17e189c0..1c03f7bd9060e8aab79195d4a93d10f4142a5e94 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
@@ -2131,6 +2132,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):