{"record":{"id":"f9fcf700311d7608","repo":"Textualize/textual","slug":"add-mode-must-be-called-with-a-screen-type-not","errorCode":null,"errorMessage":"add_mode() must be called with a Screen type, not an instance (got instance of {type(base_screen).__name__})","messagePattern":"add_mode\\(\\) must be called with a Screen type, not an instance \\(got instance of (.+?)\\)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/textual/app.py","lineNumber":2693,"sourceCode":"        return await_mount\n\n    def add_mode(self, mode: str, base_screen: str | Callable[[], Screen]) -> None:\n        \"\"\"Adds a mode and its corresponding base screen to the app.\n\n        Args:\n            mode: The new mode.\n            base_screen: The base screen associated with the given mode.\n\n        Raises:\n            InvalidModeError: If the name of the mode is not valid/duplicated.\n        \"\"\"\n        if mode == \"_default\":\n            raise InvalidModeError(\"Cannot use '_default' as a custom mode.\")\n        elif mode in self._modes:\n            raise InvalidModeError(f\"Duplicated mode name {mode!r}.\")\n\n        if isinstance(base_screen, Screen):\n            raise TypeError(\n                \"add_mode() must be called with a Screen type, not an instance\"\n                f\" (got instance of {type(base_screen).__name__})\"\n            )\n        self._modes[mode] = base_screen\n\n    def remove_mode(self, mode: str) -> AwaitComplete:\n        \"\"\"Removes a mode from the app.\n\n        Screens that are running in the stack of that mode are scheduled for pruning.\n\n        Args:\n            mode: The mode to remove. It can't be the active mode.\n\n        Raises:\n            ActiveModeError: If trying to remove the active mode.\n            UnknownModeError: If trying to remove an unknown mode.\n        \"\"\"\n        if mode == self._current_mode:","sourceCodeStart":2675,"sourceCodeEnd":2711,"githubUrl":"https://github.com/Textualize/textual/blob/06dbeef4bb70fb718236aa418ed658ef4667a126/src/textual/app.py#L2675-L2711","documentation":"TypeError raised by App.add_mode when base_screen is an instantiated Screen rather than a type/callable, mirroring the MODES validation: modes store screen factories.","triggerScenarios":"await app.add_mode(\"editor\", EditorScreen()) — note the parentheses creating an instance.","commonSituations":"Easy slip of adding () when typing the call; passing a pre-built screen to share state.","solutions":["Pass the class: await app.add_mode(\"editor\", EditorScreen)","Pass a factory lambda if construction args are needed: lambda: EditorScreen(item)"],"exampleFix":"# before\nawait app.add_mode(\"editor\", EditorScreen())\n\n# after\nawait app.add_mode(\"editor\", EditorScreen)","handlingStrategy":"type-guard","validationCode":"assert not isinstance(base_screen, Screen), 'pass the Screen class'","typeGuard":"def is_screen_factory(v) -> bool:\n    return isinstance(v, str) or (callable(v) and not isinstance(v, Screen))","tryCatchPattern":null,"preventionTips":["Never add () when passing screens to add_mode","Use lambda: Screen(args) for parameterized screens"],"tags":["modes","screens","typeerror","textual"],"backgroundTag":"instance-instead-of-class","analyzedSha":"06dbeef4bb70fb718236aa418ed658ef4667a126","analyzedAt":"2026-08-27T02:36:57.214Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}