]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
fix(AsyncResult): Fix scalar method error due to missing attribute
authorallenyuchen <allenyuchen@hotmail.com>
Wed, 12 Feb 2025 17:35:58 +0000 (12:35 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 12 Feb 2025 19:43:44 +0000 (14:43 -0500)
Fixed bug where :meth:`_asyncio.AsyncResult.scalar`,
:meth:`_asyncio.AsyncResult.scalar_one_or_none`, and
:meth:`_asyncio.AsyncResult.scalar_one` would raise an ``AttributeError``
due to a missing internal attribute.  Pull request courtesy Allen Ho.

Fixes: #12338
Closes: #12339
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12339
Pull-request-sha: 63ba43365e9624a75e3f206e6b0f4569e3940da6

Change-Id: I44a949e4a942a080338037cd570d4b1dc0d7550d
(cherry picked from commit ca092e73a254a3914fd93ca98340ba7762d4cee9)

doc/build/changelog/unreleased_20/12338.rst [new file with mode: 0644]
lib/sqlalchemy/ext/asyncio/result.py
test/ext/asyncio/test_engine_py3k.py

diff --git a/doc/build/changelog/unreleased_20/12338.rst b/doc/build/changelog/unreleased_20/12338.rst
new file mode 100644 (file)
index 0000000..6a71f08
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, asyncio
+    :tickets: 12338
+
+    Fixed bug where :meth:`_asyncio.AsyncResult.scalar`,
+    :meth:`_asyncio.AsyncResult.scalar_one_or_none`, and
+    :meth:`_asyncio.AsyncResult.scalar_one` would raise an ``AttributeError``
+    due to a missing internal attribute.  Pull request courtesy Allen Ho.
index c51e166d9163989e2aa10dd9308801ac697d2f43..8003f66afe29320db9014b0559a1a380657c4212 100644 (file)
@@ -93,6 +93,7 @@ class AsyncResult(_WithKeys, AsyncCommon[Row[_TP]]):
 
         self._metadata = real_result._metadata
         self._unique_filter_state = real_result._unique_filter_state
+        self._source_supports_scalars = real_result._source_supports_scalars
         self._post_creational_filter = None
 
         # BaseCursorResult pre-generates the "_row_getter".  Use that
index 227307e086f23137076792f0526215b40ace1ce2..6be408ecaeaa5b2a14ca1b1efb0863503eb6d367 100644 (file)
@@ -1376,6 +1376,26 @@ class AsyncResultTest(EngineFixture):
 
             await conn.run_sync(lambda _: cursor.close())
 
+    @async_test
+    @testing.variation("case", ["scalar_one", "scalar_one_or_none", "scalar"])
+    async def test_stream_scalar(self, async_engine, case: testing.Variation):
+        users = self.tables.users
+        async with async_engine.connect() as conn:
+            result = await conn.stream(
+                select(users).limit(1).order_by(users.c.user_name)
+            )
+
+            if case.scalar_one:
+                u1 = await result.scalar_one()
+            elif case.scalar_one_or_none:
+                u1 = await result.scalar_one_or_none()
+            elif case.scalar:
+                u1 = await result.scalar()
+            else:
+                case.fail()
+
+            eq_(u1, 1)
+
 
 class TextSyncDBAPI(fixtures.TestBase):
     __requires__ = ("asyncio",)