]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Deprecate scoped_session usage with async sessions
authorFederico Caselli <cfederico87@gmail.com>
Tue, 27 Jul 2021 20:30:06 +0000 (22:30 +0200)
committerFederico Caselli <cfederico87@gmail.com>
Wed, 28 Jul 2021 19:36:14 +0000 (21:36 +0200)
Deprecate usage of :class:`_orm.scoped_session` with asyncio drivers.
When using Asyncio the :class:`_asyncio.async_scoped_session` should
be used instead.

Fixes: #6746
Change-Id: I540d57a406f59efc37fc61f0e9dfe03f32fe2904

doc/build/changelog/unreleased_14/6746.rst [new file with mode: 0644]
lib/sqlalchemy/ext/asyncio/scoping.py
lib/sqlalchemy/ext/asyncio/session.py
lib/sqlalchemy/orm/scoping.py
lib/sqlalchemy/orm/session.py
test/orm/test_deprecations.py

diff --git a/doc/build/changelog/unreleased_14/6746.rst b/doc/build/changelog/unreleased_14/6746.rst
new file mode 100644 (file)
index 0000000..cff1a4a
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: orm
+    :tickets: 6746
+
+    Deprecate usage of :class:`_orm.scoped_session` with asyncio drivers.
+    When using Asyncio the :class:`_asyncio.async_scoped_session` should
+    be used instead.
index 92d894599cd54e807dfaef7e321525ea684f37f3..7fa8cba5cf5e148d8f125da5fccd2545b2d51268 100644 (file)
@@ -63,6 +63,8 @@ class async_scoped_session(ScopedSessionMixin):
 
     """
 
+    _support_async = True
+
     def __init__(self, session_factory, scopefunc):
         """Construct a new :class:`_asyncio.async_scoped_session`.
 
index 59b6a2b15cc9155216434c7d79edcf6a3d6a9522..a10621eef356ffae301d86343c73c5c6a8ae18b3 100644 (file)
@@ -53,6 +53,8 @@ class AsyncSession(ReversibleProxy):
 
     """
 
+    _is_asyncio = True
+
     __slots__ = (
         "binds",
         "bind",
index be16bc9112ca093e78f7f2bf1d3d6490ef91a47d..353caa8fef8983255c5cb8a036a6699a6128b8a1 100644 (file)
@@ -13,6 +13,7 @@ from ..util import create_proxy_methods
 from ..util import ScopedRegistry
 from ..util import ThreadLocalRegistry
 from ..util import warn
+from ..util import warn_deprecated
 
 __all__ = ["scoped_session", "ScopedSessionMixin"]
 
@@ -42,9 +43,16 @@ class ScopedSessionMixin(object):
             else:
                 sess = self.session_factory(**kw)
                 self.registry.set(sess)
-                return sess
         else:
-            return self.registry()
+            sess = self.registry()
+        if not self._support_async and sess._is_asyncio:
+            warn_deprecated(
+                "Using `scoped_session` with asyncio is deprecated and "
+                "will raise an error in a future version. "
+                "Please use `async_scoped_session` instead.",
+                "1.4.23",
+            )
+        return sess
 
     def configure(self, **kwargs):
         """reconfigure the :class:`.sessionmaker` used by this
@@ -116,8 +124,16 @@ class scoped_session(ScopedSessionMixin):
 
     See :ref:`unitofwork_contextual` for a tutorial.
 
+    ..warning::
+
+       When using :ref:`asyncio_toplevel` the async
+       version :class:`_asyncio.async_scoped_session` should be
+       used instead.
+
     """
 
+    _support_async = False
+
     session_factory = None
     """The `session_factory` provided to `__init__` is stored in this
     attribute and may be accessed at a later time.  This can be useful when
index 8302f70d6b8e6a672b1af55e7b2c5437c53afc7e..f9ea1c16926223f3966f197764b5203ef4fe770c 100644 (file)
@@ -948,6 +948,8 @@ class Session(_SessionClassMethods):
 
     """
 
+    _is_asyncio = False
+
     @util.deprecated_params(
         autocommit=(
             "2.0",
index 4f762e4b719765bff62b758223727867c81e27c6..baa1f44cfb3ea92c5f337d6a4ef82745ada3102e 100644 (file)
@@ -38,7 +38,9 @@ from sqlalchemy.orm import joinedload
 from sqlalchemy.orm import mapper
 from sqlalchemy.orm import relation
 from sqlalchemy.orm import relationship
+from sqlalchemy.orm import scoped_session
 from sqlalchemy.orm import Session
+from sqlalchemy.orm import sessionmaker
 from sqlalchemy.orm import subqueryload
 from sqlalchemy.orm import synonym
 from sqlalchemy.orm import undefer
@@ -54,6 +56,7 @@ from sqlalchemy.testing import assertions
 from sqlalchemy.testing import AssertsCompiledSQL
 from sqlalchemy.testing import eq_
 from sqlalchemy.testing import eq_ignore_whitespace
+from sqlalchemy.testing import expect_deprecated
 from sqlalchemy.testing import fixtures
 from sqlalchemy.testing import is_
 from sqlalchemy.testing import is_true
@@ -5475,3 +5478,23 @@ class GetBindTest(_GetBindTest):
         session = self._fixture({self.tables.base_table: base_class_bind})
 
         is_(session.get_bind(self.classes.ConcreteSubClass), testing.db)
+
+
+class DeprecationScopedSessionTest(fixtures.MappedTest):
+    def test_config_errors(self):
+        sm = sessionmaker()
+
+        def go():
+            s = sm()
+            s._is_asyncio = True
+            return s
+
+        Session = scoped_session(go)
+
+        with expect_deprecated(
+            "Using `scoped_session` with asyncio is deprecated and "
+            "will raise an error in a future version. "
+            "Please use `async_scoped_session` instead."
+        ):
+            Session()
+        Session.remove()