MCPcopy
hub / github.com/nonebot/nonebot2 / _solve

Method _solve

nonebot/internal/params.py:257–319  ·  view source on GitHub ↗
(
        self,
        stack: AsyncExitStack | None = None,
        dependency_cache: T_DependencyCache | None = None,
        **kwargs: Any,
    )

Source from the content-addressed store, hash-verified

255
256 @override
257 async def _solve(
258 self,
259 stack: AsyncExitStack | None = None,
260 dependency_cache: T_DependencyCache | None = None,
261 **kwargs: Any,
262 ) -> Any:
263 use_cache: bool = self.use_cache
264 dependency_cache = {} if dependency_cache is None else dependency_cache
265
266 sub_dependent = self.dependent
267 call = cast(Callable[..., Any], sub_dependent.call)
268
269 # solve sub dependency with current cache
270 exc: BaseExceptionGroup[SkippedException] | None = None
271
272 def _handle_skipped(exc_group: BaseExceptionGroup[SkippedException]):
273 nonlocal exc
274 exc = exc_group
275
276 with catch({SkippedException: _handle_skipped}):
277 sub_values = await sub_dependent.solve(
278 stack=stack,
279 dependency_cache=dependency_cache,
280 **kwargs,
281 )
282
283 if exc is not None:
284 raise exc
285
286 # run dependency function
287 if use_cache and call in dependency_cache:
288 return await dependency_cache[call].wait()
289
290 if is_gen_callable(call) or is_async_gen_callable(call):
291 assert isinstance(stack, AsyncExitStack), (
292 "Generator dependency should be called in context"
293 )
294 if is_gen_callable(call):
295 cm = run_sync_ctx_manager(contextmanager(call)(**sub_values))
296 else:
297 cm = asynccontextmanager(call)(**sub_values)
298
299 target = stack.enter_async_context(cm)
300 elif is_coroutine_callable(call):
301 target = call(**sub_values)
302 else:
303 target = run_sync(call)(**sub_values)
304
305 dependency_cache[call] = cache = DependencyCache()
306 try:
307 result = await target
308 except Exception as e:
309 cache.set_exception(e)
310 raise
311 except BaseException as e:
312 cache.set_exception(e)
313 # remove cache when base exception occurs
314 # e.g. CancelledError

Callers

nothing calls this directly

Calls 11

set_exceptionMethod · 0.95
set_resultMethod · 0.95
is_gen_callableFunction · 0.90
is_async_gen_callableFunction · 0.90
run_sync_ctx_managerFunction · 0.90
is_coroutine_callableFunction · 0.90
run_syncFunction · 0.90
DependencyCacheClass · 0.85
solveMethod · 0.80
waitMethod · 0.80
popMethod · 0.80

Tested by

no test coverage detected