Skip to content

Commit 8acfd11

Browse files
authored
Improvements to type to support alembic (#131)
* Improvements to type to support alembic * Move improvements. added class var type - instance instance descriptor thingy * more updates. * final updates
1 parent 609787e commit 8acfd11

File tree

13 files changed

+74
-21
lines changed

13 files changed

+74
-21
lines changed

sqlalchemy-stubs/_typing.pyi

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import Any
2+
from typing import Generic
3+
from typing import overload
4+
from typing import Type
5+
from typing import TypeVar
6+
7+
_T = TypeVar("_T")
8+
9+
class _TypeToInstance(Generic[_T]):
10+
@overload
11+
def __get__(self, instance: None, owner: Any) -> Type[_T]: ...
12+
@overload
13+
def __get__(self, instance: object, owner: Any) -> _T: ...
14+
@overload
15+
def __set__(self, instance: None, value: Type[_T]) -> None: ...
16+
@overload
17+
def __set__(self, instance: object, value: _T) -> None: ...

sqlalchemy-stubs/engine/base.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Connection(Connectable):
6969
def connect(self: _TConnection, close_with_result: bool = ...) -> _TConnection: ... # type: ignore[override]
7070
def invalidate(self, exception: Optional[Any] = ...) -> None: ...
7171
def detach(self) -> None: ...
72-
def begin(self) -> Optional[Transaction]: ...
72+
def begin(self) -> Transaction: ...
7373
def begin_nested(self) -> NestedTransaction: ...
7474
def begin_twophase(
7575
self, xid: Optional[str] = ...

sqlalchemy-stubs/engine/default.pyi

Lines changed: 2 additions & 7 deletions
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 _TypeToInstance
1920
from ..sql import compiler
2021
from ..sql.type_api import TypeEngine
2122
from ..util import langhelpers
@@ -30,14 +31,9 @@ CACHING_DISABLED: langhelpers._symbol
3031
NO_CACHE_KEY: langhelpers._symbol
3132

3233
class DefaultDialect(interfaces.Dialect):
33-
statement_compiler: Type[compiler.SQLCompiler] = ...
34-
ddl_compiler: Type[compiler.DDLCompiler] = ...
35-
type_compiler: Type[compiler.GenericTypeCompiler] = ...
36-
preparer: Type[compiler.IdentifierPreparer] = ...
34+
type_compiler: _TypeToInstance[compiler.GenericTypeCompiler] = ...
3735
execution_ctx_cls: Type[DefaultExecutionContext] = ...
3836
supports_alter: bool = ...
39-
supports_comments: bool = ...
40-
inline_comments: bool = ...
4137
use_setinputsizes: bool = ...
4238
default_sequence_base: int = ...
4339
execute_sequence_format: Union[
@@ -96,7 +92,6 @@ class DefaultDialect(interfaces.Dialect):
9692
positional: bool = ...
9793
dbapi: Any = ...
9894
paramstyle: str = ...
99-
identifier_preparer: compiler.IdentifierPreparer = ...
10095
case_sensitive: bool = ...
10196
label_length: Optional[int] = ...
10297
compiler_linting: int = ...

sqlalchemy-stubs/engine/interfaces.pyi

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ from .base import Engine
1616
from .cursor import CursorFetchStrategy
1717
from .cursor import CursorResult
1818
from .url import URL
19+
from .._typing import _TypeToInstance
1920
from ..exc import StatementError
21+
from ..sql import compiler
2022
from ..sql.compiler import Compiled as Compiled
2123
from ..sql.compiler import IdentifierPreparer
2224
from ..sql.compiler import TypeCompiler as TypeCompiler
@@ -92,15 +94,18 @@ class Dialect:
9294
positional: bool = ...
9395
paramstyle: str = ...
9496
encoding: str = ...
95-
statement_compiler: Type[Compiled] = ...
96-
ddl_compiler: Type[Compiled] = ...
97+
statement_compiler: Type[compiler.SQLCompiler] = ...
98+
ddl_compiler: Type[compiler.DDLCompiler] = ...
99+
type_compiler: _TypeToInstance[compiler.TypeCompiler] = ...
100+
preparer: Type[compiler.IdentifierPreparer] = ...
101+
identifier_preparer: compiler.IdentifierPreparer = ...
97102
server_version_info: Optional[Tuple[Any, ...]] = ...
98103
default_schema_name: Optional[str] = ...
99104
execution_ctx_cls: Type[ExecutionContext] = ...
100105
execute_sequence_format: Union[
101106
Type[Tuple[Any, ...]], Type[List[Any]]
102107
] = ...
103-
preparer: Type[IdentifierPreparer] = ...
108+
preparer: IdentifierPreparer = ...
104109
supports_alter: bool = ...
105110
max_identifier_length: int = ...
106111
supports_sane_rowcount: bool = ...
@@ -114,6 +119,9 @@ class Dialect:
114119
supports_native_boolean: bool = ...
115120
dbapi_exception_translation_map: Dict[str, str] = ...
116121
supports_statement_cache: bool = ...
122+
supports_comments: bool = ...
123+
inline_comments: bool = ...
124+
def __init__(self, *args: Any, **kw: Any) -> None: ...
117125
def create_connect_args(
118126
self, url: URL
119127
) -> Tuple[Sequence[Any], Mapping[str, Any]]: ...

sqlalchemy-stubs/engine/reflection.pyi

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@ from typing import Set
77
from typing import Tuple
88
from typing import Type
99
from typing import TypeVar
10+
from typing import Union
1011

1112
from .base import Connectable
13+
from .base import Connection
14+
from .base import Engine
15+
from .interfaces import Dialect
1216
from ..sql.schema import Table
1317

1418
_TInspector = TypeVar("_TInspector", bound=Inspector)
1519

1620
def cache(fn: Any, self: Any, con: Any, *args: Any, **kw: Any) -> Any: ...
1721

1822
class Inspector:
23+
bind: Union[Engine, Connection] = ...
24+
engine: Engine = ...
25+
dialect: Dialect = ...
1926
def __init__(self, bind: Connectable): ...
2027
@classmethod
2128
def from_engine(
@@ -68,15 +75,15 @@ class Inspector:
6875
def reflecttable(
6976
self,
7077
table: Table,
71-
include_columns: Collection[str],
78+
include_columns: Optional[Collection[str]],
7279
exclude_columns: Collection[str] = ...,
7380
resolve_fks: bool = ...,
7481
_extend_on: Set[Table] = ...,
7582
) -> None: ...
7683
def reflect_table(
7784
self,
7885
table: Table,
79-
include_columns: Collection[str],
86+
include_columns: Optional[Collection[str]],
8087
exclude_columns: Collection[str] = ...,
8188
resolve_fks: bool = ...,
8289
_extend_on: Set[Table] = ...,

sqlalchemy-stubs/orm/__init__.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ from typing import Mapping
33
from typing import Optional
44
from typing import Tuple
55

6-
from . import exc
7-
from . import mapper as mapperlib
6+
from . import exc as exc
7+
from . import mapper as mapperlib # noqa
88
from . import strategy_options
99
from .attributes import AttributeEvent as AttributeEvent
1010
from .attributes import InstrumentedAttribute as InstrumentedAttribute

sqlalchemy-stubs/schema.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ from .sql.ddl import SetTableComment as SetTableComment
2121
from .sql.ddl import sort_tables as sort_tables
2222
from .sql.ddl import sort_tables_and_constraints as sort_tables_and_constraints
2323
from .sql.naming import conv as conv
24+
from .sql.schema import _get_table_key as _get_table_key
2425
from .sql.schema import BLANK_SCHEMA as BLANK_SCHEMA
2526
from .sql.schema import CheckConstraint as CheckConstraint
2627
from .sql.schema import Column as Column

sqlalchemy-stubs/sql/compiler.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Compiled:
9494
self, element: Any, err: Any
9595
) -> _VisitResult: ...
9696
@property
97-
def sql_compiler(self) -> Compiled: ...
97+
def sql_compiler(self) -> SQLCompiler: ...
9898
def process(self, obj: Any, **kwargs: Any) -> _VisitResult: ...
9999
def construct_params(
100100
self,

sqlalchemy-stubs/sql/elements.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,9 @@ class AnnotatedColumnElement(Annotated):
714714
def anon_label(self) -> Any: ...
715715

716716
class _truncated_label(quoted_name):
717+
def __new__(
718+
cls: Type[_QN], value: Any, quote: Optional[Any] = ...
719+
) -> _QN: ...
717720
def __reduce__(self) -> Any: ...
718721
def apply_map(self: _TL, map_: Any) -> _TL: ...
719722

sqlalchemy-stubs/sql/schema.pyi

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ from typing import List
88
from typing import Mapping
99
from typing import Optional
1010
from typing import overload
11+
from typing import Sequence
1112
from typing import Set
1213
from typing import Type
1314
from typing import TypeVar
@@ -22,11 +23,13 @@ from . import type_api
2223
from . import visitors
2324
from .base import DedupeColumnCollection
2425
from .base import DialectKWArgs
26+
from .base import ImmutableColumnCollection
2527
from .base import SchemaEventTarget
2628
from .elements import ClauseElement
2729
from .elements import ColumnClause
2830
from .elements import ColumnElement
2931
from .elements import TextClause
32+
from .events import DDLEvents
3033
from .selectable import TableClause
3134
from .. import util
3235
from ..engine import Connection
@@ -54,6 +57,7 @@ _ID = TypeVar("_ID", bound=Identity)
5457
class SchemaItem(SchemaEventTarget, visitors.Visitable):
5558
__visit_name__: str = ...
5659
create_drop_stringify_dialect: str = ...
60+
dispatch: DDLEvents
5761
@util.memoized_property
5862
def info(self) -> Dict[Any, Any]: ...
5963

@@ -67,7 +71,8 @@ class Table(DialectKWArgs, SchemaItem, TableClause):
6771
primary_key: PrimaryKeyConstraint = ...
6872
fullname: str = ...
6973
implicit_returning: bool = ...
70-
comment: Optional[Any] = ...
74+
comment: Optional[str] = ...
75+
_prefixes: Sequence[str] = ...
7176
def __new__(cls: Type[_TAB], *args: Any, **kw: Any) -> _TAB: ...
7277
def __init__(self, *args: Any, **kw: Any) -> None: ...
7378
@property
@@ -118,9 +123,18 @@ class Table(DialectKWArgs, SchemaItem, TableClause):
118123
] = ...,
119124
name: Optional[util.text_type] = ...,
120125
) -> Table: ...
126+
@util.memoized_property
127+
def columns(self) -> ImmutableColumnCollection[Column[Any]]: ...
128+
@util.memoized_property
129+
def c(self) -> ImmutableColumnCollection[Column[Any]]: ...
130+
@property
131+
def _autoincrement_column(self) -> Optional[Column[Any]]: ...
132+
@_autoincrement_column.setter
133+
def _autoincrement_column(self, value: Optional[Column[Any]]) -> None: ...
121134

122135
class Column(DialectKWArgs, SchemaItem, ColumnClause[_TE]):
123136
__visit_name__: str = ...
137+
name: str = ...
124138
inherit_cache: bool = ...
125139
key: Optional[str] = ...
126140
primary_key: bool = ...
@@ -438,6 +452,7 @@ class Constraint(DialectKWArgs, SchemaItem):
438452
name: Optional[str] = ...
439453
deferrable: Optional[bool] = ...
440454
initially: Optional[str] = ...
455+
parent: Optional[Table] = ...
441456
def __init__(
442457
self,
443458
name: Optional[str] = ...,
@@ -502,7 +517,7 @@ class ForeignKeyConstraint(ColumnCollectionConstraint):
502517
elements: List[ForeignKey] = ...
503518
def __init__(
504519
self,
505-
columns: Iterable[str],
520+
columns: Iterable[Union[str, Column[Any]]],
506521
refcolumns: Iterable[Union[str, Column[Any]]],
507522
name: Optional[str] = ...,
508523
onupdate: Optional[str] = ...,
@@ -651,3 +666,5 @@ class Identity(IdentityOptions, FetchedValue, SchemaItem):
651666
order: Optional[bool] = ...,
652667
) -> None: ...
653668
def copy(self: _ID, **kw: Any) -> _ID: ...
669+
670+
def _get_table_key(name: str, schema: Optional[str]) -> str: ...

0 commit comments

Comments
 (0)