]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- [bug] the @compiles decorator raises an
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 6 Dec 2011 17:49:39 +0000 (12:49 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 6 Dec 2011 17:49:39 +0000 (12:49 -0500)
informative error message when no "default"
compilation handler is present, rather
than KeyError.

CHANGES
lib/sqlalchemy/ext/compiler.py
test/ext/test_compiler.py

diff --git a/CHANGES b/CHANGES
index 06c55158f2162800699111085b6b64e6936710ab..3988ed22a4b0fdbd20c55d4e252ad221099227c6 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -261,6 +261,11 @@ CHANGES
      case here is fairly experimental, but only
      adds one line of code to Query.
 
+   - [bug] the @compiles decorator raises an
+     informative error message when no "default"
+     compilation handler is present, rather
+     than KeyError.
+
 - examples
    - [bug] Fixed bug in history_meta.py example where
      the "unique" flag was not removed from a 
index cb126c374fb9095a34113bc7e07a2907711da39f..daa9e15518d51435b9e013d79c6bbe4c62193aa0 100644 (file)
@@ -367,6 +367,7 @@ Example usage::
     )
     
 """
+from sqlalchemy import exc
 
 def compiles(class_, *specs):
     def decorate(fn):
@@ -399,6 +400,11 @@ class _dispatcher(object):
         # TODO: yes, this could also switch off of DBAPI in use.
         fn = self.specs.get(compiler.dialect.name, None)
         if not fn:
-            fn = self.specs['default']
+            try:
+                fn = self.specs['default']
+            except KeyError:
+                raise exc.CompileError(
+                        "%s construct has no default "
+                        "compilation handler." % type(element))
         return fn(element, compiler, **kw)
 
index 0f53f2cb0c5a8b586538672db355be3afee7a4da..318a1e76c4c2fc6f814c717c544cf3747bdad832 100644 (file)
@@ -6,7 +6,9 @@ from sqlalchemy.sql.expression import ClauseElement, ColumnClause,\
 
 from sqlalchemy.schema import DDLElement
 from sqlalchemy.ext.compiler import compiles
+from sqlalchemy import exc
 from sqlalchemy.sql import table, column, visitors
+from test.lib.testing import assert_raises_message
 from test.lib import *
 
 class UserDefinedTest(fixtures.TestBase, AssertsCompiledSQL):
@@ -105,6 +107,21 @@ class UserDefinedTest(fixtures.TestBase, AssertsCompiledSQL):
             "FROM mytable WHERE mytable.x > :x_1)"
         )
 
+    def test_no_default_message(self):
+        class MyThingy(ColumnClause):
+            pass
+
+        @compiles(MyThingy, "psotgresql")
+        def visit_thingy(thingy, compiler, **kw):
+            return "mythingy"
+
+        assert_raises_message(
+            exc.CompileError,
+            "<class 'test.ext.test_compiler.MyThingy'> "
+            "construct has no default compilation handler.",
+            str, MyThingy('x')
+        )
+
     def test_annotations(self):
         """test that annotated clause constructs use the 
         decorated class' compiler.