diff options
-rw-r--r-- | CHANGES | 6 | ||||
-rw-r--r-- | lib/sqlalchemy/orm/util.py | 9 | ||||
-rw-r--r-- | test/orm/test_mapper.py | 12 |
3 files changed, 24 insertions, 3 deletions
@@ -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 d57b04f0c..3dc1f8676 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 ce729ed6b..90ad2d215 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 |