From: allenyuchen Date: Wed, 12 Feb 2025 17:35:58 +0000 (-0500) Subject: fix(AsyncResult): Fix scalar method error due to missing attribute X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ca092e73a254a3914fd93ca98340ba7762d4cee9;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git fix(AsyncResult): Fix scalar method error due to missing attribute 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 --- diff --git a/doc/build/changelog/unreleased_20/12338.rst b/doc/build/changelog/unreleased_20/12338.rst new file mode 100644 index 0000000000..6a71f08d73 --- /dev/null +++ b/doc/build/changelog/unreleased_20/12338.rst @@ -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. diff --git a/lib/sqlalchemy/ext/asyncio/result.py b/lib/sqlalchemy/ext/asyncio/result.py index 7b0b23ee44..ab3e23c593 100644 --- a/lib/sqlalchemy/ext/asyncio/result.py +++ b/lib/sqlalchemy/ext/asyncio/result.py @@ -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 diff --git a/test/ext/asyncio/test_engine_py3k.py b/test/ext/asyncio/test_engine_py3k.py index a37b088c7d..305beaef7c 100644 --- a/test/ext/asyncio/test_engine_py3k.py +++ b/test/ext/asyncio/test_engine_py3k.py @@ -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",)