Skip to content

Commit 1577a20

Browse files
bryanforbesilevkivskyi
authored andcommitted
Update typings to be more precise (#42)
1 parent b6647c0 commit 1577a20

File tree

3 files changed

+265
-203
lines changed

3 files changed

+265
-203
lines changed

sqlalchemy-stubs/sql/elements.pyi

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ from .type_api import TypeEngine
1313
from .sqltypes import NullType, Boolean, Integer
1414
from .selectable import TextAsFrom, TableClause
1515
from .functions import FunctionElement
16+
from .schema import ForeignKey
1617

1718
_T = TypeVar('_T')
1819
_T_contra = TypeVar('_T_contra', contravariant=True)
@@ -44,27 +45,32 @@ class ClauseElement(Visitable):
4445
def __bool__(self): ...
4546
__nonzero__: Any = ...
4647

48+
_CE = TypeVar('_CE', bound=ColumnElement)
49+
4750
class ColumnElement(operators.ColumnOperators, ClauseElement, Generic[_T]):
4851
__visit_name__: str = ...
49-
primary_key: Any = ...
50-
foreign_keys: List[Any] = ...
51-
key: Any = ...
52-
def self_group(self, against: Optional[Any] = ...): ...
52+
primary_key: bool = ...
53+
foreign_keys: List[ForeignKey] = ...
54+
key: Optional[str] = ...
55+
def self_group(self: _CE, against: Optional[Any] = ...) -> Union[AsBoolean, Grouping[_T], _CE]: ...
5356
@property
5457
def type(self) -> TypeEngine[_T]: ...
5558
def comparator(self): ...
56-
def __getattr__(self, key): ...
59+
def __getattr__(self, key: str) -> Any: ...
5760
def operate(self, op, *other, **kwargs): ...
5861
def reverse_operate(self, op, other, **kwargs): ...
5962
@property
60-
def expression(self): ...
61-
def base_columns(self): ...
62-
def proxy_set(self): ...
63-
def shares_lineage(self, othercolumn): ...
64-
def compare(self, other: Any, **kw: Any) -> bool: ...
65-
def cast(self, type_): ...
66-
def label(self, name): ...
67-
def anon_label(self): ...
63+
def expression(self: _CE) -> _CE: ...
64+
def base_columns(self) -> Set[ColumnElement[Any]]: ...
65+
def proxy_set(self) -> Set[ColumnElement[Any]]: ...
66+
def shares_lineage(self, othercolumn: ColumnElement[Any]) -> bool: ...
67+
def compare(self, other: ColumnElement[Any], use_proxies: bool = ..., equivalents: bool = ..., **kw: Any) -> bool: ...
68+
@overload
69+
def cast(self, type_: Type[TypeEngine[_U]]) -> Cast[_U]: ...
70+
@overload
71+
def cast(self, type_: TypeEngine[_U]) -> Cast[_U]: ...
72+
def label(self, name: str) -> Label[_T]: ...
73+
def anon_label(self) -> _anonymous_label: ...
6874

6975
class BindParameter(ColumnElement[_T]):
7076
__visit_name__: str = ...
@@ -87,7 +93,8 @@ class BindParameter(ColumnElement[_T]):
8793
_compared_to_type: Optional[Any] = ...) -> None: ...
8894
@property
8995
def effective_value(self) -> _T: ...
90-
def compare(self, other: Any, **kw: Any) -> bool: ...
96+
# Signature of "compare" incompatible with supertype "ColumnElement"
97+
def compare(self, other: ColumnElement[Any], **kw: Any) -> bool: ... # type: ignore
9198

9299
class TypeClause(ClauseElement, Generic[_T]):
93100
__visit_name__: str = ...
@@ -168,19 +175,21 @@ class ClauseList(ClauseElement):
168175

169176
_BCL = TypeVar('_BCL', bound=BooleanClauseList)
170177

171-
class BooleanClauseList(ClauseList, ColumnElement[bool]):
178+
# Definitions of "compare" and "self_group" in ClauseList are incompatible with ColumnElement
179+
class BooleanClauseList(ClauseList, ColumnElement[bool]): # type: ignore
172180
__visit_name__: str = ...
173181
# Note: passing strings to below generates a warning, but still works.
174182
@classmethod
175183
def and_(cls, *clauses: Union[ClauseElement, str, bool]) -> BooleanClauseList: ...
176184
@classmethod
177185
def or_(cls, *clauses: Union[ClauseElement, str, bool]) -> BooleanClauseList: ...
178-
def self_group(self: _BCL, against: Optional[Any] = ...) -> Union[_BCL, Grouping[Any]]: ...
186+
def self_group(self: _BCL, against: Optional[Any] = ...) -> Union[_BCL, Grouping[bool]]: ...
179187

180188
and_ = BooleanClauseList.and_
181189
or_ = BooleanClauseList.or_
182190

183-
class Tuple(ClauseList, ColumnElement[_T]):
191+
# Definitions of "compare" and "self_group" in ClauseList are incompatible with ColumnElement
192+
class Tuple(ClauseList, ColumnElement[_T]): # type: ignore
184193
type: TypeEngine[_T] = ...
185194
@overload
186195
def __init__(self, *clauses: ColumnElement[Any], type_: Type[TypeEngine[_T]], **kw: Any) -> None: ...
@@ -261,7 +270,8 @@ class UnaryExpression(ColumnElement[_T]):
261270
type_: Optional[TypeEngine[_T]] = ..., negate: Optional[Any] = ...,
262271
wraps_column_expression: bool = ...) -> None: ...
263272
def get_children(self, **kwargs) -> _TupleType[Any]: ...
264-
def compare(self, other: Any, **kw: Any) -> bool: ...
273+
# Signature of "compare" incompatible with supertype "ColumnElement"
274+
def compare(self, other: ColumnElement[Any], **kw: Any) -> bool: ... # type: ignore
265275
def self_group(self: _UE, against: Optional[Any] = ...) -> Union[_UE, Grouping[_T]]: ...
266276
@classmethod
267277
def _create_nullsfirst(cls, column: ColumnElement[Any]) -> UnaryExpression[NullType]: ...
@@ -292,14 +302,14 @@ class CollectionAggregate(UnaryExpression[_T]):
292302

293303
_AB = TypeVar('_AB', bound=AsBoolean)
294304

295-
class AsBoolean(UnaryExpression):
296-
element: Any = ...
305+
class AsBoolean(UnaryExpression[bool]):
306+
element: ColumnElement[Any] = ...
297307
type: Boolean = ...
298308
operator: Callable[..., Any] = ...
299-
negate: Any = ...
309+
negate: Callable[..., Any] = ...
300310
modifier: Any = ...
301311
wraps_column_expression: bool = ...
302-
def __init__(self, element: Any, operator: Callable[..., Any], negate: Any) -> None: ...
312+
def __init__(self, element: ColumnElement[Any], operator: Callable[..., Any], negate: Callable[..., Any]) -> None: ...
303313
def self_group(self: _AB, against: Optional[Any] = ...) -> _AB: ...
304314

305315
_BE = TypeVar('_BE', bound=BinaryExpression)
@@ -324,7 +334,8 @@ class BinaryExpression(ColumnElement[_T], Generic[_T, _U, _V]):
324334
@property
325335
def is_comparison(self): ...
326336
def get_children(self, **kwargs): ...
327-
def compare(self, other: Any, **kw: Any) -> bool: ...
337+
# Signature of "compare" incompatible with supertype "ColumnElement"
338+
def compare(self, other: ColumnElement[Any], **kw: Any) -> bool: ... # type: ignore
328339
def self_group(self: _BE, against: Optional[Any] = ...) -> Union[_BE, Grouping[_T]]: ...
329340

330341
_SL = TypeVar('_SL', bound=Slice)
@@ -336,7 +347,7 @@ class Slice(ColumnElement[None]):
336347
step: Any = ...
337348
type: NullType = ...
338349
def __init__(self, start, stop, step) -> None: ...
339-
def self_group(self: _SL, against: Optional[Any] = ...) -> Union[_SL, Grouping[NullType]]: ...
350+
def self_group(self: _SL, against: Optional[Any] = ...) -> _SL: ...
340351

341352
class IndexExpression(BinaryExpression): ...
342353

@@ -354,7 +365,8 @@ class Grouping(ColumnElement[_T]):
354365
def self_group(self: _G, against: Optional[Any] = ...) -> _G: ...
355366
def get_children(self, **kwargs) -> _TupleType[ClauseElement]: ...
356367
def __getattr__(self, attr): ...
357-
def compare(self, other: Any, **kw: Any) -> bool: ...
368+
# Signature of "compare" incompatible with supertype "ColumnElement"
369+
def compare(self, other: ColumnElement[Any], **kw: Any) -> bool: ... # type: ignore
358370

359371
RANGE_UNBOUNDED: util.symbol = ...
360372
RANGE_CURRENT: util.symbol = ...
@@ -432,11 +444,13 @@ class Label(ColumnElement[_T]):
432444
def type(self) -> TypeEngine[_T]: ...
433445
@property
434446
def element(self) -> ColumnElement[_T]: ...
435-
def self_group(self, against: Optional[Any] = ...) -> Union[ColumnElement[_T], Grouping[_T]]: ...
447+
def self_group(self: _L, against: Optional[Any] = ...) -> Union[_L, Label[_T]]: ...
448+
# Signature of "primary_key" incompatible with supertype "ColumnElement"
436449
@property
437-
def primary_key(self) -> Any: ...
450+
def primary_key(self) -> bool: ... # type: ignore
451+
# Signature of "foreign_keys" incompatible with supertype "ColumnElement"
438452
@property
439-
def foreign_keys(self) -> List[Any]: ... # type: ignore
453+
def foreign_keys(self) -> List[ForeignKey]: ... # type: ignore
440454
def get_children(self, **kwargs: Any) -> Any: ...
441455

442456
class ColumnClause(Immutable, ColumnElement[_T]):
@@ -485,6 +499,8 @@ class _truncated_label(quoted_name):
485499

486500
class conv(_truncated_label): ...
487501

502+
class _anonymous_label(_truncated_label): ...
503+
488504
class AnnotatedColumnElement(Annotated, Generic[_T]):
489505
def __init__(self, element: ColumnElement[_T], values: Any) -> None: ...
490506
@property

sqlalchemy-stubs/sql/functions.pyi

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from typing import Any, Optional, Union, Iterable, Tuple, TypeVar, Generic, overload
22
from .base import Executable as Executable, ColumnCollection
3-
from .elements import ColumnElement as ColumnElement, Grouping, ClauseList, ColumnElement, Over, WithinGroup, FunctionFilter
3+
from .elements import (
4+
ColumnElement as ColumnElement, Grouping, ClauseList, ColumnElement, Over, WithinGroup, FunctionFilter, AsBoolean
5+
)
46
from .selectable import FromClause as FromClause, Alias, Select
57
from . import util as sqlutil
68
from .visitors import VisitableType as VisitableType
@@ -13,6 +15,8 @@ _T = TypeVar('_T')
1315

1416
def register_function(identifier, fn, package: str = ...): ...
1517

18+
_FE = TypeVar('_FE', bound=FunctionElement)
19+
1620
class FunctionElement(Executable, ColumnElement[_T], FromClause, Generic[_T]): # type: ignore
1721
# ColumnElement.foreign_keys() is not compatible with FromClause.foreign_keys()
1822
packagenames: Any = ...
@@ -35,7 +39,7 @@ class FunctionElement(Executable, ColumnElement[_T], FromClause, Generic[_T]):
3539
ColumnElement[Any],
3640
Iterable[Union[str, ColumnElement[Any]]]]) -> WithinGroup[_T]: ...
3741
@overload
38-
def filter(self) -> FunctionElement[_T]: ...
42+
def filter(self: _FE) -> _FE: ...
3943
@overload
4044
def filter(self, criteria: Any, *criterion: Any) -> FunctionFilter[_T]: ...
4145
def get_children(self, **kwargs: Any) -> Tuple[Grouping[Any]]: ...
@@ -44,7 +48,7 @@ class FunctionElement(Executable, ColumnElement[_T], FromClause, Generic[_T]):
4448
def select(self) -> Select: ... # type: ignore # incompatible with FromClause.select
4549
def scalar(self) -> Any: ... # type: ignore # incompatible with Executable.scalar
4650
def execute(self) -> ResultProxy: ... # type: ignore # incompatible with Executable.execute
47-
def self_group(self, against: Optional[Any] = ...) -> Union[ColumnElement[_T], Grouping[_T]]: ...
51+
def self_group(self: _FE, against: Optional[Any] = ...) -> Union[AsBoolean, Grouping[_T], _FE]: ...
4852

4953
class _FunctionGenerator(object):
5054
opts: Any = ...

0 commit comments

Comments
 (0)