]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix string formatting TypeError if tuple is passed
authorMiguel Ventura <miguel.ventura@gmail.com>
Mon, 14 May 2018 20:56:58 +0000 (16:56 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 17 May 2018 16:23:10 +0000 (12:23 -0400)
Fixed issue where the "ambiguous literal" error message used when
interpreting literal values as SQL expression values would encounter a
tuple value, and fail to format the message properly. Pull request courtesy
Miguel Ventura.

Change-Id: I50d5d32d5f80ec79703a42d4b19b42c2f9701f24
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/442

doc/build/changelog/unreleased_12/pr442.rst [new file with mode: 0644]
lib/sqlalchemy/sql/elements.py
test/sql/test_case_statement.py

diff --git a/doc/build/changelog/unreleased_12/pr442.rst b/doc/build/changelog/unreleased_12/pr442.rst
new file mode 100644 (file)
index 0000000..6cc5c2c
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, sql
+    :versions: 1.3.0b1
+
+    Fixed issue where the "ambiguous literal" error message used when
+    interpreting literal values as SQL expression values would encounter a
+    tuple value, and fail to format the message properly. Pull request courtesy
+    Miguel Ventura.
index 22636a05bde9a4cb981d12defbcb6709dc30e801..2e8c39f3b2c936c3eddd4ef30d09b635ffca10c1 100644 (file)
@@ -4283,7 +4283,7 @@ def _no_literals(element):
         raise exc.ArgumentError("Ambiguous literal: %r.  Use the 'text()' "
                                 "function to indicate a SQL expression "
                                 "literal, or 'literal()' to indicate a "
-                                "bound value." % element)
+                                "bound value." % (element, ))
     else:
         return element
 
index f7025b360586c29b40347acb0683ce9ba02e33c8..d81ff20cdb77268758d8a2455157b945d7c17391 100644 (file)
@@ -1,4 +1,4 @@
-from sqlalchemy.testing import assert_raises, eq_
+from sqlalchemy.testing import assert_raises, eq_, assert_raises_message
 from sqlalchemy.testing import fixtures, AssertsCompiledSQL
 from sqlalchemy import (
     testing, exc, case, select, literal_column, text, and_, Integer, cast,
@@ -102,11 +102,23 @@ class CaseTest(fixtures.TestBase, AssertsCompiledSQL):
             (0, 6, 'pk_6_data')
         ]
 
+    def test_literal_interpretation_ambiguous(self):
+        assert_raises_message(
+            exc.ArgumentError,
+            r"Ambiguous literal: 'x'.  Use the 'text\(\)' function",
+            case, [("x", "y")]
+        )
+
+    def test_literal_interpretation_ambiguous_tuple(self):
+        assert_raises_message(
+            exc.ArgumentError,
+            r"Ambiguous literal: \('x', 'y'\).  Use the 'text\(\)' function",
+            case, [(("x", "y"), "z")]
+        )
+
     def test_literal_interpretation(self):
         t = table('test', column('col1'))
 
-        assert_raises(exc.ArgumentError, case, [("x", "y")])
-
         self.assert_compile(
             case([("x", "y")], value=t.c.col1),
             "CASE test.col1 WHEN :param_1 THEN :param_2 END")