From: Mike Bayer Date: Thu, 12 Dec 2013 18:30:16 +0000 (-0500) Subject: - :func:`.composite` will raise an informative error message when the X-Git-Tag: rel_0_8_5~79 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=730f585dc6878f4ab5575f847bac0d29f02686f2;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - :func:`.composite` will raise an informative error message when the columns/attribute (names) passed don't resolve to a Column or mapped attribute (such as an erroneous tuple); previously raised an unbound local. [ticket:2889] Conflicts: test/orm/test_composites.py --- diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index b65dd77cd9..6ca5a6e698 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -11,6 +11,16 @@ .. changelog:: :version: 0.8.5 + .. change:: + :tags: bug, orm + :versions: 0.9.0b2 + :tickets: 2889 + + :func:`.composite` will raise an informative error message when the + columns/attribute (names) passed don't resolve to a Column or mapped + attribute (such as an erroneous tuple); previously raised an unbound + local. + .. change:: :tags: bug, declarative :versions: 0.9.0b2 diff --git a/lib/sqlalchemy/orm/descriptor_props.py b/lib/sqlalchemy/orm/descriptor_props.py index e4c0d1525a..ea38182fb0 100644 --- a/lib/sqlalchemy/orm/descriptor_props.py +++ b/lib/sqlalchemy/orm/descriptor_props.py @@ -190,6 +190,11 @@ class CompositeProperty(DescriptorProperty): prop = self.parent._columntoproperty[attr] elif isinstance(attr, attributes.InstrumentedAttribute): prop = attr.property + else: + raise sa_exc.ArgumentError( + "Composite expects Column or Column-bound " + "attributes/attribute names as arguments, got: %r" + % (attr,)) props.append(prop) @property diff --git a/test/orm/test_composites.py b/test/orm/test_composites.py index 9b582ce17a..5c9dbbd175 100644 --- a/test/orm/test_composites.py +++ b/test/orm/test_composites.py @@ -6,7 +6,7 @@ from sqlalchemy import MetaData, Integer, String, ForeignKey, func, \ from sqlalchemy.testing.schema import Table, Column from sqlalchemy.orm import mapper, relationship, backref, \ class_mapper, CompositeProperty, \ - validates, aliased + validates, aliased, configure_mappers from sqlalchemy.orm import attributes, \ composite, relationship, \ Session @@ -712,6 +712,24 @@ class ConfigurationTest(fixtures.MappedTest): }) self._test_roundtrip() + def test_check_prop_type(self): + edge, Edge, Point = (self.tables.edge, + self.classes.Edge, + self.classes.Point) + mapper(Edge, edge, properties={ + 'start': sa.orm.composite(Point, (edge.c.x1,), edge.c.y1), + }) + assert_raises_message( + sa.exc.ArgumentError, + # note that we also are checking that the tuple + # renders here, so the "%" operator in the string needs to + # apply the tuple also + r"Composite expects Column or Column-bound " + "attributes/attribute names as " + "arguments, got: \(Column", + configure_mappers + ) + class ComparatorTest(fixtures.MappedTest, testing.AssertsCompiledSQL): __dialect__ = 'default'