Commits

Mike Bayer  committed ce3cbe6

warn when a subclass' base uses @declared_attr for a regular column -
does not propagate to subclasses. [ticket:2283]

  • Participants
  • Parent commits ae15b58

Comments (0)

Files changed (2)

File lib/sqlalchemy/ext/declarative.py

                     if base is not cls:
                         inherited_table_args = True
             elif class_mapped:
+                if isinstance(obj, declarative_props):
+                    util.warn("Regular (i.e. not __special__) "
+                            "attribute '%s.%s' uses @declared_attr, "
+                            "but owning class %s is mapped - "
+                            "not applying to subclass %s." 
+                            % (base.__name__, name, base, cls))
                 continue
             elif base is not cls:
                 # we're a mixin.

File test/ext/test_declarative.py

         eq_(sess.query(User).all(), [User(name='u1', address_count=2,
             addresses=[Address(email='one'), Address(email='two')])])
 
+    def test_useless_declared_attr_warns_on_subclass(self):
+        def go():
+            class MyBase(Base):
+                __tablename__ = 'foo'
+                id = Column(Integer, primary_key=True)
+                @declared_attr
+                def somecol(cls):
+                    return Column(Integer)
+
+            class MyClass(MyBase):
+                __tablename__ = 'bar'
+        assert_raises_message(
+            sa.exc.SAWarning,
+            r"Regular \(i.e. not __special__\) attribute 'MyBase.somecol' "
+            "uses @declared_attr, but owning class "
+            "<class 'test.ext.test_declarative.MyBase'> is "
+            "mapped - not applying to subclass <class "
+            "'test.ext.test_declarative.MyClass'>.",
+            go
+        )
+
     def test_column(self):
 
         class User(Base, fixtures.ComparableEntity):