Skip to content

Commit f92fd9c

Browse files
authored
Improve sql.ddl (#31)
* Improve `sql.ddl` * Add dialect typing * Addressed PR feedback
1 parent 18c563f commit f92fd9c

File tree

1 file changed

+98
-53
lines changed

1 file changed

+98
-53
lines changed

sqlalchemy-stubs/sql/ddl.pyi

Lines changed: 98 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,103 @@
11
from typing import Any
2+
from typing import Callable
3+
from typing import Dict
4+
from typing import Iterable
5+
from typing import List
26
from typing import Optional
7+
from typing import Sequence
8+
from typing import Set
9+
from typing import Tuple
10+
from typing import TypeVar
11+
from typing import Union
312

4-
from . import roles as roles
5-
from .base import Executable as Executable
6-
from .base import SchemaVisitor as SchemaVisitor
7-
from .elements import ClauseElement as ClauseElement
8-
from .. import exc as exc
9-
from .. import util as util
10-
from ..util import topological as topological
13+
from typing_extensions import Protocol
14+
15+
from . import roles
16+
from .base import Executable
17+
from .base import SchemaVisitor
18+
from .elements import ClauseElement
19+
from .schema import ForeignKey
20+
from .schema import ForeignKeyConstraint
21+
from .schema import Index
22+
from .schema import SchemaItem
23+
from .schema import Table
24+
from ..engine import Connection
25+
from ..engine import Dialect
26+
from ..engine import Engine
27+
28+
_DDLE = TypeVar("_DDLE", bound=DDLElement)
29+
30+
_Bind = Union[Engine, Connection]
31+
32+
class _DDLElementCallback(Protocol):
33+
def __call__(
34+
self,
35+
__ddl: DDLElement,
36+
__target: Optional[SchemaItem],
37+
__bind: _Bind,
38+
*,
39+
state: Optional[Any],
40+
**kw: Any,
41+
) -> bool: ...
1142

1243
class _DDLCompiles(ClauseElement): ...
1344

1445
class DDLElement(roles.DDLRole, Executable, _DDLCompiles):
15-
target: Any = ...
46+
target: Optional[SchemaItem] = ...
1647
on: Any = ...
17-
dialect: Any = ...
18-
callable_: Any = ...
19-
def execute(
20-
self, bind: Optional[Any] = ..., target: Optional[Any] = ...
21-
): ...
22-
def against(self, target: Any) -> None: ...
23-
state: Any = ...
24-
def execute_if(
48+
dialect: Union[str, Tuple[str, ...], List[str], Set[str], None] = ...
49+
callable_: Optional[_DDLElementCallback] = ...
50+
state: Optional[Any] = ...
51+
bind: Optional[_Bind] = ...
52+
def execute( # type: ignore[override]
2553
self,
26-
dialect: Optional[Any] = ...,
27-
callable_: Optional[Any] = ...,
54+
bind: Optional[_Bind] = ...,
55+
target: Optional[SchemaItem] = ...,
56+
) -> Any: ...
57+
def against(self: _DDLE, target: Optional[SchemaItem]) -> _DDLE: ...
58+
def execute_if(
59+
self: _DDLE,
60+
dialect: Optional[
61+
Union[str, Tuple[str, ...], List[str], Set[str]]
62+
] = ...,
63+
callable_: Optional[_DDLElementCallback] = ...,
2864
state: Optional[Any] = ...,
29-
) -> None: ...
30-
def __call__(self, target: Any, bind: Any, **kw: Any): ...
31-
def bind(self): ...
32-
bind: Any = ...
65+
) -> _DDLE: ...
66+
def __call__(
67+
self, target: SchemaItem, bind: _Bind, **kw: Any
68+
) -> Optional[Any]: ...
3369

3470
class DDL(DDLElement):
3571
__visit_name__: str = ...
36-
statement: Any = ...
72+
statement: str = ...
3773
context: Any = ...
3874
def __init__(
3975
self,
40-
statement: Any,
41-
context: Optional[Any] = ...,
42-
bind: Optional[Any] = ...,
76+
statement: str,
77+
context: Optional[Dict[str, Any]] = ...,
78+
bind: Optional[_Bind] = ...,
4379
) -> None: ...
4480

4581
class _CreateDropBase(DDLElement):
4682
element: Any = ...
47-
bind: Any = ...
48-
if_exists: Any = ...
49-
if_not_exists: Any = ...
83+
if_exists: bool = ...
84+
if_not_exists: bool = ...
5085
def __init__(
5186
self,
5287
element: Any,
53-
bind: Optional[Any] = ...,
88+
bind: Optional[_Bind] = ...,
5489
if_exists: bool = ...,
5590
if_not_exists: bool = ...,
5691
_legacy_bind: Optional[Any] = ...,
5792
) -> None: ...
5893
@property
59-
def stringify_dialect(self): ...
94+
def stringify_dialect(self) -> str: ... # type: ignore[override]
6095

6196
class CreateSchema(_CreateDropBase):
6297
__visit_name__: str = ...
6398
quote: Any = ...
6499
def __init__(
65-
self, name: Any, quote: Optional[Any] = ..., **kw: Any
100+
self, name: str, quote: Optional[Any] = ..., **kw: Any
66101
) -> None: ...
67102

68103
class DropSchema(_CreateDropBase):
@@ -71,7 +106,7 @@ class DropSchema(_CreateDropBase):
71106
cascade: Any = ...
72107
def __init__(
73108
self,
74-
name: Any,
109+
name: str,
75110
quote: Optional[Any] = ...,
76111
cascade: bool = ...,
77112
**kw: Any,
@@ -83,9 +118,11 @@ class CreateTable(_CreateDropBase):
83118
include_foreign_key_constraints: Any = ...
84119
def __init__(
85120
self,
86-
element: Any,
87-
bind: Optional[Any] = ...,
88-
include_foreign_key_constraints: Optional[Any] = ...,
121+
element: Table,
122+
bind: Optional[_Bind] = ...,
123+
include_foreign_key_constraints: Optional[
124+
Sequence[ForeignKeyConstraint]
125+
] = ...,
89126
if_not_exists: bool = ...,
90127
) -> None: ...
91128

@@ -100,7 +137,10 @@ class CreateColumn(_DDLCompiles):
100137
class DropTable(_CreateDropBase):
101138
__visit_name__: str = ...
102139
def __init__(
103-
self, element: Any, bind: Optional[Any] = ..., if_exists: bool = ...
140+
self,
141+
element: Table,
142+
bind: Optional[_Bind] = ...,
143+
if_exists: bool = ...,
104144
) -> None: ...
105145

106146
class CreateSequence(_CreateDropBase):
@@ -113,15 +153,18 @@ class CreateIndex(_CreateDropBase):
113153
__visit_name__: str = ...
114154
def __init__(
115155
self,
116-
element: Any,
117-
bind: Optional[Any] = ...,
156+
element: Index,
157+
bind: Optional[_Bind] = ...,
118158
if_not_exists: bool = ...,
119159
) -> None: ...
120160

121161
class DropIndex(_CreateDropBase):
122162
__visit_name__: str = ...
123163
def __init__(
124-
self, element: Any, bind: Optional[Any] = ..., if_exists: bool = ...
164+
self,
165+
element: Index,
166+
bind: Optional[_Bind] = ...,
167+
if_exists: bool = ...,
125168
) -> None: ...
126169

127170
class AddConstraint(_CreateDropBase):
@@ -155,11 +198,11 @@ class SchemaGenerator(DDLBase):
155198
checkfirst: Any = ...
156199
tables: Any = ...
157200
preparer: Any = ...
158-
dialect: Any = ...
201+
dialect: Dialect = ...
159202
memo: Any = ...
160203
def __init__(
161204
self,
162-
dialect: Any,
205+
dialect: Dialect,
163206
connection: Any,
164207
checkfirst: bool = ...,
165208
tables: Optional[Any] = ...,
@@ -181,17 +224,17 @@ class SchemaDropper(DDLBase):
181224
checkfirst: Any = ...
182225
tables: Any = ...
183226
preparer: Any = ...
184-
dialect: Any = ...
227+
dialect: Dialect = ...
185228
memo: Any = ...
186229
def __init__(
187230
self,
188-
dialect: Any,
231+
dialect: Dialect,
189232
connection: Any,
190233
checkfirst: bool = ...,
191234
tables: Optional[Any] = ...,
192235
**kwargs: Any,
193236
) -> None: ...
194-
def visit_metadata(self, metadata: Any): ...
237+
def visit_metadata(self, metadata: Any) -> None: ...
195238
def visit_index(self, index: Any, drop_ok: bool = ...) -> None: ...
196239
def visit_table(
197240
self,
@@ -203,13 +246,15 @@ class SchemaDropper(DDLBase):
203246
def visit_sequence(self, sequence: Any, drop_ok: bool = ...) -> None: ...
204247

205248
def sort_tables(
206-
tables: Any,
207-
skip_fn: Optional[Any] = ...,
208-
extra_dependencies: Optional[Any] = ...,
209-
): ...
249+
tables: Iterable[Table],
250+
skip_fn: Optional[Callable[[ForeignKey], bool]] = ...,
251+
extra_dependencies: Optional[Iterable[Tuple[Table, Table]]] = ...,
252+
) -> List[Table]: ...
210253
def sort_tables_and_constraints(
211-
tables: Any,
212-
filter_fn: Optional[Any] = ...,
213-
extra_dependencies: Optional[Any] = ...,
254+
tables: Iterable[Table],
255+
filter_fn: Optional[
256+
Callable[[ForeignKeyConstraint], Optional[bool]]
257+
] = ...,
258+
extra_dependencies: Optional[Iterable[Tuple[Table, Table]]] = ...,
214259
_warn_for_cycles: bool = ...,
215-
): ...
260+
) -> List[Tuple[Optional[Table], List[ForeignKeyConstraint]]]: ...

0 commit comments

Comments
 (0)