Skip to content

Commit 6035190

Browse files
committed
Improve declarative API annotations
Some legacy constructs are not typed.
1 parent a0e1ae4 commit 6035190

File tree

1 file changed

+43
-28
lines changed

1 file changed

+43
-28
lines changed

sqlalchemy-stubs/orm/decl_api.pyi

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from typing import Any
2+
from typing import Callable
3+
from typing import FrozenSet
24
from typing import Generic
5+
from typing import MutableMapping
36
from typing import Optional
7+
from typing import Tuple
8+
from typing import Type
49
from typing import TypeVar
510
from typing import Union
611

@@ -12,11 +17,15 @@ from .mapper import Mapper as Mapper
1217
from .. import inspection as inspection
1318
from .. import util as util
1419
from ..sql import ColumnElement
20+
from ..sql import FromClause
1521
from ..sql.schema import MetaData as MetaData
22+
from ..sql.schema import Table
1623
from ..util import hybridmethod as hybridmethod
1724
from ..util import hybridproperty as hybridproperty
1825

19-
def has_inherited_table(cls): ...
26+
_T = TypeVar("_T")
27+
28+
def has_inherited_table(cls: type) -> bool: ...
2029

2130
class DeclarativeMeta(type):
2231
def __init__(
@@ -25,13 +34,10 @@ class DeclarativeMeta(type):
2534
def __setattr__(cls, key: Any, value: Any) -> None: ...
2635
def __delattr__(cls, key: Any) -> None: ...
2736
metadata: MetaData
28-
registry: "registry"
37+
registry: _registry # Avoid circural reference
2938

3039
def synonym_for(name: Any, map_column: bool = ...): ...
3140

32-
_T = TypeVar("_T")
33-
_Generic_T = Generic[_T]
34-
3541
class declared_attr(interfaces._MappedAttribute, property, Generic[_T]):
3642
__doc__: Any = ...
3743
def __init__(self, fget: Any, cascading: bool = ...) -> None: ...
@@ -46,42 +52,51 @@ class _stateful_declared_attr(declared_attr):
4652
def __call__(self, fn: Any): ...
4753

4854
def declarative_base(
49-
bind: Optional[Any] = ...,
50-
metadata: Optional[Any] = ...,
51-
mapper: Optional[Any] = ...,
52-
cls: Any = ...,
55+
bind: Optional[
56+
Any
57+
] = ..., # NOTE: Deprecated in 1.4, to be removed in 2.0.
58+
metadata: Optional[MetaData] = ...,
59+
mapper: Optional[Mapper] = ...,
60+
cls: Union[type, Tuple[type, ...]] = ...,
5361
name: str = ...,
54-
constructor: Any = ...,
55-
class_registry: Optional[Any] = ...,
56-
metaclass: Any = ...,
57-
): ...
62+
constructor: Callable[..., None] = ...,
63+
class_registry: Optional[MutableMapping[Any, Any]] = ...,
64+
metaclass: type = ...,
65+
) -> type: ...
5866

5967
class registry:
60-
metadata: Any = ...
61-
constructor: Any = ...
68+
metadata: MetaData
69+
constructor: Callable[..., None]
6270
def __init__(
6371
self,
64-
metadata: Optional[Any] = ...,
65-
class_registry: Optional[Any] = ...,
66-
constructor: Any = ...,
72+
metadata: Optional[MetaData] = ...,
73+
class_registry: Optional[MutableMapping[Any, Any]] = ...,
74+
constructor: Callable[..., None] = ...,
6775
_bind: Optional[Any] = ...,
6876
) -> None: ...
6977
@property
70-
def mappers(self): ...
78+
def mappers(self) -> FrozenSet[Mapper]: ...
7179
def configure(self, cascade: bool = ...) -> None: ...
7280
def dispose(self, cascade: bool = ...) -> None: ...
7381
def generate_base(
7482
self,
75-
mapper: Optional[Any] = ...,
76-
cls: Any = ...,
83+
mapper: Optional[Mapper] = ...,
84+
cls: Union[type, Tuple[type, ...]] = ...,
7785
name: str = ...,
78-
metaclass: Any = ...,
79-
): ...
80-
def mapped(self, cls: Any): ...
81-
def as_declarative_base(self, **kw: Any): ...
86+
metaclass: type = ...,
87+
) -> type: ...
88+
def mapped(self, cls: Type[_T]) -> Type[_T]: ...
89+
def as_declarative_base(
90+
self, **kw: Any
91+
) -> Callable[[Type[_T]], Type[_T]]: ...
8292
def map_declaratively(self, cls: type) -> Mapper: ...
8393
def map_imperatively(
84-
self, class_: Any, local_table: Optional[Any] = ..., **kw: Any
85-
): ...
94+
self,
95+
class_: type,
96+
local_table: Optional[Union[FromClause, Table]] = ...,
97+
**kw: Any,
98+
) -> Mapper: ...
99+
100+
_registry = registry
86101

87-
def as_declarative(**kw: Any): ...
102+
def as_declarative(**kw: Any) -> Callable[[Type[_T]], Type[_T]]: ...

0 commit comments

Comments
 (0)