Skip to content

Commit d4d0a06

Browse files
authored
Improve engine.default (#97)
* Improve `engine.default` * Updates based on PR feedback
1 parent cc73545 commit d4d0a06

File tree

1 file changed

+167
-121
lines changed

1 file changed

+167
-121
lines changed

sqlalchemy-stubs/engine/default.pyi

Lines changed: 167 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,48 @@
11
from typing import Any
2+
from typing import Dict
3+
from typing import List
4+
from typing import Mapping
25
from typing import Optional
6+
from typing import Pattern
7+
from typing import Sequence
8+
from typing import Tuple
9+
from typing import Type
10+
from typing import TypeVar
11+
from typing import Union
312

4-
from . import characteristics as characteristics
5-
from . import interfaces as interfaces
6-
from .. import event as event
7-
from .. import exc as exc
8-
from .. import pool as pool
9-
from .. import processors as processors
10-
from .. import types as sqltypes
11-
from .. import util as util
12-
from ..sql import compiler as compiler
13-
from ..sql import expression as expression
14-
from ..sql.elements import quoted_name as quoted_name
13+
from . import base
14+
from . import interfaces
15+
from .cursor import CursorFetchStrategy
16+
from .url import URL
17+
from .. import pool
18+
from .. import util
19+
from ..sql import compiler
20+
from ..sql.type_api import TypeEngine
21+
from ..util import langhelpers
1522

16-
AUTOCOMMIT_REGEXP: Any
17-
SERVER_SIDE_CURSOR_RE: Any
18-
CACHE_HIT: Any
19-
CACHE_MISS: Any
20-
CACHING_DISABLED: Any
21-
NO_CACHE_KEY: Any
23+
_T = TypeVar("_T")
24+
25+
AUTOCOMMIT_REGEXP: Pattern[str]
26+
SERVER_SIDE_CURSOR_RE: Pattern[str]
27+
CACHE_HIT: langhelpers._symbol
28+
CACHE_MISS: langhelpers._symbol
29+
CACHING_DISABLED: langhelpers._symbol
30+
NO_CACHE_KEY: langhelpers._symbol
2231

2332
class DefaultDialect(interfaces.Dialect):
24-
statement_compiler: Any = ...
25-
ddl_compiler: Any = ...
26-
type_compiler: Any = ...
27-
preparer: Any = ...
33+
statement_compiler: Type[compiler.SQLCompiler] = ...
34+
ddl_compiler: Type[compiler.DDLCompiler] = ...
35+
type_compiler: Type[compiler.GenericTypeCompiler] = ...
36+
preparer: Type[compiler.IdentifierPreparer] = ...
37+
execution_ctx_cls: Type[DefaultExecutionContext] = ...
2838
supports_alter: bool = ...
2939
supports_comments: bool = ...
3040
inline_comments: bool = ...
3141
use_setinputsizes: bool = ...
3242
default_sequence_base: int = ...
33-
execute_sequence_format: Any = ...
43+
execute_sequence_format: Union[
44+
Type[Tuple[Any, ...]], Type[List[Any]]
45+
] = ...
3446
supports_views: bool = ...
3547
supports_sequences: bool = ...
3648
sequences_optional: bool = ...
@@ -45,21 +57,21 @@ class DefaultDialect(interfaces.Dialect):
4557
non_native_boolean_check_constraint: bool = ...
4658
supports_simple_order_by_label: bool = ...
4759
tuple_in_values: bool = ...
48-
connection_characteristics: Any = ...
49-
engine_config_types: Any = ...
60+
connection_characteristics: util.immutabledict[str, Any] = ...
61+
engine_config_types: util.immutabledict[str, Any] = ...
5062
supports_native_decimal: bool = ...
5163
supports_unicode_statements: bool = ...
5264
supports_unicode_binds: bool = ...
53-
returns_unicode_strings: Any = ...
54-
description_encoding: Any = ...
65+
returns_unicode_strings: langhelpers._symbol = ...
66+
description_encoding: Optional[str] = ...
5567
name: str = ...
5668
max_identifier_length: int = ...
57-
isolation_level: Any = ...
58-
max_index_name_length: Any = ...
59-
max_constraint_name_length: Any = ...
69+
isolation_level: Optional[Any] = ...
70+
max_index_name_length: Optional[int] = ...
71+
max_constraint_name_length: Optional[int] = ...
6072
supports_sane_rowcount: bool = ...
6173
supports_sane_multi_rowcount: bool = ...
62-
colspecs: Any = ...
74+
colspecs: Dict[Type[TypeEngine[Any]], Type[TypeEngine[Any]]] = ...
6375
default_paramstyle: str = ...
6476
supports_default_values: bool = ...
6577
supports_empty_insert: bool = ...
@@ -68,119 +80,141 @@ class DefaultDialect(interfaces.Dialect):
6880
supports_server_side_cursors: bool = ...
6981
server_side_cursors: bool = ...
7082
supports_for_update_of: bool = ...
71-
server_version_info: Any = ...
72-
default_schema_name: Any = ...
83+
server_version_info: Optional[Tuple[Any, ...]] = ...
84+
default_schema_name: Optional[str] = ...
7385
construct_arguments: Any = ...
7486
requires_name_normalize: bool = ...
75-
reflection_options: Any = ...
76-
dbapi_exception_translation_map: Any = ...
87+
reflection_options: Tuple[Any, ...] = ...
88+
dbapi_exception_translation_map: util.immutabledict[str, str] = ...
7789
is_async: bool = ...
78-
CACHE_HIT: Any = ...
79-
CACHE_MISS: Any = ...
80-
CACHING_DISABLED: Any = ...
81-
NO_CACHE_KEY: Any = ...
82-
convert_unicode: Any = ...
83-
encoding: Any = ...
90+
CACHE_HIT: langhelpers._symbol = ...
91+
CACHE_MISS: langhelpers._symbol = ...
92+
CACHING_DISABLED: langhelpers._symbol = ...
93+
NO_CACHE_KEY: langhelpers._symbol = ...
94+
convert_unicode: bool = ...
95+
encoding: str = ...
8496
positional: bool = ...
8597
dbapi: Any = ...
86-
paramstyle: Any = ...
87-
identifier_preparer: Any = ...
88-
case_sensitive: Any = ...
89-
label_length: Any = ...
90-
compiler_linting: Any = ...
98+
paramstyle: str = ...
99+
identifier_preparer: compiler.IdentifierPreparer = ...
100+
case_sensitive: bool = ...
101+
label_length: Optional[int] = ...
102+
compiler_linting: int = ...
91103
def __init__(
92104
self,
93105
convert_unicode: bool = ...,
94106
encoding: str = ...,
95-
paramstyle: Optional[Any] = ...,
107+
paramstyle: Optional[str] = ...,
96108
dbapi: Optional[Any] = ...,
97109
implicit_returning: Optional[Any] = ...,
98110
case_sensitive: bool = ...,
99111
supports_native_boolean: Optional[Any] = ...,
100112
max_identifier_length: Optional[Any] = ...,
101113
label_length: Optional[Any] = ...,
102-
compiler_linting: Any = ...,
114+
compiler_linting: langhelpers._symbol = ...,
103115
server_side_cursors: bool = ...,
104116
**kwargs: Any,
105117
) -> None: ...
106118
@property
107-
def dialect_description(self): ...
119+
def dialect_description(self) -> str: ...
108120
@property
109-
def supports_sane_rowcount_returning(self): ...
121+
def supports_sane_rowcount_returning(self) -> bool: ...
110122
@classmethod
111-
def get_pool_class(cls, url: Any): ...
112-
def get_dialect_pool_class(self, url: Any): ...
123+
def get_pool_class(cls, url: URL) -> Type[pool.Pool]: ...
124+
def get_dialect_pool_class(self, url: URL) -> Type[pool.Pool]: ...
113125
@classmethod
114126
def load_provisioning(cls) -> None: ...
115-
default_isolation_level: Any = ...
116-
def initialize(self, connection: Any) -> None: ...
117-
def on_connect(self) -> None: ...
118-
def get_default_isolation_level(self, dbapi_conn: Any): ...
119-
def type_descriptor(self, typeobj: Any): ...
127+
def initialize(self, connection: base.Connection) -> None: ...
128+
def on_connect(self) -> Optional[interfaces._OnConnect]: ...
129+
def get_default_isolation_level(
130+
self, dbapi_conn: interfaces._DBAPIConnection
131+
) -> Any: ...
132+
def type_descriptor(self, typeobj: Type[TypeEngine[Any]]) -> Any: ... # type: ignore[override]
120133
def has_index(
121134
self,
122-
connection: Any,
123-
table_name: Any,
124-
index_name: Any,
125-
schema: Optional[Any] = ...,
126-
): ...
135+
connection: base.Connection,
136+
table_name: str,
137+
index_name: str,
138+
schema: Optional[str] = ...,
139+
) -> bool: ...
127140
def validate_identifier(self, ident: Any) -> None: ...
128-
def connect(self, *cargs: Any, **cparams: Any): ...
129-
def create_connect_args(self, url: Any): ...
141+
def connect(
142+
self, *cargs: Any, **cparams: Any
143+
) -> interfaces._DBAPIConnection: ...
144+
def create_connect_args(
145+
self, url: Any
146+
) -> Tuple[Sequence[Any], Mapping[str, Any]]: ...
130147
def set_engine_execution_options(self, engine: Any, opts: Any) -> None: ...
131148
def set_connection_execution_options(
132149
self, connection: Any, opts: Any
133150
) -> None: ...
134-
def do_begin(self, dbapi_connection: Any) -> None: ...
135-
def do_rollback(self, dbapi_connection: Any) -> None: ...
136-
def do_commit(self, dbapi_connection: Any) -> None: ...
137-
def do_close(self, dbapi_connection: Any) -> None: ...
138-
def do_ping(self, dbapi_connection: Any): ...
139-
def create_xid(self): ...
140-
def do_savepoint(self, connection: Any, name: Any) -> None: ...
141-
def do_rollback_to_savepoint(self, connection: Any, name: Any) -> None: ...
142-
def do_release_savepoint(self, connection: Any, name: Any) -> None: ...
151+
def do_begin(
152+
self, dbapi_connection: interfaces._DBAPIConnection
153+
) -> None: ...
154+
def do_rollback(
155+
self, dbapi_connection: interfaces._DBAPIConnection
156+
) -> None: ...
157+
def do_commit(
158+
self, dbapi_connection: interfaces._DBAPIConnection
159+
) -> None: ...
160+
def do_close(
161+
self, dbapi_connection: interfaces._DBAPIConnection
162+
) -> None: ...
163+
def do_ping(
164+
self, dbapi_connection: interfaces._DBAPIConnection
165+
) -> bool: ...
166+
def create_xid(self) -> str: ...
167+
def do_savepoint(self, connection: base.Connection, name: str) -> None: ...
168+
def do_rollback_to_savepoint(
169+
self, connection: base.Connection, name: str
170+
) -> None: ...
171+
def do_release_savepoint(
172+
self, connection: base.Connection, name: str
173+
) -> None: ...
143174
def do_executemany(
144175
self,
145-
cursor: Any,
176+
cursor: interfaces._DBAPICursor,
146177
statement: Any,
147178
parameters: Any,
148-
context: Optional[Any] = ...,
179+
context: Optional[interfaces.ExecutionContext] = ...,
149180
) -> None: ...
150181
def do_execute(
151182
self,
152-
cursor: Any,
183+
cursor: interfaces._DBAPICursor,
153184
statement: Any,
154185
parameters: Any,
155-
context: Optional[Any] = ...,
186+
context: Optional[interfaces.ExecutionContext] = ...,
156187
) -> None: ...
157-
def do_execute_no_params(
158-
self, cursor: Any, statement: Any, context: Optional[Any] = ...
188+
def do_execute_no_params( # type: ignore[override]
189+
self,
190+
cursor: interfaces._DBAPICursor,
191+
statement: Any,
192+
context: Optional[interfaces.ExecutionContext] = ...,
159193
) -> None: ...
160-
def is_disconnect(self, e: Any, connection: Any, cursor: Any): ...
161-
def reset_isolation_level(self, dbapi_conn: Any) -> None: ...
162-
def normalize_name(self, name: Any): ...
163-
def denormalize_name(self, name: Any): ...
164-
165-
class _RendersLiteral:
166-
def literal_processor(self, dialect: Any): ...
167-
168-
class _StrDateTime(_RendersLiteral, sqltypes.DateTime): ...
169-
class _StrDate(_RendersLiteral, sqltypes.Date): ...
170-
class _StrTime(_RendersLiteral, sqltypes.Time): ...
194+
def is_disconnect(
195+
self,
196+
e: Any,
197+
connection: base.Connection,
198+
cursor: interfaces._DBAPICursor,
199+
) -> bool: ...
200+
def reset_isolation_level(
201+
self, dbapi_conn: interfaces._DBAPIConnection
202+
) -> None: ...
203+
def normalize_name(self, name: Optional[str]) -> Optional[str]: ...
204+
def denormalize_name(self, name: Optional[str]) -> Optional[str]: ...
171205

172206
class StrCompileDialect(DefaultDialect):
173-
statement_compiler: Any = ...
174-
ddl_compiler: Any = ...
175-
type_compiler: Any = ...
176-
preparer: Any = ...
207+
statement_compiler: Type[compiler.StrSQLCompiler] = ...
208+
ddl_compiler: Type[compiler.DDLCompiler] = ...
209+
type_compiler: Type[compiler.StrSQLTypeCompiler] = ...
210+
preparer: Type[compiler.IdentifierPreparer] = ...
177211
supports_sequences: bool = ...
178212
sequences_optional: bool = ...
179213
preexecute_autoincrement_sequences: bool = ...
180214
implicit_returning: bool = ...
181215
supports_native_boolean: bool = ...
182216
supports_simple_order_by_label: bool = ...
183-
colspecs: Any = ...
217+
colspecs: Dict[Type[TypeEngine[Any]], Type[TypeEngine[Any]]] = ...
184218

185219
class DefaultExecutionContext(interfaces.ExecutionContext):
186220
isinsert: bool = ...
@@ -190,45 +224,57 @@ class DefaultExecutionContext(interfaces.ExecutionContext):
190224
is_text: bool = ...
191225
isddl: bool = ...
192226
executemany: bool = ...
193-
compiled: Any = ...
194-
statement: Any = ...
227+
compiled: Optional[compiler.Compiled] = ...
228+
statement: Optional[str] = ...
195229
result_column_struct: Any = ...
196230
returned_default_rows: Any = ...
197-
execution_options: Any = ...
231+
execution_options: util.immutabledict[Any, Any] = ...
198232
include_set_input_sizes: Any = ...
199233
exclude_set_input_sizes: Any = ...
200234
cursor_fetch_strategy: Any = ...
201235
cache_stats: Any = ...
202236
invoked_statement: Any = ...
203237
cache_hit: Any = ...
204-
def identifier_preparer(self): ...
205-
def engine(self): ...
206-
def postfetch_cols(self): ...
207-
def prefetch_cols(self): ...
238+
@util.memoized_property
239+
def identifier_preparer(self) -> compiler.IdentifierPreparer: ...
240+
@util.memoized_property
241+
def engine(self) -> base.Engine: ...
242+
@util.memoized_property
243+
def postfetch_cols(self) -> List[Any]: ... # type: ignore[override]
244+
@util.memoized_property
245+
def prefetch_cols(self) -> List[Any]: ... # type: ignore[override]
246+
@util.memoized_property
208247
def returning_cols(self) -> None: ...
209-
def no_parameters(self): ...
210-
def should_autocommit(self): ...
248+
@util.memoized_property
249+
def no_parameters(self) -> bool: ...
250+
@util.memoized_property
251+
def should_autocommit(self) -> bool: ... # type: ignore[override]
211252
@property
212-
def connection(self): ...
213-
def should_autocommit_text(self, statement: Any): ...
214-
def create_cursor(self): ...
215-
def create_default_cursor(self): ...
216-
def create_server_side_cursor(self) -> None: ...
253+
def connection(self) -> base.Connection: ... # type: ignore[override]
254+
def should_autocommit_text(self, statement: str) -> bool: ...
255+
def create_cursor(self) -> interfaces._DBAPICursor: ...
256+
def create_default_cursor(self) -> interfaces._DBAPICursor: ...
257+
def create_server_side_cursor(self) -> interfaces._DBAPICursor: ...
217258
def pre_exec(self) -> None: ...
218-
def get_out_parameter_values(self, names: Any) -> None: ...
259+
def get_out_parameter_values(
260+
self, names: Sequence[str]
261+
) -> Sequence[Any]: ...
219262
def post_exec(self) -> None: ...
220-
def get_result_processor(self, type_: Any, colname: Any, coltype: Any): ...
221-
def get_lastrowid(self): ...
222-
def handle_dbapi_exception(self, e: Any) -> None: ...
263+
def get_result_processor(
264+
self, type_: Any, colname: Any, coltype: Any
265+
) -> CursorFetchStrategy: ...
266+
def get_lastrowid(self) -> Any: ...
267+
def handle_dbapi_exception(self, e: BaseException) -> None: ...
223268
@property
224-
def rowcount(self): ...
225-
def supports_sane_rowcount(self): ...
226-
def supports_sane_multi_rowcount(self): ...
227-
def inserted_primary_key_rows(self): ...
228-
def lastrow_has_defaults(self): ...
229-
current_parameters: Any = ...
269+
def rowcount(self) -> int: ...
270+
def supports_sane_rowcount(self) -> bool: ...
271+
def supports_sane_multi_rowcount(self) -> bool: ...
272+
@util.memoized_property
273+
def inserted_primary_key_rows(self) -> Any: ...
274+
def lastrow_has_defaults(self) -> bool: ...
275+
current_parameters: Optional[Dict[Any, Any]] = ...
230276
def get_current_parameters(
231277
self, isolate_multiinsert_groups: bool = ...
232-
): ...
233-
def get_insert_default(self, column: Any): ...
234-
def get_update_default(self, column: Any): ...
278+
) -> Optional[Dict[Any, Any]]: ...
279+
def get_insert_default(self, column: Any) -> Optional[Any]: ...
280+
def get_update_default(self, column: Any) -> Optional[Any]: ...

0 commit comments

Comments
 (0)