Skip to content

Commit 9cb21f4

Browse files
authored
Merge pull request #5 from layday/fix-3
Flesh out `Relationship` annotations
2 parents 936bee5 + 12462a2 commit 9cb21f4

File tree

2 files changed

+74
-57
lines changed

2 files changed

+74
-57
lines changed

sqlalchemy-stubs/orm/__init__.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from typing import Any
2+
from typing import Mapping
23
from typing import Optional
4+
from typing import Tuple
35

46
from .attributes import AttributeEvent as AttributeEvent
57
from .attributes import InstrumentedAttribute as InstrumentedAttribute
@@ -94,7 +96,9 @@ def dynamic_loader(argument: Any, **kw: Any) -> RelationshipProperty: ...
9496
column_property = ColumnProperty
9597
composite = CompositeProperty
9698

97-
def backref(name: Any, **kwargs: Any): ...
99+
_BackrefResult = Tuple[str, Mapping[str, Any]]
100+
101+
def backref(name: str, **kwargs: Any) -> _BackrefResult: ...
98102
def deferred(*columns: Any, **kw: Any) -> ColumnProperty: ...
99103
def query_expression(default_expr: Any = ...) -> ColumnProperty: ...
100104

sqlalchemy-stubs/orm/relationships.pyi

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
from typing import AbstractSet
12
from typing import Any
3+
from typing import Callable
4+
from typing import Mapping
25
from typing import Optional
6+
from typing import Sequence
7+
from typing import Tuple
38
from typing import Type
49
from typing import TypeVar
510
from typing import Union
611

12+
from typing_extensions import Literal
13+
14+
from . import _BackrefResult
715
from . import attributes as attributes
816
from .base import state_str as state_str
917
from .interfaces import MANYTOMANY as MANYTOMANY
@@ -19,6 +27,7 @@ from .. import schema as schema
1927
from .. import sql as sql
2028
from .. import util as util
2129
from ..inspection import inspect as inspect
30+
from ..schema import Column
2231
from ..sql import coercions as coercions
2332
from ..sql import expression as expression
2433
from ..sql import operators as operators
@@ -35,78 +44,82 @@ def foreign(expr: Any): ...
3544

3645
_T = TypeVar("_T")
3746

47+
_InfoDict = Mapping[Any, Any]
48+
3849
class RelationshipProperty(StrategizedProperty[_T]):
39-
strategy_wildcard_key: str = ...
40-
inherit_cache: bool = ...
41-
uselist: Any = ...
42-
argument: Any = ...
43-
secondary: Any = ...
44-
primaryjoin: Any = ...
45-
secondaryjoin: Any = ...
46-
post_update: Any = ...
47-
direction: Any = ...
48-
viewonly: Any = ...
49-
sync_backref: Any = ...
50-
lazy: Any = ...
51-
single_parent: Any = ...
52-
collection_class: Optional[Type] = ...
53-
passive_deletes: Any = ...
54-
cascade_backrefs: Any = ...
55-
passive_updates: Any = ...
56-
remote_side: Any = ...
57-
enable_typechecks: Any = ...
58-
query_class: Any = ...
59-
innerjoin: Any = ...
60-
distinct_target_key: Any = ...
61-
doc: Any = ...
62-
active_history: Any = ...
63-
join_depth: Any = ...
64-
omit_join: Any = ...
65-
local_remote_pairs: Any = ...
66-
bake_queries: Any = ...
67-
load_on_pending: Any = ...
68-
comparator_factory: Any = ...
69-
comparator: Any = ...
70-
info: Any = ...
71-
strategy_key: Any = ...
72-
order_by: Any = ...
73-
back_populates: Any = ...
74-
backref: Any = ...
50+
strategy_wildcard_key: str
51+
inherit_cache: bool
52+
uselist: Optional[bool]
53+
argument: Any
54+
secondary: Any
55+
primaryjoin: Any
56+
secondaryjoin: Any
57+
post_update: bool
58+
direction: Any
59+
viewonly: Any
60+
sync_backref: Any
61+
lazy: str
62+
single_parent: Any
63+
collection_class: Optional[Type]
64+
passive_deletes: Union[bool, Literal["all"]]
65+
cascade_backrefs: bool
66+
passive_updates: bool
67+
remote_side: Any
68+
enable_typechecks: bool # NOTE: not documented
69+
query_class: Any
70+
innerjoin: Union[bool, str]
71+
distinct_target_key: Optional[bool]
72+
doc: Optional[str]
73+
active_history: bool
74+
join_depth: Optional[int]
75+
omit_join: Optional[Literal[False]]
76+
local_remote_pairs: Any # NOTE: not documented
77+
bake_queries: bool
78+
load_on_pending: bool
79+
comparator_factory: Any
80+
comparator: Any
81+
info: _InfoDict # NOTE: not set if constructor argument is ``None``
82+
strategy_key: Tuple[Tuple[str, str]] # NOTE: not documented
83+
order_by: Any
84+
back_populates: Union[None, str]
85+
backref: Union[None, str, _BackrefResult]
7586
def __init__(
7687
self,
7788
argument: Any,
7889
secondary: Optional[Any] = ...,
7990
primaryjoin: Optional[Any] = ...,
8091
secondaryjoin: Optional[Any] = ...,
8192
foreign_keys: Optional[Any] = ...,
82-
uselist: Optional[Any] = ...,
83-
order_by: bool = ...,
84-
backref: Optional[Any] = ...,
85-
back_populates: Optional[Any] = ...,
86-
overlaps: Optional[Any] = ...,
93+
uselist: Optional[bool] = ...,
94+
order_by: Union[
95+
Literal[False], str, Column, Callable[[], Column]
96+
] = ...,
97+
backref: Union[str, _BackrefResult] = ...,
98+
back_populates: str = ...,
99+
overlaps: Union[AbstractSet[str], str] = ...,
87100
post_update: bool = ...,
88-
cascade: bool = ...,
101+
cascade: Union[Literal[False], Sequence[str]] = ...,
89102
viewonly: bool = ...,
90103
lazy: str = ...,
91104
collection_class: Optional[Any] = ...,
92-
passive_deletes: Any = ...,
93-
passive_updates: Any = ...,
105+
passive_deletes: Union[bool, Literal["all"]] = ...,
106+
passive_updates: bool = ...,
94107
remote_side: Optional[Any] = ...,
95-
enable_typechecks: Any = ...,
96-
join_depth: Optional[Any] = ...,
108+
enable_typechecks: bool = ..., # NOTE: not documented
109+
join_depth: Optional[int] = ...,
97110
comparator_factory: Optional[Any] = ...,
98111
single_parent: bool = ...,
99-
innerjoin: bool = ...,
100-
distinct_target_key: Optional[Any] = ...,
101-
doc: Optional[Any] = ...,
102-
active_history: Any = ...,
103-
cascade_backrefs: Any = ...,
112+
innerjoin: Union[bool, str] = ...,
113+
distinct_target_key: Optional[bool] = ...,
114+
doc: Optional[str] = ...,
115+
active_history: bool = ...,
116+
cascade_backrefs: bool = ...,
104117
load_on_pending: bool = ...,
105118
bake_queries: bool = ...,
106119
_local_remote_pairs: Optional[Any] = ...,
107120
query_class: Optional[Any] = ...,
108-
info: Optional[Any] = ...,
109-
omit_join: Optional[Any] = ...,
121+
info: Optional[_InfoDict] = ...,
122+
omit_join: Optional[Literal[False]] = ...,
110123
sync_backref: Optional[Any] = ...,
111124
) -> None: ...
112125
def instrument_class(self, mapper: Any) -> None: ...
@@ -154,12 +167,12 @@ class RelationshipProperty(StrategizedProperty[_T]):
154167
halt_on: Optional[Any] = ...,
155168
) -> None: ...
156169
def entity(self) -> Union[AliasedInsp, Mapper]: ...
157-
def mapper(self): ...
170+
def mapper(self) -> Mapper: ...
158171
def do_init(self) -> None: ...
159172
@property
160-
def cascade(self): ...
173+
def cascade(self) -> CascadeOptions: ...
161174
@cascade.setter
162-
def cascade(self, cascade: Any) -> None: ...
175+
def cascade(self, cascade: Sequence[str]) -> None: ...
163176

164177
class JoinCondition:
165178
parent_persist_selectable: Any = ...

0 commit comments

Comments
 (0)