From: Miguel Ventura Date: Mon, 14 May 2018 20:56:58 +0000 (-0400) Subject: Fix string formatting TypeError if tuple is passed X-Git-Tag: rel_1_3_0b1~182^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0b0b58c938c6a38fccc3e0ba59876b3b6b4f8009;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fix string formatting TypeError if tuple is passed 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 --- diff --git a/doc/build/changelog/unreleased_12/pr442.rst b/doc/build/changelog/unreleased_12/pr442.rst new file mode 100644 index 0000000000..6cc5c2c8b1 --- /dev/null +++ b/doc/build/changelog/unreleased_12/pr442.rst @@ -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. diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index 22636a05bd..2e8c39f3b2 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -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 diff --git a/test/sql/test_case_statement.py b/test/sql/test_case_statement.py index f7025b3605..d81ff20cdb 100644 --- a/test/sql/test_case_statement.py +++ b/test/sql/test_case_statement.py @@ -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")