diff options
author | mike bayer <mike_mp@zzzcomputing.com> | 2018-11-27 07:55:07 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@bbpush.zzzcomputing.com> | 2018-11-27 07:55:07 +0000 |
commit | bfa44f6a15582ef801cea53f89201f3cb3d329bf (patch) | |
tree | ba36f434030daaad4c6c565b51e92b2b46d6f9ca | |
parent | d04575c55467da57ad7091615b313db2486908b6 (diff) | |
parent | 9e53163dbdee113f9c84311ca5804a16ac82fbf7 (diff) | |
download | sqlalchemy-bfa44f6a15582ef801cea53f89201f3cb3d329bf.tar.gz |
Merge "Warn for lower-case column attribute on declarative" into rel_1_2
-rw-r--r-- | doc/build/changelog/unreleased_12/4374.rst | 7 | ||||
-rw-r--r-- | lib/sqlalchemy/ext/declarative/base.py | 14 | ||||
-rw-r--r-- | test/ext/declarative/test_basic.py | 57 |
3 files changed, 78 insertions, 0 deletions
diff --git a/doc/build/changelog/unreleased_12/4374.rst b/doc/build/changelog/unreleased_12/4374.rst new file mode 100644 index 000000000..716ff5b67 --- /dev/null +++ b/doc/build/changelog/unreleased_12/4374.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: bug, orm, declarative + :tickets: 4374 + + A warning is emitted in the case that a :func:`.column` object is applied to + a declarative class, as it seems likely this intended to be a + :class:`.Column` object. diff --git a/lib/sqlalchemy/ext/declarative/base.py b/lib/sqlalchemy/ext/declarative/base.py index f2b708f9f..a41a46848 100644 --- a/lib/sqlalchemy/ext/declarative/base.py +++ b/lib/sqlalchemy/ext/declarative/base.py @@ -303,6 +303,11 @@ class _MapperConfig(object): if isinstance(ret, (Column, MapperProperty)) and \ ret.doc is None: ret.doc = obj.__doc__ + # here, the attribute is some other kind of property that + # we assume is not part of the declarative mapping. + # however, check for some more common mistakes + else: + self._warn_for_decl_attributes(base, name, obj) if inherited_table_args and not tablename: table_args = None @@ -311,6 +316,14 @@ class _MapperConfig(object): self.tablename = tablename self.mapper_args_fn = mapper_args_fn + def _warn_for_decl_attributes(self, cls, key, c): + if isinstance(c, expression.ColumnClause): + util.warn( + "Attribute '%s' on class %s appears to be a non-schema " + "'sqlalchemy.sql.column()' " + "object; this won't be part of the declarative mapping" % + (key, cls)) + def _produce_column_copies(self, base): cls = self.cls dict_ = self.dict_ @@ -382,6 +395,7 @@ class _MapperConfig(object): # and place the evaluated value onto the class. if not k.startswith('__'): dict_.pop(k) + self._warn_for_decl_attributes(cls, k, value) if not late_mapped: setattr(cls, k, value) continue diff --git a/test/ext/declarative/test_basic.py b/test/ext/declarative/test_basic.py index 5d1655e87..3eb0743e7 100644 --- a/test/ext/declarative/test_basic.py +++ b/test/ext/declarative/test_basic.py @@ -1,6 +1,7 @@ from sqlalchemy.testing import eq_, assert_raises, \ assert_raises_message, expect_warnings, is_ +from sqlalchemy.testing import assertions from sqlalchemy.ext import declarative as decl from sqlalchemy import exc import sqlalchemy as sa @@ -174,6 +175,62 @@ class DeclarativeTest(DeclarativeTestBase): assert class_mapper(Bar).get_property('some_data').columns[0] \ is t.c.data + def test_lower_case_c_column_warning(self): + with assertions.expect_warnings( + r"Attribute 'x' on class <class .*Foo.* appears to be a " + r"non-schema 'sqlalchemy.sql.column\(\)' object; ", + ): + class Foo(Base): + __tablename__ = 'foo' + + id = Column(Integer, primary_key=True) + x = sa.sql.expression.column(Integer) + y = Column(Integer) + + class MyMixin(object): + x = sa.sql.expression.column(Integer) + y = Column(Integer) + + with assertions.expect_warnings( + r"Attribute 'x' on class <class .*MyMixin.* appears to be a " + r"non-schema 'sqlalchemy.sql.column\(\)' object; ", + ): + class Foo2(MyMixin, Base): + __tablename__ = 'foo2' + + id = Column(Integer, primary_key=True) + + with assertions.expect_warnings( + r"Attribute 'x' on class <class .*Foo3.* appears to be a " + r"non-schema 'sqlalchemy.sql.column\(\)' object; ", + ): + class Foo3(Base): + __tablename__ = 'foo3' + + id = Column(Integer, primary_key=True) + + @declared_attr + def x(cls): + return sa.sql.expression.column(Integer) + + y = Column(Integer) + + with assertions.expect_warnings( + r"Attribute 'x' on class <class .*Foo4.* appears to be a " + r"non-schema 'sqlalchemy.sql.column\(\)' object; ", + ): + class MyMixin2(object): + @declared_attr + def x(cls): + return sa.sql.expression.column(Integer) + + y = Column(Integer) + + class Foo4(MyMixin2, Base): + __tablename__ = 'foo4' + + id = Column(Integer, primary_key=True) + def test_column_named_twice(self): def go(): class Foo(Base): |