Skip to content

Commit b772662

Browse files
bryanforbesilevkivskyi
authored andcommitted
Various updates to sqlalchemy.sql typings (#46)
1 parent d05a736 commit b772662

File tree

11 files changed

+300
-228
lines changed

11 files changed

+300
-228
lines changed
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
from typing import Any, Dict
1+
from typing import Any, Dict, Type, TypeVar, Generic
22

3-
class Annotated(object):
3+
_T = TypeVar('_T')
4+
5+
class Annotated(Generic[_T]):
6+
__element: _T
47
def __new__(cls, *args): ...
5-
def __init__(self, element, values) -> None: ...
8+
def __init__(self, element: _T, values: Any) -> None: ...
69
def __hash__(self): ...
710
def __eq__(self, other): ...
811
def _annotate(self, values): ...
@@ -12,4 +15,4 @@ class Annotated(object):
1215
def _constructor(self): ...
1316
def _clone(self): ...
1417

15-
annotated_classes: Dict[Any, Any] = ...
18+
annotated_classes: Dict[Type[Any], Type[Annotated[Any]]] = ...

sqlalchemy-stubs/sql/base.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ NO_ARG: util.symbol = ...
1414
_I = TypeVar('_I', bound=Immutable)
1515

1616
class Immutable(object):
17-
def unique_params(self, *optionaldict: Any, **kwargs: Any): ...
18-
def params(self, *optionaldict: Any, **kwargs: Any): ...
17+
def unique_params(self, *optionaldict: Any, **kwargs: Any) -> Any: ...
18+
def params(self, *optionaldict: Any, **kwargs: Any) -> Any: ...
1919
def _clone(self: _I) -> _I: ...
2020

2121
class _DialectArgView(MutableMapping[str, Any]):

sqlalchemy-stubs/sql/compiler.pyi

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Optional, Set, Dict, List, Set, Pattern, TypeVar, Type
1+
from typing import Any, Optional, Union, Set, Dict, List, Set, Pattern, TypeVar, Type, Mapping, Tuple, Callable
22
from .. import util, engine
33
from .elements import ClauseElement, BindParameter
44
from .type_api import TypeEngine
@@ -17,21 +17,22 @@ COMPOUND_KEYWORDS: Dict[Any, str] = ...
1717
class Compiled(object):
1818
execution_options: Any = ...
1919
dialect: engine.Dialect = ...
20-
bind: Any = ...
21-
preparer: Any = ...
20+
bind: Optional[Union[engine.Engine, engine.Connection]] = ...
21+
preparer: Optional[IdentifierPreparer] = ...
2222
statement: ClauseElement = ...
2323
can_execute: Optional[bool] = ...
2424
string: Optional[str] = ...
25-
def __init__(self, dialect: engine.Dialect, statement: ClauseElement, bind: Optional[Any] = ...,
26-
schema_translate_map: Optional[Any] = ..., compile_kwargs: Any = ...) -> None: ...
25+
def __init__(self, dialect: engine.Dialect, statement: ClauseElement,
26+
bind: Optional[Union[engine.Engine, engine.Connection]] = ...,
27+
schema_translate_map: Optional[Any] = ..., compile_kwargs: Mapping[Any, Any] = ...) -> None: ...
2728
def compile(self) -> str: ...
2829
@property
29-
def sql_compiler(self) -> Any: ...
30+
def sql_compiler(self) -> SQLCompiler: ...
3031
def process(self, obj: Any, **kwargs: Any) -> str: ...
3132
def __str__(self) -> str: ...
32-
def construct_params(self, params: Optional[Dict[str, Any]] = ...) -> Any: ...
33+
def construct_params(self, params: Optional[Dict[str, Any]] = ...) -> Dict[str, Any]: ...
3334
@property
34-
def params(self) -> Optional[Dict[str, Any]]: ...
35+
def params(self) -> Dict[str, Any]: ...
3536
def execute(self, *multiparams: Any, **params: Any) -> engine.ResultProxy: ...
3637
def scalar(self, *multiparams: Any, **params: Any) -> Any: ...
3738

@@ -50,35 +51,35 @@ class SQLCompiler(Compiled):
5051
isinsert: bool = ...
5152
isupdate: bool = ...
5253
isplaintext: bool = ...
53-
returning: Any = ...
54+
returning: bool = ...
5455
returning_precedes_values: bool = ...
5556
render_table_with_column_in_update_from: bool = ...
5657
ansi_bind_rules: bool = ...
57-
insert_prefetch: Any = ...
58-
update_prefetch: Any = ...
58+
insert_prefetch: Tuple[Any, ...] = ...
59+
update_prefetch: Tuple[Any, ...] = ...
5960
column_keys: Optional[List[str]] = ...
6061
inline: bool = ...
6162
binds: Dict[str, BindParameter[Any]] = ...
62-
bind_names: Any = ...
63-
stack: Any = ...
64-
positional: Any = ...
65-
positiontup: Any = ...
66-
bindtemplate: Any = ...
67-
ctes: Any = ...
68-
label_length: Any = ...
69-
anon_map: Any = ...
70-
truncated_names: Any = ...
63+
bind_names: Dict[BindParameter[Any], str] = ...
64+
stack: List[Dict[str, Any]] = ...
65+
positional: bool = ...
66+
positiontup: List[Any] = ...
67+
bindtemplate: str = ...
68+
ctes: Optional[util.OrderedDict[str, Any]] = ...
69+
label_length: int = ...
70+
anon_map: util.PopulateDict = ...
71+
truncated_names: Dict[str, Any] = ...
7172
def __init__(self, dialect: engine.Dialect, statement: ClauseElement, column_keys: Optional[List[str]] = ...,
7273
inline: bool = ..., **kwargs: Any) -> None: ...
7374
@property
7475
def prefetch(self) -> List[Any]: ...
7576
def is_subquery(self) -> bool: ...
7677
@property
7778
def sql_compiler(self: _SQLC) -> _SQLC: ...
78-
def construct_params(self, params: Optional[Any] = ..., _group_number: Optional[Any] = ...,
79-
_check: bool = ...) -> Optional[Dict[str, Any]]: ...
79+
def construct_params(self, params: Optional[Dict[str, Any]] = ..., _group_number: Optional[Any] = ...,
80+
_check: bool = ...) -> Dict[str, Any]: ...
8081
@property
81-
def params(self) -> Optional[Dict[str, Any]]: ...
82+
def params(self) -> Dict[str, Any]: ...
8283
def default_from(self) -> str: ...
8384
def visit_grouping(self, grouping: Any, asfrom: bool = ..., **kwargs: Any) -> str: ...
8485
def visit_label_reference(self, element, within_columns_clause: bool = ..., **kwargs: Any) -> str: ...
@@ -178,10 +179,12 @@ class SQLCompiler(Compiled):
178179
def visit_release_savepoint(self, savepoint_stmt) -> str: ...
179180

180181
class StrSQLCompiler(SQLCompiler):
181-
def visit_getitem_binary(self, binary, operator, **kw): ...
182-
def visit_json_getitem_op_binary(self, binary, operator, **kw): ...
183-
def visit_json_path_getitem_op_binary(self, binary, operator, **kw): ...
184-
def returning_clause(self, stmt, returning_cols): ...
182+
def visit_getitem_binary(self, binary, operator, **kw: Any) -> str: ...
183+
def visit_json_getitem_op_binary(self, binary, operator, **kw: Any) -> str: ...
184+
def visit_json_path_getitem_op_binary(self, binary, operator, **kw: Any) -> str: ...
185+
def returning_clause(self, stmt, returning_cols) -> str: ...
186+
def update_from_clause(self, update_stmt, from_table, extra_froms, from_hints, **kw: Any) -> str: ...
187+
def delete_extra_from_clause(self, update_stmt, from_table, extra_froms, from_hints, **kw: Any) -> str: ...
185188

186189
class DDLCompiler(Compiled):
187190
@property
@@ -271,7 +274,7 @@ class IdentifierPreparer(object):
271274
reserved_words: Set[str] = ...
272275
legal_characters: Pattern[str] = ...
273276
illegal_initial_characters: Pattern[str] = ...
274-
schema_for_object: Any = ...
277+
schema_for_object: Callable[[Any], str] = ...
275278
dialect: engine.Dialect = ...
276279
initial_quote: str = ...
277280
final_quote: str = ...

sqlalchemy-stubs/sql/crud.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from typing import Any
1+
from .. import util
22

3-
REQUIRED: Any = ...
4-
ISINSERT: Any = ...
5-
ISUPDATE: Any = ...
6-
ISDELETE: Any = ...
3+
REQUIRED: util.symbol = ...
4+
ISINSERT: util.symbol = ...
5+
ISUPDATE: util.symbol = ...
6+
ISDELETE: util.symbol = ...

sqlalchemy-stubs/sql/ddl.pyi

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
1-
from typing import Any, Optional, Union, List, Dict, TypeVar, Generic, Sequence, Callable, Tuple
1+
from typing import Any, Optional, Union, List, Dict, TypeVar, Generic, Sequence, Callable, Tuple, Set
22
from typing_extensions import Protocol
33
from .elements import ClauseElement as ClauseElement, ColumnElement
44
from .base import Executable as Executable, SchemaVisitor as SchemaVisitor
55
from .schema import Table, MetaData, SchemaItem, ForeignKeyConstraint, ForeignKey
66
from .compiler import IdentifierPreparer
77
from .type_api import TypeEngine
8-
from .. import engine
8+
from ..engine import Dialect, Engine, Connection, ResultProxy, Connectable
99

1010
_T = TypeVar('_T')
1111

1212
class _DDLCompiles(ClauseElement): ...
1313

1414
class _DDLCallable(Protocol):
15-
def __call__(self, ddl: DDLElement, target: Optional[Union[Table, MetaData]], bind: engine.Connection,
15+
def __call__(self, ddl: DDLElement, target: Optional[Union[Table, MetaData]], bind: Connection,
1616
tables: Optional[List[Any]] = ..., state: Optional[Any] = ...,
1717
checkfirst: bool = ...) -> bool: ...
1818

1919
class _DDLOnCallback(Protocol):
2020
def __call__(self, ddl: DDLElement, event: Optional[str], target: Optional[Union[Table, MetaData]],
21-
connection: engine.Connection, tables: Optional[List[Any]] = ...) -> bool: ...
21+
connection: Connection, tables: Optional[List[Any]] = ...) -> bool: ...
2222

2323
_DDLE = TypeVar('_DDLE', bound=DDLElement)
2424

2525
class DDLElement(Executable, _DDLCompiles):
2626
target: Optional[SchemaItem] = ...
2727
on: Optional[Union[str, Tuple[str, ...], _DDLOnCallback]] = ...
28-
dialect: Optional[engine.Dialect] = ...
28+
dialect: Optional[Dialect] = ...
2929
callable_: Optional[_DDLCallable] = ...
30-
bind: Optional[engine.Connection] = ...
31-
def execute(self, bind: Optional[engine.Connectable] = ..., # type: ignore # incompatible with Executable.execute
32-
target: Optional[SchemaItem] = ...) -> Optional[engine.ResultProxy]: ...
30+
bind: Optional[Union[Engine, Connection]] = ...
31+
def execute(self, bind: Optional[Union[Engine, Connection]] = ..., # type: ignore # incompatible with Executable.execute
32+
target: Optional[SchemaItem] = ...) -> Optional[ResultProxy]: ...
3333
def execute_at(self, event_name: str, target: SchemaItem) -> None: ...
3434
def against(self: _DDLE, target: SchemaItem) -> _DDLE: ...
3535
state: Any = ...
36-
def execute_if(self: _DDLE, dialect: Optional[engine.Dialect] = ..., callable_: Optional[_DDLCallable] = ...,
36+
def execute_if(self: _DDLE, dialect: Optional[Dialect] = ..., callable_: Optional[_DDLCallable] = ...,
3737
state: Optional[Any] = ...) -> _DDLE: ...
38-
def __call__(self, target, bind, **kw): ...
38+
def __call__(self, target: Optional[Union[Table, MetaData]], bind: Connection, **kw: Any) -> Optional[ResultProxy]: ...
3939

4040
class DDL(DDLElement):
4141
__visit_name__: str = ...
4242
statement: str = ...
4343
context: Dict[Any, Any] = ...
4444
on: Optional[Union[str, Tuple[str, ...], _DDLOnCallback]] = ...
4545
def __init__(self, statement: str, on: Optional[Union[str, Tuple[str, ...], _DDLOnCallback]] = ...,
46-
context: Optional[Dict[Any, Any]] = ..., bind: Optional[engine.Connectable] = ...) -> None: ...
46+
context: Optional[Dict[Any, Any]] = ..., bind: Optional[Connectable] = ...) -> None: ...
4747

4848
class _CreateDropBase(DDLElement, Generic[_T]):
4949
element: _T = ...
50-
bind: Optional[engine.Connection] = ...
50+
bind: Optional[Connection] = ...
5151
def __init__(self, element: _T, on: Optional[Union[str, Tuple[str, ...], _DDLOnCallback]] = ...,
52-
bind: Optional[engine.Connection] = ...) -> None: ...
52+
bind: Optional[Connection] = ...) -> None: ...
5353

5454
class CreateSchema(_CreateDropBase[str]):
5555
__visit_name__: str = ...
@@ -67,7 +67,7 @@ class CreateTable(_CreateDropBase[Table]):
6767
columns: List[CreateColumn[Any]] = ...
6868
include_foreign_key_constraints: Optional[Sequence[ForeignKeyConstraint]] = ...
6969
def __init__(self, element: Table, on: Optional[Union[str, Tuple[str, ...], _DDLOnCallback]] = ...,
70-
bind: Optional[engine.Connectable] = ...,
70+
bind: Optional[Connectable] = ...,
7171
include_foreign_key_constraints: Optional[Sequence[ForeignKeyConstraint]] = ...) -> None: ...
7272

7373
class _DropView(_CreateDropBase[str]):
@@ -100,19 +100,19 @@ class AddConstraint(_CreateDropBase[str]):
100100
class DropConstraint(_CreateDropBase[str]):
101101
__visit_name__: str = ...
102102
cascade: bool = ...
103-
def __init__(self, element: str, cascade: bool = ..., **kw) -> None: ...
103+
def __init__(self, element: str, cascade: bool = ..., **kw: Any) -> None: ...
104104

105105
class DDLBase(SchemaVisitor):
106-
connection: engine.Connection = ...
107-
def __init__(self, connection: engine.Connection) -> None: ...
106+
connection: Connection = ...
107+
def __init__(self, connection: Connection) -> None: ...
108108

109109
class SchemaGenerator(DDLBase):
110110
checkfirst: bool = ...
111111
tables: Optional[List[Table]] = ...
112112
preparer: IdentifierPreparer = ...
113-
dialect: engine.Dialect = ...
113+
dialect: Dialect = ...
114114
memo: Dict[Any, Any] = ...
115-
def __init__(self, dialect: engine.Dialect, connection: engine.Connection, checkfirst: bool = ...,
115+
def __init__(self, dialect: Dialect, connection: Connection, checkfirst: bool = ...,
116116
tables: Optional[List[Table]] = ..., **kwargs: Any) -> None: ...
117117
def visit_metadata(self, metadata: MetaData) -> None: ...
118118
def visit_table(self, table: Table, create_ok: bool = ...,
@@ -126,9 +126,9 @@ class SchemaDropper(DDLBase):
126126
checkfirst: bool = ...
127127
tables: Optional[List[Table]] = ...
128128
preparer: IdentifierPreparer = ...
129-
dialect: engine.Dialect = ...
129+
dialect: Dialect = ...
130130
memo: Dict[Any, Any] = ...
131-
def __init__(self, dialect: engine.Dialect, connection: engine.Connection, checkfirst: bool = ...,
131+
def __init__(self, dialect: Dialect, connection: Connection, checkfirst: bool = ...,
132132
tables: Optional[List[Table]] = ..., **kwargs: Any) -> None: ...
133133
def visit_metadata(self, metadata: MetaData) -> None: ...
134134
def visit_index(self, index: str) -> None: ...
@@ -137,7 +137,11 @@ class SchemaDropper(DDLBase):
137137
def visit_sequence(self, sequence: str, drop_ok: bool = ...) -> None: ...
138138

139139
def sort_tables(tables: Sequence[Table], skip_fn: Optional[Callable[[ForeignKey], bool]] = ...,
140-
extra_dependencies: Optional[Sequence[Tuple[Table, Table]]] = ...): ...
140+
extra_dependencies: Optional[Sequence[Tuple[Table, Table]]] = ...) -> List[Table]: ...
141+
142+
143+
_SortResultType = List[Tuple[Optional[Table], Union[Set[ForeignKeyConstraint], List[ForeignKeyConstraint]]]]
144+
141145
def sort_tables_and_constraints(tables: Sequence[Table],
142146
filter_fn: Optional[Callable[[ForeignKeyConstraint], Optional[bool]]] = ...,
143-
extra_dependencies: Optional[Sequence[Tuple[Table, Table]]] = ...): ...
147+
extra_dependencies: Optional[Sequence[Tuple[Table, Table]]] = ...) -> _SortResultType: ...

sqlalchemy-stubs/sql/dml.pyi

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,65 @@
1-
from typing import Any, Optional
1+
from typing import Any, Optional, Union, TypeVar, Iterable, Tuple
22
from .base import Executable as Executable, DialectKWArgs as DialectKWArgs
3-
from .elements import ClauseElement as ClauseElement
4-
from .selectable import HasPrefixes as HasPrefixes, HasCTE as HasCTE
3+
from .elements import ClauseElement as ClauseElement, ColumnElement, TextClause
4+
from .selectable import HasPrefixes as HasPrefixes, HasCTE as HasCTE, TableClause, Selectable
5+
from .schema import Table, Column
6+
from .visitors import Visitable
7+
8+
_UB = TypeVar('_UB', bound=UpdateBase)
59

610
class UpdateBase(HasCTE, DialectKWArgs, HasPrefixes, Executable, ClauseElement):
711
__visit_name__: str = ...
812
named_with_column: bool = ...
9-
def params(self, *arg, **kw): ...
13+
def params(self, *arg: Any, **kw: Any) -> Any: ...
1014
bind: Any = ...
11-
def returning(self, *cols): ...
12-
def with_hint(self, text, selectable: Optional[Any] = ..., dialect_name: str = ...): ...
15+
def returning(self: _UB, *cols: ColumnElement[Any]) -> _UB: ...
16+
def with_hint(self: _UB, text: str, selectable: Optional[Table] = ..., dialect_name: str = ...) -> _UB: ...
17+
18+
_VB = TypeVar('_VB', bound=ValuesBase)
1319

1420
class ValuesBase(UpdateBase):
1521
__visit_name__: str = ...
1622
select: Any = ...
17-
table: Any = ...
18-
def __init__(self, table, values, prefixes) -> None: ...
23+
table: Union[TextClause, Selectable] = ...
24+
def __init__(self, table: Union[str, Selectable], values: Any, prefixes: Any) -> None: ...
1925
parameters: Any = ...
20-
def values(self, *args, **kwargs): ...
21-
def return_defaults(self, *cols): ...
26+
def values(self: _VB, *args: Any, **kwargs: Any) -> _VB: ...
27+
def return_defaults(self: _VB, *cols: Any) -> _VB: ...
28+
29+
_I = TypeVar('_I', bound=Insert)
2230

2331
class Insert(ValuesBase):
2432
__visit_name__: str = ...
25-
select: Any = ...
33+
select: Optional[Selectable] = ...
2634
include_insert_from_select_defaults: bool = ...
27-
inline: Any = ...
28-
def __init__(self, table, values: Optional[Any] = ..., inline: bool = ..., bind: Optional[Any] = ...,
35+
inline: bool = ...
36+
select_names: Optional[Iterable[Union[str, Column[Any]]]] = ...
37+
def __init__(self, table: Union[str, Selectable], values: Optional[Any] = ..., inline: bool = ..., bind: Optional[Any] = ...,
2938
prefixes: Optional[Any] = ..., returning: Optional[Any] = ..., return_defaults: bool = ...,
30-
**dialect_kw) -> None: ...
31-
def get_children(self, **kwargs): ...
32-
select_names: Any = ...
33-
def from_select(self, names, select, include_defaults: bool = ...): ...
39+
**dialect_kw: Any) -> None: ...
40+
def get_children(self, **kwargs: Any) -> Union[Tuple, Tuple[Selectable]]: ...
41+
def from_select(self: _I, names: Iterable[Union[str, Column[Any]]], select: Union[str, Selectable],
42+
include_defaults: bool = ...) -> _I: ...
43+
44+
_U = TypeVar('_U', bound=Update)
3445

3546
class Update(ValuesBase):
3647
__visit_name__: str = ...
37-
inline: Any = ...
38-
def __init__(self, table, whereclause: Optional[Any] = ..., values: Optional[Any] = ...,
39-
inline: bool = ..., bind: Optional[Any] = ..., prefixes: Optional[Any] = ...,
48+
inline: bool = ...
49+
def __init__(self, table: Union[str, Selectable], whereclause: Optional[Union[str, bool, Visitable]] = ...,
50+
values: Optional[Any] = ..., inline: bool = ..., bind: Optional[Any] = ..., prefixes: Optional[Any] = ...,
4051
returning: Optional[Any] = ..., return_defaults: bool = ..., preserve_parameter_order: bool = ...,
41-
**dialect_kw) -> None: ...
42-
def get_children(self, **kwargs): ...
43-
def where(self, whereclause): ...
52+
**dialect_kw: Any) -> None: ...
53+
def get_children(self, **kwargs: Any) -> Union[Tuple, Tuple[Visitable]]: ...
54+
def where(self: _U, whereclause: Union[str, bool, Visitable]) -> _U: ...
55+
56+
_D = TypeVar('_D', bound=Delete)
4457

4558
class Delete(UpdateBase):
4659
__visit_name__: str = ...
47-
table: Any = ...
48-
def __init__(self, table, whereclause: Optional[Any] = ..., bind: Optional[Any] = ...,
49-
returning: Optional[Any] = ..., prefixes: Optional[Any] = ..., **dialect_kw) -> None: ...
50-
def get_children(self, **kwargs): ...
51-
def where(self, whereclause): ...
60+
table: Union[TextClause, Selectable] = ...
61+
def __init__(self, table: Union[str, Selectable], whereclause: Optional[Union[str, bool, Visitable]] = ...,
62+
bind: Optional[Any] = ..., returning: Optional[Any] = ..., prefixes: Optional[Any] = ...,
63+
**dialect_kw: Any) -> None: ...
64+
def get_children(self, **kwargs: Any) -> Union[Tuple, Tuple[Visitable]]: ...
65+
def where(self: _D, whereclause: Union[str, bool, Visitable]) -> _D: ...

sqlalchemy-stubs/sql/elements.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ class conv(_truncated_label): ...
501501

502502
class _anonymous_label(_truncated_label): ...
503503

504-
class AnnotatedColumnElement(Annotated, Generic[_T]):
504+
class AnnotatedColumnElement(Annotated[ColumnElement[_T]], Generic[_T]):
505505
def __init__(self, element: ColumnElement[_T], values: Any) -> None: ...
506506
@property
507507
def name(self) -> str: ...

0 commit comments

Comments
 (0)