From 7002b2b0b109e70b47d4ae4f5f6a4d7df5586f21 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 29 Apr 2021 16:38:03 -0400 Subject: Ensure iterable passed to Select is not a mapped class Fixed regression caused by :ticket:`5395` where tuning back the check for sequences in :func:`_sql.select` now caused failures when doing 2.0-style querying with a mapped class that also happens to have an ``__iter__()`` method. Tuned the check some more to accommodate this as well as some other interesting ``__iter__()`` scenarios. Fixes: #6300 Change-Id: Idf1983fd764b91a7d5fa8117aee8a3def3cfe5ff --- lib/sqlalchemy/sql/selectable.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'lib/sqlalchemy/sql') diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index ff830dbf6..43ba0da4c 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -56,6 +56,7 @@ from .elements import UnaryExpression from .visitors import InternalTraversal from .. import exc from .. import util +from ..inspection import inspect if util.TYPE_CHECKING: from typing import Any @@ -4959,8 +4960,17 @@ class Select( """ if ( args - and hasattr(args[0], "__iter__") - and not isinstance(args[0], util.string_types + (ClauseElement,)) + and ( + isinstance(args[0], list) + or ( + hasattr(args[0], "__iter__") + and not isinstance( + args[0], util.string_types + (ClauseElement,) + ) + and inspect(args[0], raiseerr=False) is None + and not hasattr(args[0], "__clause_element__") + ) + ) ) or kw: return cls.create_legacy_select(*args, **kw) else: -- cgit v1.2.1