11from _typeshed import Incomplete
2- from typing import Any
2+ from collections import Callable
3+ from typing import Any , Generic , Protocol , TypeVar
34
45from ..sql .base import Generative
56from .interfaces import LoaderOption
@@ -23,44 +24,116 @@ class Load(Generative, LoaderOption):
2324 def set_generic_strategy (self , attrs , strategy ) -> None : ...
2425 def set_class_strategy (self , strategy , opts ) -> None : ...
2526 # added dynamically at runtime
26- def contains_eager (self , attr , alias : Incomplete | None = ...): ...
27- def load_only (self , * attrs ): ...
28- def joinedload (self , attr , innerjoin : Incomplete | None = ...): ...
29- def subqueryload (self , attr ): ...
30- def selectinload (self , attr ): ...
31- def lazyload (self , attr ): ...
32- def immediateload (self , attr ): ...
33- def noload (self , attr ): ...
34- def raiseload (self , attr , sql_only : bool = ...): ...
35- def defaultload (self , attr ): ...
36- def defer (self , key , raiseload : bool = ...): ...
37- def undefer (self , key ): ...
38- def undefer_group (self , name ): ...
39- def with_expression (self , key , expression ): ...
40- def selectin_polymorphic (self , classes ): ...
27+ def contains_eager (loadopt , attr , alias : Incomplete | None = ...): ...
28+ def load_only (loadopt , * attrs ): ...
29+ def joinedload (loadopt , attr , innerjoin : Incomplete | None = ...): ...
30+ def subqueryload (loadopt , attr ): ...
31+ def selectinload (loadopt , attr ): ...
32+ def lazyload (loadopt , attr ): ...
33+ def immediateload (loadopt , attr ): ...
34+ def noload (loadopt , attr ): ...
35+ def raiseload (loadopt , attr , sql_only : bool = ...): ...
36+ def defaultload (loadopt , attr ): ...
37+ def defer (loadopt , key , raiseload : bool = ...): ...
38+ def undefer (loadopt , key ): ...
39+ def undefer_group (loadopt , name ): ...
40+ def with_expression (loadopt , key , expression ): ...
41+ def selectin_polymorphic (loadopt , classes ): ...
4142
4243class _UnboundLoad (Load ):
4344 path : Any
4445 local_opts : Any
4546 def __init__ (self ) -> None : ...
4647
47- class loader_option :
48- name : Any
49- fn : Any
50- def __call__ (self , fn ): ...
51-
52- def contains_eager (loadopt , attr , alias : Incomplete | None = ...): ...
53- def load_only (loadopt , * attrs ): ...
54- def joinedload (loadopt , attr , innerjoin : Incomplete | None = ...): ...
55- def subqueryload (loadopt , attr ): ...
56- def selectinload (loadopt , attr ): ...
57- def lazyload (loadopt , attr ): ...
58- def immediateload (loadopt , attr ): ...
59- def noload (loadopt , attr ): ...
60- def raiseload (loadopt , attr , sql_only : bool = ...): ...
61- def defaultload (loadopt , attr ): ...
62- def defer (loadopt , key , raiseload : bool = ...): ...
63- def undefer (loadopt , key ): ...
64- def undefer_group (loadopt , name ): ...
65- def with_expression (loadopt , key , expression ): ...
66- def selectin_polymorphic (loadopt , classes ): ...
48+ ###
49+ # The methods below are decorated with the class loader_option
50+ # They dynamically become instances of loader_option,
51+ # wich is callable with their original parameters.
52+ #
53+ # While both mypy and pyright's validation work, Pylance is unable to
54+ # show the parameters and return types.
55+ #
56+ # There is a workaround (define the method for Pylance, then reassign
57+ # an instance of loader_option to it for mypy, and add pyright+Flake8
58+ # suppressions), but it is too hacky and relies on some unsupported quirks.
59+ #
60+ # Asking Pylance to add support for these generic callables might be preferable.
61+ ###
62+
63+ _F = TypeVar ("_F" , bound = Callable [..., loader_option [Any ]])
64+
65+ class loader_option (Generic [_F ]):
66+ name : str
67+ _dynamic : _F
68+ fn : _F
69+ __call__ : _F # Cheesy "__call__" definition to use the dynamic methods instead
70+
71+ class _contains_eager (Protocol ):
72+ def __call__ (self , loadopt , attr , alias : Incomplete | None = ...) -> loader_option [_contains_eager ]: ...
73+
74+ contains_eager : loader_option [_contains_eager ]
75+
76+ class _load_only (Protocol ):
77+ def __call__ (self , loadopt , * attrs ) -> loader_option [_load_only ]: ...
78+
79+ load_only : loader_option [_load_only ]
80+
81+ class _subqueryload (Protocol ):
82+ def __call__ (self , loadopt , attr ) -> loader_option [_subqueryload ]: ...
83+
84+ subqueryload : loader_option [_subqueryload ]
85+
86+ class _selectinload (Protocol ):
87+ def __call__ (self , loadopt , attr ) -> loader_option [_selectinload ]: ...
88+
89+ selectinload : loader_option [_selectinload ]
90+
91+ class _lazyload (Protocol ):
92+ def __call__ (self , loadopt , attr ) -> loader_option [_lazyload ]: ...
93+
94+ lazyload : loader_option [_lazyload ]
95+
96+ class _immediateload (Protocol ):
97+ def __call__ (self , loadopt , attr ) -> loader_option [_immediateload ]: ...
98+
99+ immediateload : loader_option [_immediateload ]
100+
101+ class _noload (Protocol ):
102+ def __call__ (self , loadopt , attr ) -> loader_option [_noload ]: ...
103+
104+ noload : loader_option [_noload ]
105+
106+ class _raiseload (Protocol ):
107+ def __call__ (self , loadopt , attr , sql_only : bool = ...) -> loader_option [_raiseload ]: ...
108+
109+ raiseload : loader_option [_raiseload ]
110+
111+ class _defaultload (Protocol ):
112+ def __call__ (self , loadopt , attr ) -> loader_option [_defaultload ]: ...
113+
114+ defaultload : loader_option [_defaultload ]
115+
116+ class _defer (Protocol ):
117+ def __call__ (self , loadopt , attr , sql_only : bool = ...) -> loader_option [_defer ]: ...
118+
119+ defer : loader_option [_defer ]
120+
121+ class _undefer (Protocol ):
122+ def __call__ (self , loadopt , key ) -> loader_option [_undefer ]: ...
123+
124+ undefer : loader_option [_undefer ]
125+
126+ class _undefer_group (Protocol ):
127+ def __call__ (self , loadopt , name ) -> loader_option [_undefer_group ]: ...
128+
129+ undefer_group : loader_option [_undefer_group ]
130+
131+ class _with_expression (Protocol ):
132+ def __call__ (self , loadopt , key , expression ) -> loader_option [_with_expression ]: ...
133+
134+ with_expression : loader_option [_with_expression ]
135+
136+ class _selectin_polymorphic (Protocol ):
137+ def __call__ (self , loadopt , classes ) -> loader_option [_selectin_polymorphic ]: ...
138+
139+ selectin_polymorphic : loader_option [_selectin_polymorphic ]
0 commit comments