]> 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:11 +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

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 7b0b23ee44b33de39d23d81577a509b7328a6cc6..ab3e23c593e544d871afa9c1bb9ee9991b162f4b 100644 (file)
@@ -97,6 +97,7 @@ class AsyncResult(_WithKeys, AsyncCommon[Row[Unpack[_Ts]]]):
 
         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 a37b088c7df536a4a4ad215f55e9a48ebea0a5eb..305beaef7cb58a5d41da883a9c7674f042d8549e 100644 (file)
@@ -1379,6 +1379,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",)