async def _(api: Api) -> None: component = use_component(RootComponent, api=api) assert component.baz("foo", "bar") another = use_component(RootAnotherComponent, api=api) assert await another.foobar() with raises(LookupError): use_component(MountedComponent)
async def _(api: Api, store: type[AnyComponentProtocol]): async with api.client(): store_instance = use_component(store, api=api) assert isinstance(store_instance, store) store_instance._startup.assert_called_once() store_instance._shutdown.assert_not_called() store_instance._shutdown.assert_called_once()
class MountedView: async def on_get(self, req: Request, resp: Response, /, **kw) -> Response: try: use_component(MountedComponent) except KeyError: return resp.set_status( HTTPStatus.NOT_FOUND).set_text("not found") return resp.set_status(HTTPStatus.OK).set_text("ok")
async def _(root_api: Api, mounted_api: Api) -> None: assert use_component(RootComponent, api=root_api) with raises(KeyError): use_component(RootComponent, api=mounted_api) assert use_component(MountedComponent, api=mounted_api) with raises(KeyError): use_component(MountedComponent, api=root_api)
async def startup(self): assert use_component(self.__class__)
def baz(self, *args) -> str: another = use_component(RootAnotherComponent) another.barbaz() self._already_in_context() return str(args)
async def shutdown(self): with raises(KeyError): use_component(RootComponent)