1111from operator import methodcaller
1212from typing_extensions import Annotated
1313from dataclasses import field , dataclass
14- from inspect import Parameter , Signature , isclass
14+ from inspect import Parameter , Signature
1515from collections .abc import Callable , Iterable , Generator
1616from typing import TYPE_CHECKING , Any , TypeVar , Coroutine
1717from importlib .metadata import Distribution , PackageNotFoundError , distribution
@@ -89,8 +89,8 @@ def flush(self):
8989class Option :
9090 stream : bool = True
9191 scalars : bool = False
92+ calls : tuple [methodcaller , ...] = field (default_factory = tuple )
9293 result : methodcaller | None = None
93- calls : tuple [methodcaller ] = field (default_factory = tuple )
9494
9595
9696@dataclass
@@ -122,12 +122,12 @@ async def __call__(self, *, _session: async_scoped_session, **params: Any) -> An
122122 else :
123123 result = await _session .execute (self .statement , params )
124124
125- for call in self .option .calls :
126- result = call (result )
127-
128125 if self .option .scalars :
129126 result = result .scalars ()
130127
128+ for call in self .option .calls :
129+ result = call (result )
130+
131131 if call := self .option .result :
132132 result = call (result )
133133
@@ -140,14 +140,17 @@ def __hash__(self) -> int:
140140 return hash ((self .statement , self .option ))
141141
142142
143- def generic_issubclass (scls : Any , cls : Any ) -> Any :
144- if cls is Any :
145- return True
143+ def generic_issubclass (scls : Any , cls : Any ) -> bool | list [ Any ] :
144+ if isinstance ( cls , tuple ) :
145+ return _map_generic_issubclass ( repeat ( scls ), cls )
146146
147147 if scls is Any :
148- return cls
148+ return [cls ]
149+
150+ if cls is Any :
151+ return True
149152
150- if isclass ( scls ) and ( isclass ( cls ) or isinstance ( cls , tuple ) ):
153+ with suppress ( TypeError ):
151154 return issubclass (scls , cls )
152155
153156 scls_origin , scls_args = get_origin (scls ) or scls , get_args (scls )
@@ -158,15 +161,17 @@ def generic_issubclass(scls: Any, cls: Any) -> Any:
158161 return generic_issubclass (scls_args [0 ], cls_args )
159162
160163 if len (cls_args ) == 2 and cls_args [1 ] is Ellipsis :
161- return all (map (generic_issubclass , scls_args , repeat (cls_args [0 ])))
164+ return _map_generic_issubclass (
165+ scls_args , repeat (cls_args [0 ]), failfast = True
166+ )
162167
163168 if scls_origin is Annotated :
164169 return generic_issubclass (scls_args [0 ], cls )
165170 if cls_origin is Annotated :
166171 return generic_issubclass (scls , cls_args [0 ])
167172
168173 if origin_is_union (scls_origin ):
169- return all ( map ( generic_issubclass , scls_args , repeat (cls )) )
174+ return _map_generic_issubclass ( scls_args , repeat (cls ), failfast = True )
170175 if origin_is_union (cls_origin ):
171176 return generic_issubclass (scls , cls_args )
172177
@@ -182,9 +187,25 @@ def generic_issubclass(scls: Any, cls: Any) -> Any:
182187 if not cls_args :
183188 return True
184189
185- return len (scls_args ) == len (cls_args ) and all (
186- map (generic_issubclass , scls_args , cls_args )
187- )
190+ if len (scls_args ) != len (cls_args ):
191+ return False
192+
193+ return _map_generic_issubclass (scls_args , cls_args , failfast = True )
194+
195+
196+ def _map_generic_issubclass (
197+ scls : Iterable [Any ], cls : Iterable [Any ], * , failfast : bool = False
198+ ) -> bool | list [Any ]:
199+ results = []
200+ for scls_arg , cls_arg in zip (scls , cls ):
201+ if not (result := generic_issubclass (scls_arg , cls_arg )) and failfast :
202+ return False
203+ elif isinstance (result , list ):
204+ results .extend (result )
205+ elif not isinstance (result , bool ):
206+ results .append (result )
207+
208+ return results or False
188209
189210
190211def return_progressbar (func : Callable [_P , Iterable [_T ]]) -> Callable [_P , Iterable [_T ]]:
@@ -217,7 +238,11 @@ def get_parent_plugins(plugin: Plugin | None) -> Generator[Plugin, Any, None]:
217238def is_editable (plugin : Plugin ) -> bool :
218239 * _ , plugin = get_parent_plugins (plugin )
219240
220- path = files (plugin .module )
241+ try :
242+ path = files (plugin .module )
243+ except TypeError :
244+ return False
245+
221246 if not isinstance (path , Path ) or "site-packages" in path .parts :
222247 return False
223248
0 commit comments