Skip to content

Commit e53944c

Browse files
authored
Improve orm.strategy_options and orm.__init__ (#43)
1 parent 95cbbae commit e53944c

File tree

2 files changed

+141
-53
lines changed

2 files changed

+141
-53
lines changed

sqlalchemy-stubs/orm/__init__.pyi

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

6+
from . import strategy_options
67
from .attributes import AttributeEvent as AttributeEvent
78
from .attributes import InstrumentedAttribute as InstrumentedAttribute
89
from .attributes import Mapped as Mapped
@@ -11,26 +12,21 @@ from .context import QueryContext as QueryContext
1112
from .decl_api import as_declarative as as_declarative
1213
from .decl_api import declarative_base as declarative_base
1314
from .decl_api import declarative_mixin as declarative_mixin
15+
from .decl_api import DeclarativeMeta as DeclarativeMeta
1416
from .decl_api import declared_attr as declared_attr
1517
from .decl_api import has_inherited_table as has_inherited_table
1618
from .decl_api import registry as registry
1719
from .decl_api import synonym_for as synonym_for
1820
from .descriptor_props import CompositeProperty as CompositeProperty
1921
from .descriptor_props import SynonymProperty as SynonymProperty
20-
from .events import AttributeEvents as AttributeEvents
21-
from .events import InstanceEvents as InstanceEvents
22-
from .events import InstrumentationEvents as InstrumentationEvents
23-
from .events import MapperEvents as MapperEvents
24-
from .events import QueryEvents as QueryEvents
25-
from .events import SessionEvents as SessionEvents
22+
from .dynamic import AppenderQuery as AppenderQuery
2623
from .identity import IdentityMap as IdentityMap
2724
from .instrumentation import ClassManager as ClassManager
2825
from .interfaces import EXT_CONTINUE as EXT_CONTINUE
2926
from .interfaces import EXT_SKIP as EXT_SKIP
3027
from .interfaces import EXT_STOP as EXT_STOP
3128
from .interfaces import InspectionAttr as InspectionAttr
3229
from .interfaces import InspectionAttrInfo as InspectionAttrInfo
33-
from .interfaces import LoaderOption
3430
from .interfaces import MANYTOMANY as MANYTOMANY
3531
from .interfaces import MANYTOONE as MANYTOONE
3632
from .interfaces import MapperProperty as MapperProperty
@@ -75,15 +71,14 @@ from .util import polymorphic_union as polymorphic_union
7571
from .util import was_deleted as was_deleted
7672
from .util import with_parent as with_parent
7773
from .util import with_polymorphic as with_polymorphic
78-
from ..util.langhelpers import public_factory as public_factory
7974

8075
def create_session(bind: Optional[Any] = ..., **kwargs: Any) -> Session: ...
8176

8277
with_loader_criteria = LoaderCriteriaOption
8378
relationship = RelationshipProperty
8479

85-
def relation(*arg: Any, **kw: Any) -> RelationshipProperty: ...
86-
def dynamic_loader(argument: Any, **kw: Any) -> RelationshipProperty: ...
80+
def relation(*arg: Any, **kw: Any) -> RelationshipProperty[Any]: ...
81+
def dynamic_loader(argument: Any, **kw: Any) -> RelationshipProperty[Any]: ...
8782

8883
column_property = ColumnProperty
8984
composite = CompositeProperty
@@ -99,25 +94,22 @@ synonym = SynonymProperty
9994

10095
def clear_mappers() -> None: ...
10196

102-
joinedload: LoaderOption
103-
contains_eager: LoaderOption
104-
defer: LoaderOption
105-
undefer: LoaderOption
106-
undefer_group: LoaderOption
107-
with_expression: LoaderOption
108-
load_only: LoaderOption
109-
lazyload: LoaderOption
110-
subqueryload: LoaderOption
111-
selectinload: LoaderOption
112-
immediateload: LoaderOption
113-
noload: LoaderOption
114-
raiseload: LoaderOption
115-
defaultload: LoaderOption
116-
selectin_polymorphic: LoaderOption
97+
joinedload = strategy_options.joinedload._unbound_fn
98+
contains_eager = strategy_options.contains_eager._unbound_fn
99+
defer = strategy_options.defer._unbound_fn
100+
undefer = strategy_options.undefer._unbound_fn
101+
undefer_group = strategy_options.undefer_group._unbound_fn
102+
with_expression = strategy_options.with_expression._unbound_fn
103+
load_only = strategy_options.load_only._unbound_fn
104+
lazyload = strategy_options.lazyload._unbound_fn
105+
subqueryload = strategy_options.subqueryload._unbound_fn
106+
selectinload = strategy_options.selectinload._unbound_fn
107+
immediateload = strategy_options.immediateload._unbound_fn
108+
noload = strategy_options.noload._unbound_fn
109+
raiseload = strategy_options.raiseload._unbound_fn
110+
defaultload = strategy_options.defaultload._unbound_fn
111+
selectin_polymorphic = strategy_options.selectin_polymorphic._unbound_fn
117112

118-
def eagerload(*args: Any, **kwargs: Any) -> LoaderOption: ...
113+
eagerload = joinedload
119114

120-
contains_alias: LoaderOption
121-
122-
# Names in __all__ with no definition:
123-
# AppenderQuery
115+
contains_alias = AliasOption
Lines changed: 119 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
from typing import Any
22
from typing import Optional
3+
from typing import Type
4+
from typing import TypeVar
35

4-
from .attributes import QueryableAttribute as QueryableAttribute
5-
from .base import InspectionAttr as InspectionAttr
6-
from .interfaces import LoaderOption as LoaderOption
7-
from .interfaces import MapperProperty as MapperProperty
8-
from .interfaces import PropComparator as PropComparator
9-
from .path_registry import PathRegistry as PathRegistry
10-
from .path_registry import TokenRegistry as TokenRegistry
11-
from .. import inspect as inspect
12-
from .. import util as util
13-
from ..sql import coercions as coercions
14-
from ..sql import roles as roles
15-
from ..sql import visitors as visitors
16-
from ..sql.base import Generative as Generative
6+
from .interfaces import LoaderOption
7+
from ..sql.base import Generative
8+
9+
_L = TypeVar("_L", bound=Load)
10+
_LO = TypeVar("_LO", bound=loader_option)
1711

1812
class Load(Generative, LoaderOption):
1913
path: Any = ...
@@ -22,24 +16,43 @@ class Load(Generative, LoaderOption):
2216
is_class_strategy: bool = ...
2317
def __init__(self, entity: Any) -> None: ...
2418
@classmethod
25-
def for_existing_path(cls, path: Any): ...
19+
def for_existing_path(cls: Type[_L], path: Any) -> _L: ...
2620
is_opts_only: bool = ...
2721
strategy: Any = ...
2822
propagate_to_loaders: bool = ...
2923
def process_compile_state(self, compile_state: Any) -> None: ...
30-
def options(self, *opts: Any) -> None: ...
24+
def options(self: _L, *opts: Any) -> _L: ...
3125
def set_relationship_strategy(
32-
self, attr: Any, strategy: Any, propagate_to_loaders: bool = ...
33-
) -> None: ...
26+
self: _L, attr: Any, strategy: Any, propagate_to_loaders: bool = ...
27+
) -> _L: ...
3428
def set_column_strategy(
35-
self,
29+
self: _L,
3630
attrs: Any,
3731
strategy: Any,
3832
opts: Optional[Any] = ...,
3933
opts_only: bool = ...,
40-
) -> None: ...
41-
def set_generic_strategy(self, attrs: Any, strategy: Any) -> None: ...
42-
def set_class_strategy(self, strategy: Any, opts: Any) -> None: ...
34+
) -> _L: ...
35+
def set_generic_strategy(self: _L, attrs: Any, strategy: Any) -> _L: ...
36+
def set_class_strategy(self: _L, strategy: Any, opts: Any) -> _L: ...
37+
def contains_eager(
38+
loadopt: _L, attr: Any, alias: Optional[Any] = ...
39+
) -> _L: ...
40+
def load_only(loadopt: _L, *attrs: Any) -> _L: ...
41+
def joinedload(
42+
loadopt: _L, attr: Any, innerjoin: Optional[bool] = ...
43+
) -> _L: ...
44+
def subqueryload(loadopt: _L, attr: Any) -> _L: ...
45+
def selectinload(loadopt: _L, attr: Any) -> _L: ...
46+
def lazyload(loadopt: _L, attr: Any) -> _L: ...
47+
def immediateload(loadopt: _L, attr: Any) -> _L: ...
48+
def noload(loadopt: _L, attr: Any) -> _L: ...
49+
def raiseload(loadopt: _L, attr: Any, sql_only: bool = ...) -> _L: ...
50+
def defaultload(loadopt: _L, attr: Any) -> _L: ...
51+
def defer(loadopt: _L, key: str, raiseload: bool = ...) -> _L: ...
52+
def undefer(loadopt: _L, key: str) -> _L: ...
53+
def undefer_group(loadopt: _L, name: str) -> _L: ...
54+
def with_expression(loadopt: _L, key: Any, expression: Any) -> _L: ...
55+
def selectin_polymorphic(loadopt: _L, classes: Any) -> _L: ...
4356

4457
class _UnboundLoad(Load):
4558
path: Any = ...
@@ -49,5 +62,88 @@ class _UnboundLoad(Load):
4962
class loader_option:
5063
def __init__(self) -> None: ...
5164
name: Any = ...
52-
fn: Any = ...
53-
def __call__(self, fn: Any): ...
65+
def __call__(self: _LO, fn: Any) -> _LO: ...
66+
67+
class _containseager_loader_option(loader_option):
68+
def fn(self, loadopt: _L, attr: Any, alias: Optional[Any] = ...) -> _L: ...
69+
def _unbound_fn(
70+
self, attr: Any, alias: Optional[Any] = ...
71+
) -> _UnboundLoad: ...
72+
73+
class _load_only_loader_option(loader_option):
74+
def fn(self, loadopt: _L, *attrs: Any) -> _L: ...
75+
def _unbound_fn(self, *attrs: Any) -> _UnboundLoad: ...
76+
77+
class _joinedload_loader_option(loader_option):
78+
def fn(
79+
self, loadopt: _L, attr: Any, innerjoin: Optional[bool] = ...
80+
) -> _L: ...
81+
def _unbound_fn(
82+
self, attr: Any, innerjoin: Optional[bool] = ...
83+
) -> _UnboundLoad: ...
84+
85+
class _subqueryload_loader_option(loader_option):
86+
def fn(self, loadopt: _L, attr: Any) -> _L: ...
87+
def _unbound_fn(self, attr: Any) -> _UnboundLoad: ...
88+
89+
class _selectinload_loader_option(loader_option):
90+
def fn(self, loadopt: _L, attr: Any) -> _L: ...
91+
def _unbound_fn(self, attr: Any) -> _UnboundLoad: ...
92+
93+
class _lazyload_loader_option(loader_option):
94+
def fn(self, loadopt: _L, attr: Any) -> _L: ...
95+
def _unbound_fn(self, attr: Any) -> _UnboundLoad: ...
96+
97+
class _immediateload_loader_option(loader_option):
98+
def fn(self, loadopt: _L, attr: Any) -> _L: ...
99+
def _unbound_fn(self, attr: Any) -> _UnboundLoad: ...
100+
101+
class _noload_loader_option(loader_option):
102+
def fn(self, loadopt: _L, attr: Any) -> _L: ...
103+
def _unbound_fn(self, attr: Any) -> _UnboundLoad: ...
104+
105+
class _raiseload_loader_option(loader_option):
106+
def fn(self, loadopt: _L, attr: Any, sql_only: bool = ...) -> _L: ...
107+
def _unbound_fn(self, attr: Any, sql_only: bool = ...) -> _UnboundLoad: ...
108+
109+
class _defaultload_loader_option(loader_option):
110+
def fn(self, loadopt: _L, attr: Any) -> _L: ...
111+
def _unbound_fn(self, attr: Any) -> _UnboundLoad: ...
112+
113+
class _defer_loader_option(loader_option):
114+
def fn(self, loadopt: _L, key: str, raiseload: bool = ...) -> _L: ...
115+
def _unbound_fn(self, key: str, raiseload: bool = ...) -> _UnboundLoad: ...
116+
117+
class _undefer_loader_option(loader_option):
118+
def fn(self, loadopt: _L, key: str) -> _L: ...
119+
def _unbound_fn(self, key: str) -> _UnboundLoad: ...
120+
121+
class _undefer_group_loader_option(loader_option):
122+
def fn(self, loadopt: _L, name: str) -> _L: ...
123+
def _unbound_fn(self, name: str) -> _UnboundLoad: ...
124+
125+
class _with_expression_loader_option(loader_option):
126+
def fn(self, loadopt: _L, key: Any, expression: Any) -> _L: ...
127+
def _unbound_fn(
128+
self, loadopt: _L, key: Any, expression: Any
129+
) -> _UnboundLoad: ...
130+
131+
class _selectin_polymorphic_loader_option(loader_option):
132+
def fn(self, loadopt: _L, classes: Any) -> _L: ...
133+
def _unbound_fn(self, classes: Any) -> _UnboundLoad: ...
134+
135+
contains_eager: _containseager_loader_option
136+
load_only: _load_only_loader_option
137+
joinedload: _joinedload_loader_option
138+
subqueryload: _subqueryload_loader_option
139+
selectinload: _selectinload_loader_option
140+
lazyload: _lazyload_loader_option
141+
immediateload: _immediateload_loader_option
142+
noload: _noload_loader_option
143+
raiseload: _raiseload_loader_option
144+
defaultload: _defaultload_loader_option
145+
defer: _defer_loader_option
146+
undefer: _undefer_loader_option
147+
undefer_group: _undefer_group_loader_option
148+
with_expression: _with_expression_loader_option
149+
selectin_polymorphic: _selectin_polymorphic_loader_option

0 commit comments

Comments
 (0)