Skip to content

Commit 711aad4

Browse files
authored
add api changes from sqlalchemy/sqlalchemy#6990 (#179)
1 parent fdf0919 commit 711aad4

File tree

6 files changed

+112
-3
lines changed

6 files changed

+112
-3
lines changed

sqlalchemy-stubs/engine/base.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ from .interfaces import Connectable as Connectable
1515
from .interfaces import Dialect
1616
from .interfaces import ExceptionContext
1717
from .interfaces import ExecutionContext
18+
from .result import ScalarResult
1819
from .url import URL
1920
from .. import log
2021
from .._typing import _ExecuteOptions
@@ -86,6 +87,9 @@ class Connection(_ConnectionTypingCommon, Connectable):
8687
def scalar(
8788
self, object_: Any, *multiparams: Any, **params: Any
8889
) -> Any: ...
90+
def scalars(
91+
self, statement: Any, *multiparams: Any, **params: Any
92+
) -> ScalarResult: ...
8993
def execute(self, statement: Any, *multiparams: Any, **params: Any) -> CursorResult: ... # type: ignore[override]
9094
def exec_driver_sql(
9195
self,

sqlalchemy-stubs/ext/asyncio/engine.pyi

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Any
22
from typing import Callable
33
from typing import Generator
4+
from typing import List
45
from typing import MutableMapping
56
from typing import NoReturn
67
from typing import Optional
@@ -9,10 +10,12 @@ from typing import TypeVar
910
from .base import ProxyComparable
1011
from .base import StartableContext
1112
from .result import AsyncResult
13+
from .result import AsyncScalarResult
1214
from ..._typing import _ExecuteOptions
1315
from ..._typing import _ExecuteParams
1416
from ...engine import Dialect
1517
from ...engine import Result
18+
from ...engine import ScalarResult
1619
from ...engine import Transaction
1720
from ...engine.base import _ConnectionTypingCommon
1821
from ...engine.base import _EngineTypingCommon
@@ -97,6 +100,18 @@ class AsyncConnection(
97100
parameters: Optional[_ExecuteParams] = ...,
98101
execution_options: Optional[_ExecuteOptions] = ...,
99102
) -> Any: ...
103+
async def scalars(
104+
self,
105+
statement: Executable,
106+
parameters: Optional[_ExecuteParams] = ...,
107+
execution_options: Optional[_ExecuteOptions] = ...,
108+
) -> ScalarResult: ...
109+
async def stream_scalars(
110+
self,
111+
statement: Executable,
112+
parameters: Optional[_ExecuteParams] = ...,
113+
execution_options: Optional[_ExecuteOptions] = ...,
114+
) -> AsyncScalarResult: ...
100115
async def run_sync(
101116
self, fn: Callable[..., Any], *arg: Any, **kw: Any
102117
) -> Any: ...

sqlalchemy-stubs/ext/asyncio/session.pyi

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ from .base import StartableContext
1616
from .engine import AsyncConnection
1717
from .engine import AsyncEngine
1818
from .result import AsyncResult
19+
from .result import AsyncScalarResult
1920
from ..._typing import _ExecuteOptions
2021
from ..._typing import _ExecuteParams
2122
from ...engine import Result
23+
from ...engine import ScalarResult
2224
from ...orm import Session
2325
from ...orm.session import _BindArguments
2426
from ...orm.session import _SessionClassMethodNoIoTypingCommon
@@ -119,7 +121,19 @@ class _AsyncSessionProtocol(
119121
execution_options: Optional[_ExecuteOptions] = ...,
120122
bind_arguments: Optional[_BindArguments] = ...,
121123
**kw: Any,
122-
) -> Any: ...
124+
) -> AsyncResult: ...
125+
async def scalars(
126+
self,
127+
statement: Executable,
128+
parameters: Optional[_ExecuteParams] = ...,
129+
execution_options: Optional[_ExecuteOptions] = ...,
130+
) -> ScalarResult: ...
131+
async def stream_scalars(
132+
self,
133+
statement: Executable,
134+
parameters: Optional[_ExecuteParams] = ...,
135+
execution_options: Optional[_ExecuteOptions] = ...,
136+
) -> AsyncScalarResult: ...
123137
async def delete(self, instance: Any) -> None: ...
124138
async def merge(
125139
self,
@@ -183,6 +197,18 @@ class _AsyncSessionTypingCommon(
183197
bind_arguments: Optional[Mapping[str, Any]] = ...,
184198
**kw: Any,
185199
) -> Any: ...
200+
async def scalars(
201+
self,
202+
statement: Executable,
203+
parameters: Optional[_ExecuteParams] = ...,
204+
execution_options: Optional[_ExecuteOptions] = ...,
205+
) -> ScalarResult: ...
206+
async def stream_scalars(
207+
self,
208+
statement: Executable,
209+
parameters: Optional[_ExecuteParams] = ...,
210+
execution_options: Optional[_ExecuteOptions] = ...,
211+
) -> AsyncScalarResult: ...
186212
@classmethod
187213
async def close_all(self) -> None: ...
188214

sqlalchemy-stubs/orm/session.pyi

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ from .._typing import _ExecuteParams
3030
from ..engine import Connection
3131
from ..engine import Engine
3232
from ..engine import Result
33+
from ..engine import ScalarResult
3334
from ..sql import ClauseElement
3435
from ..sql import Executable
3536
from ..sql.base import Options
@@ -129,6 +130,14 @@ class _SessionProtocol(
129130
bind_arguments: Optional[_BindArguments] = ...,
130131
**kw: Any,
131132
) -> Any: ...
133+
def scalars(
134+
self,
135+
statement: Executable,
136+
params: Optional[_ExecuteParams] = ...,
137+
execution_options: Optional[_ExecuteOptions] = ...,
138+
bind_arguments: Optional[_BindArguments] = ...,
139+
**kw: Any,
140+
) -> ScalarResult: ...
132141
def close(self) -> None: ...
133142
def refresh(
134143
self,
@@ -416,6 +425,14 @@ class _SessionTypingCommon(
416425
bind_arguments: Optional[_BindArguments] = ...,
417426
**kw: Any,
418427
) -> Any: ...
428+
def scalars(
429+
self,
430+
statement: Executable,
431+
params: Optional[_ExecuteParams] = ...,
432+
execution_options: Optional[_ExecuteOptions] = ...,
433+
bind_arguments: Optional[_BindArguments] = ...,
434+
**kw: Any,
435+
) -> ScalarResult: ...
419436

420437
class _SessionInTransactionTypingCommon:
421438
def in_transaction(self) -> bool: ...

test/files/async_stuff.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1+
from asyncio import current_task
2+
3+
from sqlalchemy import text
4+
from sqlalchemy.ext.asyncio import async_scoped_session
15
from sqlalchemy.ext.asyncio import AsyncSession
26
from sqlalchemy.ext.asyncio import create_async_engine
37
from sqlalchemy.orm import sessionmaker
4-
from sqlalchemy.orm import Session
58

69
engine = create_async_engine(...)
7-
async_session = sessionmaker(engine, class_=AsyncSession)
10+
SM = sessionmaker(engine, class_=AsyncSession)
11+
12+
async_session = AsyncSession(engine)
13+
14+
as_session = async_scoped_session(SM, current_task)
15+
16+
17+
async def go() -> None:
18+
r = await async_session.scalars(text("select 1"))
19+
r.first()
20+
sr = await async_session.stream_scalars(text("select 1"))
21+
await sr.all()
22+
r = await as_session.scalars(text("select 1"))
23+
r.first()
24+
sr = await as_session.stream_scalars(text("select 1"))
25+
await sr.all()
26+
27+
async with engine.connect() as conn:
28+
cr = await conn.scalars(text("select 1"))
29+
cr.first()
30+
scr = await conn.stream_scalars(text("select 1"))
31+
await scr.all()

test/files/sync_stuff.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from sqlalchemy import create_engine
2+
from sqlalchemy import text
3+
from sqlalchemy.orm import scoped_session
4+
from sqlalchemy.orm import Session
5+
from sqlalchemy.orm import sessionmaker
6+
7+
engine = create_engine("")
8+
SM = sessionmaker(engine)
9+
10+
session = Session(engine)
11+
12+
s_session = scoped_session(SM)
13+
14+
15+
def go() -> None:
16+
r = session.scalars(text("select 1"))
17+
r.first()
18+
r = s_session.scalars(text("select 1"))
19+
r.first()
20+
21+
with engine.connect() as conn:
22+
cr = conn.scalars(text("select 1"))
23+
cr.first()

0 commit comments

Comments
 (0)