From 56976624169af6d0d329b4834ee9caa7243573dc Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Mon, 21 Oct 2013 15:06:41 -0400 Subject: [PATCH] - Fixed bug where :func:`.type_coerce` would not interpret ORM elements with a ``__clause_element__()`` method properly. [ticket:2849] --- doc/build/changelog/changelog_08.rst | 8 ++++++++ lib/sqlalchemy/sql/elements.py | 4 ++-- test/sql/test_types.py | 11 +++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index 727931f9aa..ab36c76f86 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -10,6 +10,14 @@ .. changelog:: :version: 0.8.3 + .. change:: + :tags: bug, sql + :tickets: 2849 + :versions: 0.9.0 + + Fixed bug where :func:`.type_coerce` would not interpret ORM + elements with a ``__clause_element__()`` method properly. + .. change:: :tags: bug, sql :tickets: 2842 diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index f70496418f..251102d59c 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -117,8 +117,8 @@ def type_coerce(expr, type_): """ type_ = type_api.to_instance(type_) - if hasattr(expr, '__clause_expr__'): - return type_coerce(expr.__clause_expr__()) + if hasattr(expr, '__clause_element__'): + return type_coerce(expr.__clause_element__(), type_) elif isinstance(expr, BindParameter): bp = expr._clone() bp.type = type_ diff --git a/test/sql/test_types.py b/test/sql/test_types.py index a2791ee29a..f739707f3a 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -466,6 +466,17 @@ class UserDefinedTest(fixtures.TablesTest, AssertsCompiledSQL): 'd1BIND_OUT' ) + class MyFoob(object): + def __clause_element__(self): + return t.c.data + + eq_( + testing.db.execute( + select([t.c.data, type_coerce(MyFoob(), MyType)]) + ).fetchall(), + [('d1', 'd1BIND_OUT')] + ) + @classmethod def define_tables(cls, metadata): class MyType(types.UserDefinedType): -- 2.47.3