[docs]asyncdefregister_app(app:FastAPI)->None:"""Register the given FastAPI app for constructing fake request later."""global_app# noqa: PLW0603asyncwith_app_lock:_app=app
def_get_app()->FastAPI|None:"""Get the registered FastAPI app."""return_app
[docs]asyncdefresolve_dependencies(func:Callable[P,T]|Callable[P,Awaitable[T]],*,use_cache:bool=True,provided_kwargs:dict[str,Any]|None=None,)->dict[str,Any]:"""Resolve dependencies for the given function using FastAPI's dependency injection system. This function resolves dependencies defined via FastAPI's dependency mechanism and returns a dictionary of resolved arguments for the given function. Args: func: The function for which dependencies need to be resolved. It can be a synchronous or asynchronous callable. use_cache: Whether to use a cache for dependency resolution. Defaults to True. provided_kwargs: Explicit kwargs passed by the caller (these override DI). Returns: A dictionary mapping argument names to resolved dependency values. Notes: - A fake HTTP request is created to mimic FastAPI's request-based dependency resolution. """provided_kwargs=provided_kwargsor{}root_dep=get_dependant(path="command",call=func)# Get names of actual dependency (Depends()) parametersdependency_names={param.nameforparaminroot_dep.dependenciesifparam.name}# Drop dependencies that are already satisfied by provided kwargseffective_dependencies=[depfordepinroot_dep.dependenciesifdep.namenotinprovided_kwargs]root_dep.dependencies=effective_dependenciesfake_request_scope:dict[str,Any]={"type":"http","headers":[],"query_string":"",}app=_get_app()ifappisnotNone:fake_request_scope["app"]=appfake_request=Request(fake_request_scope)root_dep.call=cast("Callable[..., Any]",root_dep.call)async_exit_stack=awaitasync_exit_stack_manager.get_stack(root_dep.call)cache=dependency_cache.get()ifuse_cacheelseNonesolved_dependency=awaitsolve_dependencies(request=fake_request,dependant=root_dep,async_exit_stack=async_exit_stack,embed_body_fields=False,dependency_cache=cache,dependency_overrides_provider=app,)ifcacheisnotNone:cache.update(solved_dependency.dependency_cache)resolved={param_name:valueforparam_name,valueinsolved_dependency.values.items()ifparam_nameindependency_names}return{**resolved,**provided_kwargs}