From: Jason Kirtland Date: Sat, 15 Mar 2008 23:13:35 +0000 (+0000) Subject: Issue a warning when a declarative detects a likely trailing comma: foo = Column... X-Git-Tag: rel_0_4_5~93 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9e15993083c9213fd900e6d92c571367c6b6e1ae;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Issue a warning when a declarative detects a likely trailing comma: foo = Column(foo), --- diff --git a/lib/sqlalchemy/ext/declarative.py b/lib/sqlalchemy/ext/declarative.py index 4feb474566..41a51c8c69 100644 --- a/lib/sqlalchemy/ext/declarative.py +++ b/lib/sqlalchemy/ext/declarative.py @@ -159,6 +159,12 @@ class DeclarativeMeta(type): our_stuff = {} for k in dict_: value = dict_[k] + if (isinstance(value, tuple) and len(value) == 1 and + isinstance(value[0], (Column, MapperProperty))): + util.warn("Ignoring declarative-like tuple value of attribute " + "%s: possibly a copy-and-paste error with a comma " + "left at the end of the line?" % k) + continue if not isinstance(value, (Column, MapperProperty)): continue prop = _deferred_relation(cls, value) diff --git a/test/ext/declarative.py b/test/ext/declarative.py index ef5fc87c6c..fcb0125ac8 100644 --- a/test/ext/declarative.py +++ b/test/ext/declarative.py @@ -3,6 +3,7 @@ import testenv; testenv.configure_for_tests() from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base, declared_synonym +from sqlalchemy import exceptions from testlib.fixtures import Base as Fixture from testlib import * @@ -50,6 +51,21 @@ class DeclarativeTest(TestBase): self.assertEquals(a1, Address(email='two')) self.assertEquals(a1.user, User(name='u1')) + @testing.emits_warning('Ignoring declarative-like tuple value of ' + 'attribute id') + def test_oops(self): + def define(): + class User(Base, Fixture): + __tablename__ = 'users' + + id = Column('id', Integer, primary_key=True), + name = Column('name', String(50)) + assert False + self.assertRaisesMessage( + exceptions.ArgumentError, + "Mapper Mapper|User|users could not assemble any primary key", + define) + def test_expression(self): class User(Base, Fixture): __tablename__ = 'users'