{"record":{"id":"5ef21e76c60966af","repo":"aio-libs/aiohttp","slug":"inheritance-class-cls-name-from-web-applicat","errorCode":null,"errorMessage":"Inheritance class {cls.__name__} from web.Application is forbidden","messagePattern":"Inheritance class (.+?) from web\\.Application is forbidden","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"aiohttp/web_app.py","lineNumber":135,"sourceCode":"        # initialized on freezing\n        self._run_middlewares: bool | None = None\n\n        self._state: dict[AppKey[Any] | str, object] = {}\n        self._frozen = False\n        self._pre_frozen = False\n        self._subapps: _Subapps = []\n\n        self._on_response_prepare: _RespPrepareSignal = Signal(self)\n        self._on_startup: _AppSignal = Signal(self)\n        self._on_shutdown: _AppSignal = Signal(self)\n        self._on_cleanup: _AppSignal = Signal(self)\n        self._cleanup_ctx = CleanupContext()\n        self._on_startup.append(self._cleanup_ctx._on_startup)\n        self._on_cleanup.append(self._cleanup_ctx._on_cleanup)\n        self._client_max_size = client_max_size\n\n    def __init_subclass__(cls: type[\"Application\"]) -> None:\n        raise TypeError(\n            f\"Inheritance class {cls.__name__} from web.Application is forbidden\"\n        )\n\n    # MutableMapping API\n\n    def __eq__(self, other: object) -> bool:\n        return self is other\n\n    @overload  # type: ignore[override]\n    def __getitem__(self, key: AppKey[_T]) -> _T: ...\n\n    @overload\n    def __getitem__(self, key: str) -> Any: ...\n\n    def __getitem__(self, key: str | AppKey[_T]) -> Any:\n        return self._state[key]\n\n    def _check_frozen(self) -> None:","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/d041d4d0fd48c3f0832084d33be16cf1c4835f85/aiohttp/web_app.py#L117-L153","documentation":"Raised by Application.__init_subclass__ when a user class subclasses web.Application. aiohttp's Application is designed to be configured via composition (routers, middlewares, signals, cleanup contexts) rather than inheritance, and subclassing can break freezing, signal wiring, and internal invariants. The metaclass-level hook makes subclassing impossible at class-creation time.","triggerScenarios":"Writing class MyApp(web.Application): ...; trying to add methods or attributes by inheriting from Application; framework wrappers that attempt to subclass instead of compose.","commonSituations":"Migrating from other frameworks (Flask/Django) where subclassing an app class is idiomatic; attempting to bundle helper methods onto the app object.","solutions":["Use composition: instantiate app = web.Application(...) and attach helpers as attributes or in the application mapping via app['key'].","Put shared logic in middleware or a cleanup context registered on a normal Application instance.","For grouping, use add_subapp / sub-applications rather than inheritance."],"exampleFix":"# before\nclass MyApp(web.Application):\n    def helper(self): ...\n\n# after\napp = web.Application()\ndef helper(app):\n    ...\napp['helper'] = helper","handlingStrategy":"type-guard","validationCode":"app = web.Application(middlewares=[...])\n# never: class MyApp(web.Application)\n# attach helpers instead:\napp['helpers'] = MyHelpers()","typeGuard":"def is_application(obj) -> bool:\n    return type(obj) is web.Application or (isinstance(obj, web.Application) and type(obj).__mro__[1] is web.Application)","tryCatchPattern":null,"preventionTips":["Do not subclass web.Application; aiohttp forbids it by design.","Use composition: store helpers/state on the app via app['key'].","Add cross-cutting behavior via middleware, not inheritance."],"tags":["web","application","design","api-misuse"],"backgroundTag":null,"analyzedSha":"d041d4d0fd48c3f0832084d33be16cf1c4835f85","analyzedAt":"2026-08-11T20:44:15.550Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}