--- /dev/null
+.. change::
+ :tags: bug, typing
+ :tickets: 9762
+
+ Fixed typing for the :paramref:`_orm.Session.get.with_for_update` parameter
+ of :meth:`_orm.Session.get` and :meth:`_orm.Session.refresh` (as well as
+ corresponding methods on :class:`_asyncio.AsyncSession`) to accept boolean
+ ``True`` and all other argument forms accepted by the parameter at runtime.
from ...orm.session import _SessionBind
from ...sql.base import Executable
from ...sql.elements import ClauseElement
- from ...sql.selectable import ForUpdateArg
+ from ...sql.selectable import ForUpdateParameter
from ...sql.selectable import TypedReturnsRows
_T = TypeVar("_T", bound=Any)
*,
options: Optional[Sequence[ORMOption]] = None,
populate_existing: bool = False,
- with_for_update: Optional[ForUpdateArg] = None,
+ with_for_update: ForUpdateParameter = None,
identity_token: Optional[Any] = None,
execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
) -> Optional[_O]:
self,
instance: object,
attribute_names: Optional[Iterable[str]] = None,
- with_for_update: Optional[ForUpdateArg] = None,
+ with_for_update: ForUpdateParameter = None,
) -> None:
r"""Expire and refresh the attributes on the given instance.
from ...sql._typing import _InfoType
from ...sql.base import Executable
from ...sql.elements import ClauseElement
- from ...sql.selectable import ForUpdateArg
+ from ...sql.selectable import ForUpdateParameter
from ...sql.selectable import TypedReturnsRows
_AsyncSessionBind = Union["AsyncEngine", "AsyncConnection"]
self,
instance: object,
attribute_names: Optional[Iterable[str]] = None,
- with_for_update: Optional[ForUpdateArg] = None,
+ with_for_update: ForUpdateParameter = None,
) -> None:
"""Expire and refresh the attributes on the given instance.
*,
options: Optional[Sequence[ORMOption]] = None,
populate_existing: bool = False,
- with_for_update: Optional[ForUpdateArg] = None,
+ with_for_update: ForUpdateParameter = None,
identity_token: Optional[Any] = None,
execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
) -> Optional[_O]:
from ..sql.base import Executable
from ..sql.elements import ClauseElement
from ..sql.roles import TypedColumnsClauseRole
- from ..sql.selectable import ForUpdateArg
+ from ..sql.selectable import ForUpdateParameter
from ..sql.selectable import TypedReturnsRows
_T = TypeVar("_T", bound=Any)
*,
options: Optional[Sequence[ORMOption]] = None,
populate_existing: bool = False,
- with_for_update: Optional[ForUpdateArg] = None,
+ with_for_update: ForUpdateParameter = None,
identity_token: Optional[Any] = None,
execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
bind_arguments: Optional[_BindArguments] = None,
self,
instance: object,
attribute_names: Optional[Iterable[str]] = None,
- with_for_update: Optional[ForUpdateArg] = None,
+ with_for_update: ForUpdateParameter = None,
) -> None:
r"""Expire and refresh attributes on the given instance.
from ..sql.base import ExecutableOption
from ..sql.elements import ClauseElement
from ..sql.roles import TypedColumnsClauseRole
+ from ..sql.selectable import ForUpdateParameter
from ..sql.selectable import TypedReturnsRows
_T = TypeVar("_T", bound=Any)
self,
instance: object,
attribute_names: Optional[Iterable[str]] = None,
- with_for_update: Optional[ForUpdateArg] = None,
+ with_for_update: ForUpdateParameter = None,
) -> None:
"""Expire and refresh attributes on the given instance.
*,
options: Optional[Sequence[ORMOption]] = None,
populate_existing: bool = False,
- with_for_update: Optional[ForUpdateArg] = None,
+ with_for_update: ForUpdateParameter = None,
identity_token: Optional[Any] = None,
execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
bind_arguments: Optional[_BindArguments] = None,
*,
options: Optional[Sequence[ExecutableOption]] = None,
populate_existing: bool = False,
- with_for_update: Optional[ForUpdateArg] = None,
+ with_for_update: ForUpdateParameter = None,
identity_token: Optional[Any] = None,
execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
bind_arguments: Optional[_BindArguments] = None,
return [self]
+ForUpdateParameter = Union["ForUpdateArg", None, bool, Dict[str, Any]]
+
+
class ForUpdateArg(ClauseElement):
_traverse_internals: _TraverseInternalsType = [
("of", InternalTraversal.dp_clauseelement_list),
@classmethod
def _from_argument(
- cls, with_for_update: Union[ForUpdateArg, None, bool, Dict[str, Any]]
+ cls, with_for_update: ForUpdateParameter
) -> Optional[ForUpdateArg]:
if isinstance(with_for_update, ForUpdateArg):
return with_for_update
from __future__ import annotations
+import asyncio
from typing import List
from sqlalchemy import create_engine
from sqlalchemy import ForeignKey
+from sqlalchemy.ext.asyncio import async_scoped_session
+from sqlalchemy.ext.asyncio import async_sessionmaker
+from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import relationship
+from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import Session
+from sqlalchemy.orm import sessionmaker
class Base(DeclarativeBase):
).offset(User.id)
# more result tests in typed_results.py
+
+
+def test_with_for_update() -> None:
+ """test #9762"""
+ sess = Session()
+ ss = scoped_session(sessionmaker())
+
+ sess.get(User, 1)
+ sess.get(User, 1, with_for_update=True)
+ ss.get(User, 1)
+ ss.get(User, 1, with_for_update=True)
+
+ u1 = User()
+ sess.refresh(u1)
+ sess.refresh(u1, with_for_update=True)
+ ss.refresh(u1)
+ ss.refresh(u1, with_for_update=True)
+
+
+async def test_with_for_update_async() -> None:
+ """test #9762"""
+ sess = AsyncSession()
+ ss = async_scoped_session(
+ async_sessionmaker(), scopefunc=asyncio.current_task
+ )
+
+ await sess.get(User, 1)
+ await sess.get(User, 1, with_for_update=True)
+
+ await ss.get(User, 1)
+ await ss.get(User, 1, with_for_update=True)
+
+ u1 = User()
+ await sess.refresh(u1)
+ await sess.refresh(u1, with_for_update=True)
+
+ await ss.refresh(u1)
+ await ss.refresh(u1, with_for_update=True)