{"record":{"id":"a963119d0c6d6b9e","repo":"aio-libs/aiohttp","slug":"the-first-argument-should-be-web-application-insta","errorCode":null,"errorMessage":"The first argument should be web.Application instance, got {app!r}","messagePattern":"The first argument should be web\\.Application instance, got (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"aiohttp/web_runner.py","lineNumber":440,"sourceCode":"    async def _cleanup_server(self) -> None:\n        pass\n\n\nclass AppRunner(BaseRunner[Request]):\n    \"\"\"Web Application runner\"\"\"\n\n    __slots__ = (\"_app\",)\n\n    def __init__(\n        self,\n        app: Application,\n        *,\n        handle_signals: bool = False,\n        access_log_class: type[AbstractAccessLogger] = AccessLogger,\n        **kwargs: Any,\n    ) -> None:\n        if not isinstance(app, Application):\n            raise TypeError(\n                f\"The first argument should be web.Application instance, got {app!r}\"\n            )\n        kwargs[\"access_log_class\"] = access_log_class\n\n        if app._handler_args:\n            for k, v in app._handler_args.items():\n                kwargs[k] = v\n\n        if not issubclass(kwargs[\"access_log_class\"], AbstractAccessLogger):\n            raise TypeError(\n                \"access_log_class must be subclass of \"\n                \"aiohttp.abc.AbstractAccessLogger, got {}\".format(\n                    kwargs[\"access_log_class\"]\n                )\n            )\n\n        super().__init__(handle_signals=handle_signals, **kwargs)\n        self._app = app","sourceCodeStart":422,"sourceCodeEnd":458,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/d041d4d0fd48c3f0832084d33be16cf1c4835f85/aiohttp/web_runner.py#L422-L458","documentation":"AppRunner.__init__ requires its first positional argument to be an instance of aiohttp.web.Application. The constructor performs an isinstance(app, Application) check at entry and raises TypeError if it fails, because every downstream step (handler_args extraction, super().__init__ setup, self._app assignment) assumes a real Application object. Passing anything else — a string, a dict, a Router, a None — would cause cryptic AttributeError failures later, so aiohttp fails fast with a descriptive message.","triggerScenarios":"Constructing AppRunner directly (AppRunner(app)) with a non-Application object: e.g. AppRunner(router), AppRunner(None), AppRunner('my-app'), or passing a sub-app (the nested Application added via add_subapp) when the outer Application is intended. Also triggered by passing an Application subclass that does not actually inherit from aiohttp.web.Application.","commonSituations":"Mistaking web.AppRunner for a generic runner that accepts a Router object; copy-pasting from examples that build the Application in a separate function and forgetting the call; test fixtures that return a mock or a partially-built object; refactoring that accidentally passes the app's .router attribute instead of the app itself.","solutions":["Ensure the first argument is an instance of aiohttp.web.Application — call web.Application() to create one if needed.","If you are passing app.router, change it to the Application instance itself.","In tests, construct a real Application (web.Application()) rather than a mock or a bare dict.","If subclassing Application, confirm the subclass derives from aiohttp.web.Application."],"exampleFix":"// before\nrunner = web.AppRunner(router)  # router is app.router\n// after\nrunner = web.AppRunner(app)    # pass the Application instance","handlingStrategy":"type-guard","validationCode":"from aiohttp import web\n\ndef make_runner(app) -> web.AppRunner:\n    if not isinstance(app, web.Application):\n        raise TypeError(f'Expected web.Application, got {type(app).__name__}')\n    return web.AppRunner(app)","typeGuard":"from aiohttp import web\n\ndef is_application(app) -> bool:\n    return isinstance(app, web.Application)","tryCatchPattern":"try:\n    runner = web.AppRunner(app)\nexcept TypeError as e:\n    if 'should be web.Application' in str(e):\n        # log and abort startup\n        raise\n    raise","preventionTips":["Always construct the Application with web.Application() in the same scope where you build the runner.","Add a startup assertion: assert isinstance(app, web.Application).","In tests, use a factory fixture that returns a fresh web.Application()."],"tags":["web-server","app-runner","configuration","type-check"],"backgroundTag":null,"analyzedSha":"d041d4d0fd48c3f0832084d33be16cf1c4835f85","analyzedAt":"2026-08-11T20:44:15.550Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}