From 8fa9392360c144ba8391add65cfb8dc4a1067d51 Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Tue, 28 Sep 2021 22:58:08 +0200 Subject: [PATCH] Add missing methods added in :ticket:`6991` to ``scoped_session`` and ``async_scoped_session``. Fixes: #7103 Change-Id: If80481771d9b428f2403af3862e0479bd069257e --- doc/build/changelog/unreleased_14/7103.rst | 6 ++++ lib/sqlalchemy/ext/asyncio/scoping.py | 3 ++ lib/sqlalchemy/orm/scoping.py | 1 + test/ext/asyncio/test_scoping_py3k.py | 32 ++++++++++++++++++++++ test/orm/test_scoping.py | 32 ++++++++++++++++++++++ 5 files changed, 74 insertions(+) create mode 100644 doc/build/changelog/unreleased_14/7103.rst diff --git a/doc/build/changelog/unreleased_14/7103.rst b/doc/build/changelog/unreleased_14/7103.rst new file mode 100644 index 0000000000..2682b0422b --- /dev/null +++ b/doc/build/changelog/unreleased_14/7103.rst @@ -0,0 +1,6 @@ +.. change:: + :tags: bug + :tickets: 7103 + + Add missing methods added in :ticket:`6991` to ``scoped_session`` and + ``async_scoped_session``. diff --git a/lib/sqlalchemy/ext/asyncio/scoping.py b/lib/sqlalchemy/ext/asyncio/scoping.py index 7fa8cba5cf..4e7f15c1fd 100644 --- a/lib/sqlalchemy/ext/asyncio/scoping.py +++ b/lib/sqlalchemy/ext/asyncio/scoping.py @@ -40,6 +40,9 @@ from ...util import ScopedRegistry "refresh", "rollback", "scalar", + "scalars", + "stream", + "stream_scalars", ], attributes=[ "bind", diff --git a/lib/sqlalchemy/orm/scoping.py b/lib/sqlalchemy/orm/scoping.py index 353caa8fef..df3012df1e 100644 --- a/lib/sqlalchemy/orm/scoping.py +++ b/lib/sqlalchemy/orm/scoping.py @@ -105,6 +105,7 @@ class ScopedSessionMixin(object): "refresh", "rollback", "scalar", + "scalars", ], attributes=[ "bind", diff --git a/test/ext/asyncio/test_scoping_py3k.py b/test/ext/asyncio/test_scoping_py3k.py index c2ca73c419..7eeff566c3 100644 --- a/test/ext/asyncio/test_scoping_py3k.py +++ b/test/ext/asyncio/test_scoping_py3k.py @@ -1,9 +1,12 @@ +from asyncio import current_task + import sqlalchemy as sa from sqlalchemy import func from sqlalchemy import select from sqlalchemy import testing from sqlalchemy.ext.asyncio import async_scoped_session from sqlalchemy.ext.asyncio import AsyncSession as _AsyncSession +from sqlalchemy.orm import sessionmaker from sqlalchemy.testing import async_test from sqlalchemy.testing import eq_ from sqlalchemy.testing import is_ @@ -44,3 +47,32 @@ class AsyncScopedSessionTest(AsyncFixture): await AsyncSession.delete(u1) await AsyncSession.flush() eq_(await conn.scalar(stmt), 0) + + def test_attributes(self, async_engine): + expected = [ + name + for cls in _AsyncSession.mro() + for name in vars(cls) + if not name.startswith("_") + ] + + ignore_list = { + "dispatch", + "sync_session_class", + "run_sync", + "get_transaction", + "get_nested_transaction", + "in_transaction", + "in_nested_transaction", + } + + SM = async_scoped_session( + sessionmaker(async_engine, class_=_AsyncSession), current_task + ) + + missing = [ + name + for name in expected + if not hasattr(SM, name) and name not in ignore_list + ] + eq_(missing, []) diff --git a/test/orm/test_scoping.py b/test/orm/test_scoping.py index ad4ab55a28..9604adc6c5 100644 --- a/test/orm/test_scoping.py +++ b/test/orm/test_scoping.py @@ -209,3 +209,35 @@ class ScopedSessionTest(fixtures.MappedTest): ss = scoped_session(sessionmaker(testing.db, class_=MySession)) is_(ss.get_bind(), testing.db) + + def test_attributes(self): + expected = [ + name + for cls in Session.mro() + for name in vars(cls) + if not name.startswith("_") + ] + + ignore_list = { + "connection_callable", + "transaction", + "in_transaction", + "in_nested_transaction", + "get_transaction", + "get_nested_transaction", + "prepare", + "invalidate", + "bind_mapper", + "bind_table", + "enable_relationship_loading", + "dispatch", + } + + SM = scoped_session(sa.orm.sessionmaker(testing.db)) + + missing = [ + name + for name in expected + if not hasattr(SM, name) and name not in ignore_list + ] + eq_(missing, []) -- 2.47.3