From: Mike Bayer Date: Fri, 9 Sep 2011 20:18:44 +0000 (-0400) Subject: - Calling class_mapper() and passing in an object X-Git-Tag: rel_0_7_3~56 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=bb978f2e0a4c455b28825aed3fcc80bf70f09c49;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Calling class_mapper() and passing in an object that is not a "type" (i.e. a class that could potentially be mapped) now raises an informative ArgumentError, rather than UnmappedClassError. [ticket:2196] --- diff --git a/CHANGES b/CHANGES index 11f1173472..aac1a7a4a7 100644 --- a/CHANGES +++ b/CHANGES @@ -43,6 +43,12 @@ CHANGES would hit the __eq__() and fail. [ticket:2260] Does not apply to 0.6.9. + - Calling class_mapper() and passing in an object + that is not a "type" (i.e. a class that could + potentially be mapped) now raises an informative + ArgumentError, rather than UnmappedClassError. + [ticket:2196] + -sql - Behavioral improvement: empty conjunctions such as and_() and or_() will be diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py index d57b04f0cd..3dc1f86763 100644 --- a/lib/sqlalchemy/orm/util.py +++ b/lib/sqlalchemy/orm/util.py @@ -555,9 +555,12 @@ def object_mapper(instance): raise exc.UnmappedInstanceError(instance) def class_mapper(class_, compile=True): - """Given a class, return the primary Mapper associated with the key. + """Given a class, return the primary :class:`.Mapper` associated + with the key. - Raises UnmappedClassError if no mapping is configured. + Raises :class:`.UnmappedClassError` if no mapping is configured + on the given class, or :class:`.ArgumentError` if a non-class + object is passed. """ @@ -566,6 +569,8 @@ def class_mapper(class_, compile=True): mapper = class_manager.mapper except exc.NO_STATE: + if not isinstance(class_, type): + raise sa_exc.ArgumentError("Class object expected, got '%r'." % class_) raise exc.UnmappedClassError(class_) if compile and mapperlib.module._new_mappers: diff --git a/test/orm/test_mapper.py b/test/orm/test_mapper.py index ce729ed6bf..90ad2d2158 100644 --- a/test/orm/test_mapper.py +++ b/test/orm/test_mapper.py @@ -1374,7 +1374,17 @@ class MapperTest(_fixtures.FixtureTest): 'addresses':relationship(Address) }) - assert_raises(sa.orm.exc.UnmappedClassError, sa.orm.configure_mappers) + assert_raises_message( + sa.orm.exc.UnmappedClassError, + "Class 'test.orm._fixtures.Address' is not mapped", + sa.orm.configure_mappers) + + def test_unmapped_not_type_error(self): + assert_raises_message( + sa.exc.ArgumentError, + "Class object expected, got '5'.", + class_mapper, 5 + ) def test_unmapped_subclass_error_postmap(self): users = self.tables.users