Fixed typing issue where the object type when using :class:`_engine.Result`
as a context manager were not preserved, indicating :class:`_engine.Result`
in all cases rather than the specific :class:`_engine.Result` sub-type.
Pull request courtesy Martin Baláž.
Fixes: #9136
Closes: #9135
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/9135
Pull-request-sha:
97a9829db59db359fbb400ec0d913bdf8954f00a
Change-Id: I60a7f89ba39bf0f9fc5e6e7bf09f642167fe476f
:tickets: 9125
Fixed typing issue where iterating over a :class:`_orm.Query` object
- was not correctly typed.
+ was not correctly typed.
+
+.. change::
+ :tags: typing, bug
+ :tickets: 9136
+
+ Fixed typing issue where the object type when using :class:`_engine.Result`
+ as a context manager were not preserved, indicating :class:`_engine.Result`
+ in all cases rather than the specific :class:`_engine.Result` sub-type.
+ Pull request courtesy Martin Baláž.
def __init__(self, cursor_metadata: ResultMetaData):
self._metadata = cursor_metadata
- def __enter__(self) -> Result[_TP]:
+ def __enter__(self: SelfResult) -> SelfResult:
return self
def __exit__(self, type_: Any, value: Any, traceback: Any) -> None:
reveal_type(multi_stmt)
+def t_result_ctxmanager() -> None:
+ with connection.execute(select(column("q", Integer))) as r1:
+ # EXPECTED_TYPE: CursorResult[Tuple[int]]
+ reveal_type(r1)
+
+ with r1.mappings() as r1m:
+ # EXPECTED_TYPE: MappingResult
+ reveal_type(r1m)
+
+ with connection.scalars(select(column("q", Integer))) as r2:
+ # EXPECTED_TYPE: ScalarResult[int]
+ reveal_type(r2)
+
+ with session.execute(select(User.id)) as r3:
+ # EXPECTED_TYPE: Result[Tuple[int]]
+ reveal_type(r3)
+
+ with session.scalars(select(User.id)) as r4:
+ # EXPECTED_TYPE: ScalarResult[int]
+ reveal_type(r4)
+
+
def t_entity_varieties() -> None:
a1 = aliased(User)