From a261a78894c4f835b5da7fcbfb3d466a687bc11b Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 4 Mar 2022 17:17:53 -0500 Subject: [PATCH] fix type string formatting calls Fixed type-related error messages that would fail for values that were tuples, due to string formatting syntax, including compile of unsupported literal values and invalid boolean values. Fixes: #7721 Change-Id: I6775721486ef2db2d0738b9aa08b9f2570f55659 --- doc/build/changelog/unreleased_14/7721.rst | 7 +++++++ lib/sqlalchemy/sql/sqltypes.py | 8 ++++---- test/sql/test_types.py | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 doc/build/changelog/unreleased_14/7721.rst diff --git a/doc/build/changelog/unreleased_14/7721.rst b/doc/build/changelog/unreleased_14/7721.rst new file mode 100644 index 0000000000..d719e22334 --- /dev/null +++ b/doc/build/changelog/unreleased_14/7721.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: bug, sql + :tickets: 7721 + + Fixed type-related error messages that would fail for values that were + tuples, due to string formatting syntax, including compile of unsupported + literal values and invalid boolean values. diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index 3794fd8f79..bf5f1dc1bc 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -1755,10 +1755,10 @@ class Boolean(Emulated, TypeEngine[bool], SchemaType): def _strict_as_bool(self, value): if value not in self._strict_bools: if not isinstance(value, int): - raise TypeError("Not a boolean value: %r" % value) + raise TypeError("Not a boolean value: %r" % (value,)) else: raise ValueError( - "Value %r is not None, True, or False" % value + "Value %r is not None, True, or False" % (value,) ) return value @@ -3087,7 +3087,7 @@ class NullType(TypeEngine): def literal_processor(self, dialect): def process(value): raise exc.CompileError( - "Don't know how to render literal SQL value: %r" % value + "Don't know how to render literal SQL value: %r" % (value,) ) return process @@ -3181,7 +3181,7 @@ def _resolve_value_to_type(value): insp.__class__ in inspection._registrars ): raise exc.ArgumentError( - "Object %r is not legal as a SQL literal value" % value + "Object %r is not legal as a SQL literal value" % (value,) ) return NULLTYPE else: diff --git a/test/sql/test_types.py b/test/sql/test_types.py index 58dfa4dd8a..223f367b1f 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -3422,6 +3422,23 @@ class ExpressionTest( class CompileTest(fixtures.TestBase, AssertsCompiledSQL): __dialect__ = "default" + def test_compile_err_formatting(self): + with expect_raises_message( + exc.CompileError, + r"Don't know how to render literal SQL value: \(1, 2, 3\)", + ): + func.foo((1, 2, 3)).compile(compile_kwargs={"literal_binds": True}) + + def test_strict_bool_err_formatting(self): + typ = Boolean() + + dialect = default.DefaultDialect() + with expect_raises_message( + TypeError, + r"Not a boolean value: \(5,\)", + ): + typ.bind_processor(dialect)((5,)) + @testing.requires.unbounded_varchar def test_string_plain(self): self.assert_compile(String(), "VARCHAR") -- 2.47.2