From 3b85eeb5d2e662141139ad54ccaad5df24ad9b7d Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 10 Sep 2019 12:46:21 -0400 Subject: [PATCH] Restore subquery.as_scalar() w/ deprecation Apparently Alias had an .as_scalar() method, so restore an equivalent to Subquery with an appropriate deprecation warning. Fixes: #4854 Change-Id: I6255d61b7d82487ca90ba8ee79d4b3a74e7cbe38 --- doc/build/changelog/unreleased_14/4617_scalar.rst | 5 ++++- lib/sqlalchemy/sql/selectable.py | 12 ++++++++++++ test/sql/test_deprecations.py | 9 +++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/doc/build/changelog/unreleased_14/4617_scalar.rst b/doc/build/changelog/unreleased_14/4617_scalar.rst index d47b3e464a..6fb4adcca7 100644 --- a/doc/build/changelog/unreleased_14/4617_scalar.rst +++ b/doc/build/changelog/unreleased_14/4617_scalar.rst @@ -13,7 +13,10 @@ This warning will in a later major release become an error, however the message will always be clear when :meth:`.SelectBase.scalar_subquery` needs to be invoked. The latter part of the change is for clarity and to reduce - the implicit decisionmaking by the query coercion system. + the implicit decisionmaking by the query coercion system. The + :meth:`.Subquery.as_scalar` method, which was previously + ``Alias.as_scalar``, is also deprecated; ``.scalar_subquery()`` should be + invoked directly from ` :func:`.select` object or :class:`.Query` object. This change is part of the larger change to convert :func:`.select` objects to no longer be directly part of the "from clause" class hierarchy, which diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index 00d3826b21..166e592b62 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -1753,6 +1753,18 @@ class Subquery(AliasedReturnsRows): roles.SelectStatementRole, selectable ).subquery(name=name) + @util.deprecated( + "1.4", + "The :meth:`.Subquery.as_scalar` method, which was previously " + "``Alias.as_scalar()`` prior to version 1.4, is deprecated and " + "will be removed in a future release; Please use the " + ":meth:`.Select.scalar_subquery` method of the :func:`.select` " + "construct before constructing a subquery object, or with the ORM " + "use the :meth:`.Query.scalar_subquery` method.", + ) + def as_scalar(self): + return self.element.scalar_subquery() + class FromGrouping(GroupedElement, FromClause): """Represent a grouping of a FROM clause""" diff --git a/test/sql/test_deprecations.py b/test/sql/test_deprecations.py index d126912a5d..09deb12944 100644 --- a/test/sql/test_deprecations.py +++ b/test/sql/test_deprecations.py @@ -579,6 +579,15 @@ class SubqueryCoercionsTest(fixtures.TestBase, AssertsCompiledSQL): is_true(stmt.compare(select([self.table1.c.myid]).scalar_subquery())) + def test_as_scalar_from_subquery(self): + with testing.expect_deprecated( + r"The Subquery.as_scalar\(\) method, which was previously " + r"``Alias.as_scalar\(\)`` prior to version 1.4" + ): + stmt = select([self.table1.c.myid]).subquery().as_scalar() + + is_true(stmt.compare(select([self.table1.c.myid]).scalar_subquery())) + def test_fromclause_subquery(self): stmt = select([self.table1.c.myid]) with testing.expect_deprecated( -- 2.47.2