From: Federico Caselli Date: Sat, 4 Jan 2025 09:21:08 +0000 (+0100) Subject: Removed executable coercion X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=404de6e5ccfcf1054ea5777c95780d55f642e44c;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Removed executable coercion Removed the automatic coercion of executable objects, such as :class:`_orm.Query`, when passed into :meth:`_orm.Session.execute`. This usage raised a deprecation warning since the 1.4 series. Fixes: #12218 Change-Id: Iaab3116fcc8d957ff3f14e84a4ece428fd176b8b --- diff --git a/doc/build/changelog/unreleased_21/12218.rst b/doc/build/changelog/unreleased_21/12218.rst new file mode 100644 index 0000000000..98ab99529f --- /dev/null +++ b/doc/build/changelog/unreleased_21/12218.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: sql + :tickets: 12218 + + Removed the automatic coercion of executable objects, such as + :class:`_orm.Query`, when passed into :meth:`_orm.Session.execute`. + This usage raised a deprecation warning since the 1.4 series. diff --git a/lib/sqlalchemy/exc.py b/lib/sqlalchemy/exc.py index c66124d6c8..077844c3c2 100644 --- a/lib/sqlalchemy/exc.py +++ b/lib/sqlalchemy/exc.py @@ -139,7 +139,7 @@ class ObjectNotExecutableError(ArgumentError): """ def __init__(self, target: Any): - super().__init__("Not an executable object: %r" % target) + super().__init__(f"Not an executable object: {target!r}") self.target = target def __reduce__(self) -> Union[str, Tuple[Any, ...]]: diff --git a/lib/sqlalchemy/sql/coercions.py b/lib/sqlalchemy/sql/coercions.py index 7119ae1c1f..acbecb8229 100644 --- a/lib/sqlalchemy/sql/coercions.py +++ b/lib/sqlalchemy/sql/coercions.py @@ -1167,21 +1167,11 @@ class StatementImpl(_CoerceLiterals, RoleImpl): if resolved is not original_element and not isinstance( original_element, str ): - # use same method as Connection uses; this will later raise - # ObjectNotExecutableError + # use same method as Connection uses try: original_element._execute_on_connection - except AttributeError: - util.warn_deprecated( - "Object %r should not be used directly in a SQL statement " - "context, such as passing to methods such as " - "session.execute(). This usage will be disallowed in a " - "future release. " - "Please use Core select() / update() / delete() etc. " - "with Session.execute() and other statement execution " - "methods." % original_element, - "1.4", - ) + except AttributeError as err: + raise exc.ObjectNotExecutableError(original_element) from err return resolved diff --git a/test/orm/test_query.py b/test/orm/test_query.py index 88e76e7c38..a2e78041dd 100644 --- a/test/orm/test_query.py +++ b/test/orm/test_query.py @@ -160,17 +160,10 @@ class MiscTest(QueryTest): q = Session().query(literal_column("1")) - if executor == "session": - with testing.expect_deprecated( - r"Object .*Query.* should not be used directly in a " - r"SQL statement context" - ): - meth(q) - else: - with testing.expect_raises_message( - sa_exc.ObjectNotExecutableError, "Not an executable object" - ): - meth(q) + with testing.expect_raises_message( + sa_exc.ObjectNotExecutableError, "Not an executable object: .*" + ): + meth(q) class OnlyReturnTuplesTest(QueryTest):