Skip to content

Commit 5219a0e

Browse files
zzzeekGerrit Code Review
authored andcommitted
Merge "Deprecate scoped_session usage with async sessions"
2 parents b5822c2 + a2e9aca commit 5219a0e

6 files changed

Lines changed: 54 additions & 2 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.. change::
2+
:tags: orm
3+
:tickets: 6746
4+
5+
Deprecate usage of :class:`_orm.scoped_session` with asyncio drivers.
6+
When using Asyncio the :class:`_asyncio.async_scoped_session` should
7+
be used instead.

lib/sqlalchemy/ext/asyncio/scoping.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class async_scoped_session(ScopedSessionMixin):
6363
6464
"""
6565

66+
_support_async = True
67+
6668
def __init__(self, session_factory, scopefunc):
6769
"""Construct a new :class:`_asyncio.async_scoped_session`.
6870

lib/sqlalchemy/ext/asyncio/session.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class AsyncSession(ReversibleProxy):
5353
5454
"""
5555

56+
_is_asyncio = True
57+
5658
__slots__ = (
5759
"binds",
5860
"bind",

lib/sqlalchemy/orm/scoping.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ..util import ScopedRegistry
1414
from ..util import ThreadLocalRegistry
1515
from ..util import warn
16+
from ..util import warn_deprecated
1617

1718
__all__ = ["scoped_session", "ScopedSessionMixin"]
1819

@@ -42,9 +43,16 @@ def __call__(self, **kw):
4243
else:
4344
sess = self.session_factory(**kw)
4445
self.registry.set(sess)
45-
return sess
4646
else:
47-
return self.registry()
47+
sess = self.registry()
48+
if not self._support_async and sess._is_asyncio:
49+
warn_deprecated(
50+
"Using `scoped_session` with asyncio is deprecated and "
51+
"will raise an error in a future version. "
52+
"Please use `async_scoped_session` instead.",
53+
"1.4.23",
54+
)
55+
return sess
4856

4957
def configure(self, **kwargs):
5058
"""reconfigure the :class:`.sessionmaker` used by this
@@ -116,8 +124,16 @@ class scoped_session(ScopedSessionMixin):
116124
117125
See :ref:`unitofwork_contextual` for a tutorial.
118126
127+
..warning::
128+
129+
When using :ref:`asyncio_toplevel` the async
130+
version :class:`_asyncio.async_scoped_session` should be
131+
used instead.
132+
119133
"""
120134

135+
_support_async = False
136+
121137
session_factory = None
122138
"""The `session_factory` provided to `__init__` is stored in this
123139
attribute and may be accessed at a later time. This can be useful when

lib/sqlalchemy/orm/session.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,8 @@ class Session(_SessionClassMethods):
948948
949949
"""
950950

951+
_is_asyncio = False
952+
951953
@util.deprecated_params(
952954
autocommit=(
953955
"2.0",

test/orm/test_deprecations.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
from sqlalchemy.orm import mapper
3939
from sqlalchemy.orm import relation
4040
from sqlalchemy.orm import relationship
41+
from sqlalchemy.orm import scoped_session
4142
from sqlalchemy.orm import Session
43+
from sqlalchemy.orm import sessionmaker
4244
from sqlalchemy.orm import subqueryload
4345
from sqlalchemy.orm import synonym
4446
from sqlalchemy.orm import undefer
@@ -54,6 +56,7 @@
5456
from sqlalchemy.testing import AssertsCompiledSQL
5557
from sqlalchemy.testing import eq_
5658
from sqlalchemy.testing import eq_ignore_whitespace
59+
from sqlalchemy.testing import expect_deprecated
5760
from sqlalchemy.testing import fixtures
5861
from sqlalchemy.testing import is_
5962
from sqlalchemy.testing import is_true
@@ -5475,3 +5478,23 @@ def test_bind_base_table_concrete_sub_class(self):
54755478
session = self._fixture({self.tables.base_table: base_class_bind})
54765479

54775480
is_(session.get_bind(self.classes.ConcreteSubClass), testing.db)
5481+
5482+
5483+
class DeprecationScopedSessionTest(fixtures.MappedTest):
5484+
def test_config_errors(self):
5485+
sm = sessionmaker()
5486+
5487+
def go():
5488+
s = sm()
5489+
s._is_asyncio = True
5490+
return s
5491+
5492+
Session = scoped_session(go)
5493+
5494+
with expect_deprecated(
5495+
"Using `scoped_session` with asyncio is deprecated and "
5496+
"will raise an error in a future version. "
5497+
"Please use `async_scoped_session` instead."
5498+
):
5499+
Session()
5500+
Session.remove()

0 commit comments

Comments
 (0)