]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Calling class_mapper() and passing in an object
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 9 Sep 2011 20:18:44 +0000 (16:18 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 9 Sep 2011 20:18:44 +0000 (16:18 -0400)
that is not a "type" (i.e. a class that could
potentially be mapped) now raises an informative
ArgumentError, rather than UnmappedClassError.
[ticket:2196]

CHANGES
lib/sqlalchemy/orm/util.py
test/orm/test_mapper.py

diff --git a/CHANGES b/CHANGES
index 11f11734723702b61b1b989e70e2c16f33496565..aac1a7a4a777ce003a2f1cd8495093da1c20db59 100644 (file)
--- 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
index d57b04f0cd50fc71de64173d0492addabac68c06..3dc1f86763b139207c864c4b5e4788bbda944cf1 100644 (file)
@@ -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:
index ce729ed6bfc05a0d958404ee9d2efa215b8e4ec6..90ad2d2158bc03eaa2a728cdad7a7e947ac889ec 100644 (file)
@@ -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