Skip to content

Commit

Permalink
apply correct type to orm connection.execution_options
Browse files Browse the repository at this point in the history
Fixed issue in :class:`_orm.Session` and :class:`_asyncio.AsyncSession`
methods such as :meth:`_orm.Session.connection` where the
:paramref:`_orm.Session.connection.execution_options` parameter were
hardcoded to an internal type that is not user-facing.

Fixes: sqlalchemy#10182
Change-Id: Iab9d510bf182c7dfc0521c948cf63396d05078e5
  • Loading branch information
zzzeek committed Aug 4, 2023
1 parent 6594c5d commit 022d148
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
8 changes: 8 additions & 0 deletions doc/build/changelog/unreleased_20/10182.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.. change::
:tags: bug, typing
:tickets: 10182

Fixed issue in :class:`_orm.Session` and :class:`_asyncio.AsyncSession`
methods such as :meth:`_orm.Session.connection` where the
:paramref:`_orm.Session.connection.execution_options` parameter were
hardcoded to an internal type that is not user-facing.
4 changes: 2 additions & 2 deletions lib/sqlalchemy/ext/asyncio/scoping.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from ...engine import Row
from ...engine import RowMapping
from ...engine.interfaces import _CoreAnyExecuteParams
from ...engine.interfaces import _ExecuteOptions
from ...engine.interfaces import CoreExecuteOptionsParameter
from ...engine.result import ScalarResult
from ...orm._typing import _IdentityKeyType
from ...orm._typing import _O
Expand Down Expand Up @@ -488,7 +488,7 @@ async def commit(self) -> None:
async def connection(
self,
bind_arguments: Optional[_BindArguments] = None,
execution_options: Optional[_ExecuteOptions] = None,
execution_options: Optional[CoreExecuteOptionsParameter] = None,
**kw: Any,
) -> AsyncConnection:
r"""Return a :class:`_asyncio.AsyncConnection` object corresponding to
Expand Down
4 changes: 2 additions & 2 deletions lib/sqlalchemy/ext/asyncio/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
from ...engine import RowMapping
from ...engine import ScalarResult
from ...engine.interfaces import _CoreAnyExecuteParams
from ...engine.interfaces import _ExecuteOptions
from ...engine.interfaces import CoreExecuteOptionsParameter
from ...event import dispatcher
from ...orm._typing import _IdentityKeyType
from ...orm._typing import _O
Expand Down Expand Up @@ -870,7 +870,7 @@ def get_bind(self, mapper=None, clause=None, **kw):
async def connection(
self,
bind_arguments: Optional[_BindArguments] = None,
execution_options: Optional[_ExecuteOptions] = None,
execution_options: Optional[CoreExecuteOptionsParameter] = None,
**kw: Any,
) -> AsyncConnection:
r"""Return a :class:`_asyncio.AsyncConnection` object corresponding to
Expand Down
4 changes: 2 additions & 2 deletions lib/sqlalchemy/orm/scoping.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
from ..engine import RowMapping
from ..engine.interfaces import _CoreAnyExecuteParams
from ..engine.interfaces import _CoreSingleExecuteParams
from ..engine.interfaces import _ExecuteOptions
from ..engine.interfaces import CoreExecuteOptionsParameter
from ..engine.result import ScalarResult
from ..sql._typing import _ColumnsClauseArgument
from ..sql._typing import _T0
Expand Down Expand Up @@ -554,7 +554,7 @@ def commit(self) -> None:
def connection(
self,
bind_arguments: Optional[_BindArguments] = None,
execution_options: Optional[_ExecuteOptions] = None,
execution_options: Optional[CoreExecuteOptionsParameter] = None,
) -> Connection:
r"""Return a :class:`_engine.Connection` object corresponding to this
:class:`.Session` object's transactional state.
Expand Down
7 changes: 4 additions & 3 deletions lib/sqlalchemy/orm/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
from ..engine.interfaces import _CoreAnyExecuteParams
from ..engine.interfaces import _CoreSingleExecuteParams
from ..engine.interfaces import _ExecuteOptions
from ..engine.interfaces import CoreExecuteOptionsParameter
from ..engine.result import ScalarResult
from ..event import _InstanceLevelDispatch
from ..sql._typing import _ColumnsClauseArgument
Expand Down Expand Up @@ -1089,7 +1090,7 @@ def _remove_snapshot(self) -> None:
def _connection_for_bind(
self,
bind: _SessionBind,
execution_options: Optional[_ExecuteOptions],
execution_options: Optional[CoreExecuteOptionsParameter],
) -> Connection:
if bind in self._connections:
if execution_options:
Expand Down Expand Up @@ -1939,7 +1940,7 @@ def prepare(self) -> None:
def connection(
self,
bind_arguments: Optional[_BindArguments] = None,
execution_options: Optional[_ExecuteOptions] = None,
execution_options: Optional[CoreExecuteOptionsParameter] = None,
) -> Connection:
r"""Return a :class:`_engine.Connection` object corresponding to this
:class:`.Session` object's transactional state.
Expand Down Expand Up @@ -1987,7 +1988,7 @@ def connection(
def _connection_for_bind(
self,
engine: _SessionBind,
execution_options: Optional[_ExecuteOptions] = None,
execution_options: Optional[CoreExecuteOptionsParameter] = None,
**kw: Any,
) -> Connection:
TransactionalContext._trans_ctx_check(self)
Expand Down
32 changes: 32 additions & 0 deletions test/typing/plain_files/orm/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,35 @@ async def test_with_for_update_async() -> None:

await ss.refresh(u1)
await ss.refresh(u1, with_for_update=True)


def test_exec_options() -> None:
"""test #10182"""

session = Session()

session.connection(
execution_options={"isolation_level": "REPEATABLE READ"}
)

scoped = scoped_session(sessionmaker())

scoped.connection(execution_options={"isolation_level": "REPEATABLE READ"})


async def async_test_exec_options() -> None:
"""test #10182"""

session = AsyncSession()

await session.connection(
execution_options={"isolation_level": "REPEATABLE READ"}
)

scoped = async_scoped_session(
async_sessionmaker(), scopefunc=asyncio.current_task
)

await scoped.connection(
execution_options={"isolation_level": "REPEATABLE READ"}
)

0 comments on commit 022d148

Please sign in to comment.