Skip to content

Commit e4d92d6

Browse files
authored
Common execute params (#168)
* Add a single definition for all execution parameters * Use a single definition for execution options
1 parent c88c464 commit e4d92d6

14 files changed

+93
-91
lines changed

sqlalchemy-stubs/_typing.pyi

+6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from typing import Any
22
from typing import Generic
3+
from typing import Mapping
34
from typing import overload
5+
from typing import Sequence
46
from typing import Type
57
from typing import TypeVar
8+
from typing import Union
69

710
_T = TypeVar("_T")
811

@@ -15,3 +18,6 @@ class _TypeToInstance(Generic[_T]):
1518
def __set__(self, instance: None, value: Type[_T]) -> None: ...
1619
@overload
1720
def __set__(self, instance: object, value: _T) -> None: ...
21+
22+
_ExecuteParams = Union[Mapping[Any, Any], Sequence[Mapping[Any, Any]]]
23+
_ExecuteOptions = Mapping[Any, Any]

sqlalchemy-stubs/engine/base.pyi

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from typing import Any
2-
from typing import Dict
32
from typing import List
43
from typing import MutableMapping
54
from typing import Optional
@@ -18,7 +17,8 @@ from .interfaces import ExceptionContext
1817
from .interfaces import ExecutionContext
1918
from .url import URL
2019
from .. import log
21-
from .. import util
20+
from .._typing import _ExecuteOptions
21+
from .._typing import _ExecuteParams
2222
from ..exc import StatementError
2323
from ..pool import Pool
2424

@@ -29,8 +29,6 @@ _TConnection = TypeVar("_TConnection", bound=Connection)
2929
_TTransaction = TypeVar("_TTransaction", bound=Transaction)
3030
_TEngine = TypeVar("_TEngine", bound=Engine)
3131

32-
_ExecutionOptions: util.immutabledict[Any, Any]
33-
3432
class _ConnectionCallable(Protocol[_T_contra, _T_co]):
3533
def __call__(
3634
self, __connection: _T_contra, *args: Any, **kwargs: Any
@@ -55,15 +53,15 @@ class Connection(_ConnectionTypingCommon, Connectable):
5553
connection: Optional[_DBAPIConnection] = ...,
5654
close_with_result: bool = ...,
5755
_branch_from: Optional[Any] = ...,
58-
_execution_options: Optional[Dict[str, Any]] = ...,
56+
_execution_options: Optional[_ExecuteOptions] = ...,
5957
_dispatch: Optional[Any] = ...,
6058
_has_events: Optional[Any] = ...,
6159
) -> None: ...
6260
def schema_for_object(self, obj: Any) -> str: ...
6361
def __enter__(self: _TConnection) -> _TConnection: ...
6462
def __exit__(self, type_: Any, value: Any, traceback: Any) -> None: ...
6563
def execution_options(self: _TConnection, **opt: Any) -> _TConnection: ...
66-
def get_execution_options(self) -> Dict[str, Any]: ...
64+
def get_execution_options(self) -> _ExecuteOptions: ...
6765
@property
6866
def connection(self) -> _DBAPIConnection: ...
6967
def get_isolation_level(self) -> Any: ...
@@ -92,8 +90,8 @@ class Connection(_ConnectionTypingCommon, Connectable):
9290
def exec_driver_sql(
9391
self,
9492
statement: str,
95-
parameters: Optional[Any] = ...,
96-
execution_options: Optional[Any] = ...,
93+
parameters: Optional[_ExecuteParams] = ...,
94+
execution_options: Optional[_ExecuteOptions] = ...,
9795
) -> CursorResult: ...
9896
def transaction(
9997
self: _TConnection,
@@ -176,7 +174,7 @@ class _EngineTypingCommon:
176174
def driver(self) -> str: ...
177175
def clear_compiled_cache(self) -> None: ...
178176
def update_execution_options(self, **opt: Any) -> None: ...
179-
def get_execution_options(self) -> Dict[str, Any]: ...
177+
def get_execution_options(self) -> _ExecuteOptions: ...
180178

181179
class Engine(_EngineTypingCommon, Connectable, log.Identified):
182180
@property
@@ -190,7 +188,7 @@ class Engine(_EngineTypingCommon, Connectable, log.Identified):
190188
logging_name: Optional[str] = ...,
191189
echo: Optional[Union[bool, Literal["debug"]]] = ...,
192190
query_cache_size: int = ...,
193-
execution_options: Optional[Dict[str, Any]] = ...,
191+
execution_options: Optional[_ExecuteOptions] = ...,
194192
hide_parameters: bool = ...,
195193
) -> None: ...
196194
def execution_options(self, **opt: Any) -> OptionEngine: ...
@@ -248,7 +246,7 @@ class OptionEngineMixin:
248246
dispatch: Any = ...
249247
pool: Pool = ...
250248
def __init__(
251-
self, proxied: Engine, execution_options: Dict[str, Any]
249+
self, proxied: Engine, execution_options: _ExecuteOptions
252250
) -> None: ...
253251

254252
class OptionEngine(OptionEngineMixin, Engine): ...

sqlalchemy-stubs/engine/create.pyi

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ from typing_extensions import Literal
1111

1212
from .base import Engine
1313
from .url import URL
14+
from .._typing import _ExecuteOptions
1415
from ..future import Engine as FutureEngine
1516
from ..pool import Pool
1617

@@ -37,7 +38,7 @@ def create_engine(
3738
echo_pool: Union[bool, _Debug] = ...,
3839
enable_from_linting: bool = ...,
3940
encoding: str = ...,
40-
execution_options: Dict[Any, Any] = ...,
41+
execution_options: _ExecuteOptions = ...,
4142
future: Literal[True],
4243
hide_parameters: bool = ...,
4344
implicit_returning: bool = ...,
@@ -76,7 +77,7 @@ def create_engine(
7677
echo_pool: Union[bool, _Debug] = ...,
7778
enable_from_linting: bool = ...,
7879
encoding: str = ...,
79-
execution_options: Dict[Any, Any] = ...,
80+
execution_options: _ExecuteOptions = ...,
8081
future: Literal[False] = ...,
8182
hide_parameters: bool = ...,
8283
implicit_returning: bool = ...,
@@ -115,7 +116,7 @@ def create_engine(
115116
echo_pool: Union[bool, _Debug] = ...,
116117
enable_from_linting: bool = ...,
117118
encoding: str = ...,
118-
execution_options: Dict[Any, Any] = ...,
119+
execution_options: _ExecuteOptions = ...,
119120
future: Union[bool],
120121
hide_parameters: bool = ...,
121122
implicit_returning: bool = ...,

sqlalchemy-stubs/engine/cursor.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ from .result import ResultMetaData
1515
from .result import Row
1616
from .row import LegacyRow
1717
from .. import util as util
18+
from .._typing import _ExecuteOptions
1819

1920
_TCursorResult = TypeVar("_TCursorResult", bound=CursorResult)
2021

@@ -97,7 +98,7 @@ class BufferedRowCursorFetchStrategy(CursorFetchStrategy):
9798
def __init__(
9899
self,
99100
dbapi_cursor: _DBAPICursor,
100-
execution_options: Any,
101+
execution_options: _ExecuteOptions,
101102
growth_factor: int = ...,
102103
initial_buffer: Optional[Any] = ...,
103104
) -> None: ...

sqlalchemy-stubs/engine/default.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ from .cursor import CursorFetchStrategy
1616
from .url import URL
1717
from .. import pool
1818
from .. import util
19+
from .._typing import _ExecuteOptions
1920
from .._typing import _TypeToInstance
2021
from ..sql import compiler
2122
from ..sql.type_api import TypeEngine
@@ -223,7 +224,7 @@ class DefaultExecutionContext(interfaces.ExecutionContext):
223224
statement: Optional[str] = ...
224225
result_column_struct: Any = ...
225226
returned_default_rows: Any = ...
226-
execution_options: util.immutabledict[Any, Any] = ...
227+
execution_options: _ExecuteOptions = ...
227228
include_set_input_sizes: Any = ...
228229
exclude_set_input_sizes: Any = ...
229230
cursor_fetch_strategy: Any = ...

sqlalchemy-stubs/engine/events.pyi

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ from typing import Any
22

33
from .interfaces import Dialect
44
from .. import event
5+
from .._typing import _ExecuteOptions
56

67
class ConnectionEvents(event.Events):
78
def before_execute(
@@ -10,15 +11,15 @@ class ConnectionEvents(event.Events):
1011
clauseelement: Any,
1112
multiparams: Any,
1213
params: Any,
13-
execution_options: Any,
14+
execution_options: _ExecuteOptions,
1415
) -> None: ...
1516
def after_execute(
1617
self,
1718
conn: Any,
1819
clauseelement: Any,
1920
multiparams: Any,
2021
params: Any,
21-
execution_options: Any,
22+
execution_options: _ExecuteOptions,
2223
result: Any,
2324
) -> None: ...
2425
def before_cursor_execute(
@@ -42,9 +43,11 @@ class ConnectionEvents(event.Events):
4243
def handle_error(self, exception_context: Any) -> None: ...
4344
def engine_connect(self, conn: Any, branch: Any) -> None: ...
4445
def set_connection_execution_options(
45-
self, conn: Any, opts: Any
46+
self, conn: Any, opts: _ExecuteOptions
47+
) -> None: ...
48+
def set_engine_execution_options(
49+
self, engine: Any, opts: _ExecuteOptions
4650
) -> None: ...
47-
def set_engine_execution_options(self, engine: Any, opts: Any) -> None: ...
4851
def engine_disposed(self, engine: Any) -> None: ...
4952
def begin(self, conn: Any) -> None: ...
5053
def rollback(self, conn: Any) -> None: ...

sqlalchemy-stubs/ext/asyncio/engine.pyi

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from typing import Any
22
from typing import Callable
33
from typing import Generator
4-
from typing import Mapping
54
from typing import MutableMapping
65
from typing import NoReturn
76
from typing import Optional
@@ -10,6 +9,8 @@ from typing import TypeVar
109
from .base import ProxyComparable
1110
from .base import StartableContext
1211
from .result import AsyncResult
12+
from ..._typing import _ExecuteOptions
13+
from ..._typing import _ExecuteParams
1314
from ...engine import Dialect
1415
from ...engine import Result
1516
from ...engine import Transaction
@@ -75,26 +76,26 @@ class AsyncConnection(
7576
async def exec_driver_sql(
7677
self,
7778
statement: str,
78-
parameters: Optional[Mapping[Any, Any]] = ...,
79-
execution_options: Mapping[Any, Any] = ...,
79+
parameters: Optional[_ExecuteParams] = ...,
80+
execution_options: Optional[_ExecuteOptions] = ...,
8081
) -> Result: ...
8182
async def stream(
8283
self,
8384
statement: Executable,
84-
parameters: Optional[Mapping[Any, Any]] = ...,
85-
execution_options: Mapping[Any, Any] = ...,
85+
parameters: Optional[_ExecuteParams] = ...,
86+
execution_options: Optional[_ExecuteOptions] = ...,
8687
) -> AsyncResult: ...
8788
async def execute(
8889
self,
8990
statement: Executable,
90-
parameters: Optional[Mapping[Any, Any]] = ...,
91-
execution_options: Mapping[Any, Any] = ...,
91+
parameters: Optional[_ExecuteParams] = ...,
92+
execution_options: Optional[_ExecuteOptions] = ...,
9293
) -> Result: ...
9394
async def scalar(
9495
self,
9596
statement: Executable,
96-
parameters: Optional[Mapping[Any, Any]] = ...,
97-
execution_options: Mapping[Any, Any] = ...,
97+
parameters: Optional[_ExecuteParams] = ...,
98+
execution_options: Optional[_ExecuteOptions] = ...,
9899
) -> Any: ...
99100
async def run_sync(
100101
self, fn: Callable[..., Any], *arg: Any, **kw: Any

sqlalchemy-stubs/ext/asyncio/session.pyi

+14-25
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ from .base import StartableContext
1616
from .engine import AsyncConnection
1717
from .engine import AsyncEngine
1818
from .result import AsyncResult
19+
from ..._typing import _ExecuteOptions
20+
from ..._typing import _ExecuteParams
1921
from ...engine import Result
20-
from ...engine.base import _ExecutionOptions
2122
from ...orm import Session
2223
from ...orm.session import _BindArguments
2324
from ...orm.session import _SessionClassMethodNoIoTypingCommon
@@ -78,20 +79,16 @@ class _AsyncSessionProtocol(
7879
async def execute(
7980
self,
8081
statement: Executable,
81-
params: Optional[
82-
Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]
83-
] = ...,
84-
execution_options: _ExecutionOptions = ...,
82+
params: Optional[_ExecuteParams] = ...,
83+
execution_options: Optional[_ExecuteOptions] = ...,
8584
bind_arguments: Optional[_BindArguments] = ...,
8685
**kw: Any,
8786
) -> Result: ...
8887
async def scalar(
8988
self,
9089
statement: Executable,
91-
params: Optional[
92-
Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]
93-
] = ...,
94-
execution_options: _ExecutionOptions = ...,
90+
params: Optional[_ExecuteParams] = ...,
91+
execution_options: Optional[_ExecuteOptions] = ...,
9592
bind_arguments: Optional[_BindArguments] = ...,
9693
**kw: Any,
9794
) -> Any: ...
@@ -118,10 +115,8 @@ class _AsyncSessionProtocol(
118115
async def stream(
119116
self,
120117
statement: Any,
121-
params: Optional[
122-
Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]
123-
] = ...,
124-
execution_options: _ExecutionOptions = ...,
118+
params: Optional[_ExecuteParams] = ...,
119+
execution_options: Optional[_ExecuteOptions] = ...,
125120
bind_arguments: Optional[_BindArguments] = ...,
126121
**kw: Any,
127122
) -> Any: ...
@@ -147,10 +142,8 @@ class _AsyncSessionTypingCommon(
147142
async def execute(
148143
self,
149144
statement: Executable,
150-
params: Optional[
151-
Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]
152-
] = ...,
153-
execution_options: Optional[Mapping[Any, Any]] = ...,
145+
params: Optional[_ExecuteParams] = ...,
146+
execution_options: Optional[_ExecuteOptions] = ...,
154147
bind_arguments: Optional[Mapping[str, Any]] = ...,
155148
**kw: Any,
156149
) -> Result: ...
@@ -175,10 +168,8 @@ class _AsyncSessionTypingCommon(
175168
async def scalar(
176169
self,
177170
statement: Executable,
178-
params: Optional[
179-
Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]
180-
] = ...,
181-
execution_options: Mapping[Any, Any] = ...,
171+
params: Optional[_ExecuteParams] = ...,
172+
execution_options: Optional[_ExecuteOptions] = ...,
182173
bind_arguments: Optional[Mapping[str, Any]] = ...,
183174
**kw: Any,
184175
) -> Any: ...
@@ -206,10 +197,8 @@ class AsyncSession(
206197
async def stream(
207198
self,
208199
statement: Any,
209-
params: Optional[
210-
Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]
211-
] = ...,
212-
execution_options: Mapping[Any, Any] = ...,
200+
params: Optional[_ExecuteParams] = ...,
201+
execution_options: Optional[_ExecuteOptions] = ...,
213202
bind_arguments: Optional[Mapping[str, Any]] = ...,
214203
**kw: Any,
215204
) -> AsyncResult: ...

sqlalchemy-stubs/future/engine.pyi

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ from typing import Type
88
from typing import Union
99

1010
from .. import util
11+
from .._typing import _ExecuteOptions
12+
from .._typing import _ExecuteParams
1113
from ..engine.base import Connection as _LegacyConnection
1214
from ..engine.base import Engine as _LegacyEngine
1315
from ..engine.base import NestedTransaction
@@ -33,7 +35,7 @@ def create_engine(
3335
echo_pool: Union[bool, _Debug] = ...,
3436
enable_from_linting: bool = ...,
3537
encoding: str = ...,
36-
execution_options: Dict[Any, Any] = ...,
38+
execution_options: _ExecuteOptions = ...,
3739
hide_parameters: bool = ...,
3840
implicit_returning: bool = ...,
3941
isolation_level: _IsolationLevel = ...,
@@ -69,14 +71,14 @@ class Connection(_LegacyConnection):
6971
def execute( # type: ignore[override]
7072
self,
7173
statement: Any,
72-
parameters: Optional[Any] = ...,
73-
execution_options: Optional[Any] = ...,
74+
parameters: Optional[_ExecuteParams] = ...,
75+
execution_options: Optional[_ExecuteOptions] = ...,
7476
) -> Any: ...
7577
def scalar( # type: ignore[override]
7678
self,
7779
statement: Any,
78-
parameters: Optional[Any] = ...,
79-
execution_options: Optional[Any] = ...,
80+
parameters: Optional[_ExecuteParams] = ...,
81+
execution_options: Optional[_ExecuteOptions] = ...,
8082
) -> Any: ...
8183

8284
class Engine(_LegacyEngine):

0 commit comments

Comments
 (0)