]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Don't warn for mixin-based __table_args__, __mapper_args__ declared_attr
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 21 Mar 2018 13:59:46 +0000 (09:59 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 21 Mar 2018 17:08:19 +0000 (13:08 -0400)
Removed a warning that would be emitted when calling upon
``__table_args__``, ``__mapper_args__`` as named with a ``@declared_attr``
method, when called from a non-mapped declarative mixin.  Calling these
directly is documented as the approach to use when one is overidding one
of these methods on a mapped class.  The warning still emits for regular
attribute names.

Change-Id: Iae7ed0bd625a2c163c910aa777cef4779128580a
Fixes: #4221
doc/build/changelog/unreleased_12/4221.rst [new file with mode: 0644]
lib/sqlalchemy/ext/declarative/api.py
test/ext/declarative/test_mixin.py

diff --git a/doc/build/changelog/unreleased_12/4221.rst b/doc/build/changelog/unreleased_12/4221.rst
new file mode 100644 (file)
index 0000000..3ce722a
--- /dev/null
@@ -0,0 +1,10 @@
+.. change::
+    :tags: bug, declarative
+    :tickets: 4221
+
+    Removed a warning that would be emitted when calling upon
+    ``__table_args__``, ``__mapper_args__`` as named with a ``@declared_attr``
+    method, when called from a non-mapped declarative mixin.  Calling these
+    directly is documented as the approach to use when one is overidding one
+    of these methods on a mapped class.  The warning still emits for regular
+    attribute names.
index 77a03bc4abd85d59c61d4100c9db423c917f0240..b08d3ce30c20b2416b817e9518cc247d54cd449c 100644 (file)
@@ -17,6 +17,7 @@ from ...util import OrderedDict, hybridmethod, hybridproperty
 from ... import util
 from ... import exc
 import weakref
+import re
 
 from .base import _as_declarative, \
     _declarative_constructor,\
@@ -189,8 +190,8 @@ class declared_attr(interfaces._MappedAttribute, property):
     def __get__(desc, self, cls):
         reg = cls.__dict__.get('_sa_declared_attr_reg', None)
         if reg is None:
-            manager = attributes.manager_of_class(cls)
-            if manager is None:
+            if not re.match(r'^__.+__$', desc.fget.__name__) and \
+                    attributes.manager_of_class(cls) is None:
                 util.warn(
                     "Unmanaged access of declarative attribute %s from "
                     "non-mapped class %s" %
index 0b1b117fbc2b9eb24ba91027929984cf17f9e779..07c790dc426320a7e08287ab43193cba18fbd24c 100644 (file)
@@ -1540,6 +1540,19 @@ class DeclaredAttrTest(DeclarativeTestBase, testing.AssertsCompiledSQL):
             getattr, Mixin, "my_prop"
         )
 
+    def test_can_we_access_the_mixin_straight_special_names(self):
+        class Mixin(object):
+            @declared_attr
+            def __table_args__(cls):
+                return (1, 2, 3)
+
+            @declared_attr
+            def __arbitrary__(cls):
+                return (4, 5, 6)
+
+        eq_(Mixin.__table_args__, (1, 2, 3))
+        eq_(Mixin.__arbitrary__, (4, 5, 6))
+
     def test_non_decl_access(self):
         counter = mock.Mock()