{"record":{"id":"a8eec60ec885d39f","repo":"oraios/serena","slug":"this-method-must-be-overridden-for-each-subclass","errorCode":null,"errorMessage":"This method must be overridden for each subclass","messagePattern":"This method must be overridden for each subclass","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"src/serena/code_editor.py","lineNumber":59,"sourceCode":"            Fully resets the contents of the file.\n\n            :param contents: the new contents\n            \"\"\"\n\n        @abstractmethod\n        def delete_text_between_positions(self, start_pos: PositionInFile, end_pos: PositionInFile) -> None:\n            pass\n\n        @abstractmethod\n        def insert_text_at_position(self, pos: PositionInFile, text: str) -> None:\n            pass\n\n    @contextmanager\n    def _open_file_context(self, relative_path: str) -> Iterator[\"CodeEditor.EditedFile\"]:\n        \"\"\"\n        Context manager for opening a file\n        \"\"\"\n        raise NotImplementedError(\"This method must be overridden for each subclass\")\n\n    def read_file(self, relative_path: str, lines: tuple[int, int] | None = None) -> str:\n        \"\"\"\n        Reads the content of a file.\n\n        :param relative_path: the relative path of the file to read\n        :param lines: tuple of (first_line, last_line) to read only a specific line range (0-based, inclusive)\n        :return: the content of the file\n        \"\"\"\n        with self._open_file_context(relative_path) as file:\n            contents = file.get_contents()\n            if lines is None:\n                return contents\n            else:\n                first_line, last_line = lines\n                return TextUtils.get_text_in_lines_range(contents, first_line, last_line)\n\n    @contextmanager","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/oraios/serena/blob/7fcbca7e62555ec2287ddb2f083caee805848ea6/src/serena/code_editor.py#L41-L77","documentation":"CodeEditor is an abstract base whose `_open_file_context` context manager must be implemented by each concrete editor subclass (e.g. the SereneFileEditor backed by a language server). The base implementation raises NotImplementedError. Hitting it means you invoked a read/edit path on a subclass that never overrode it.","triggerScenarios":"Calling read_file, edited_file_context (and thus replace_body, insert_at_line, delete_lines, etc.), or edited_file_context directly on a CodeEditor instance whose class doesn't override `_open_file_context`.","commonSituations":"Custom editor subclasses inheriting from CodeEditor without implementing `_open_file_context`; tests or tools instantiating the base CodeEditor directly; a refactor adding a new editor backend that missed the method.","solutions":["Use a concrete editor subclass (e.g. the LS-backed editor serena instantiates for real projects) instead of the base class.","If you wrote a subclass, implement `_open_file_context` to yield an EditedFile for the given relative path.","Check that project initialization created the correct editor type for the project's language.","Upgrade serena if a bundled subclass is missing the override (likely a version bug)."],"exampleFix":"// before\nclass MyEditor(CodeEditor):\n    pass  # _open_file_context not implemented\n// after\nclass MyEditor(CodeEditor):\n    @contextmanager\n    def _open_file_context(self, relative_path):\n        with open(...) as f:\n            yield self.EditedFile(...)","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"from inspect import isabstract\nfrom serena.code_editor import CodeEditor\n\ndef has_file_context(editor: CodeEditor) -> bool:\n    cls = type(editor)\n    return not isabstract(cls) and cls._open_file_context is not CodeEditor._open_file_context\n\nif not has_file_context(editor):\n    raise RuntimeError('Editor does not implement _open_file_context; use a concrete subclass')","tryCatchPattern":"try:\n    with editor.read_file('src/main.py') as content:\n        ...\nexcept NotImplementedError:\n    print('This editor subclass does not support file I/O; use the concrete project editor')","preventionTips":["Never instantiate CodeEditor directly; always use the editor serena creates for the project.","When subclassing, implement `_open_file_context` before using any read/edit APIs.","Add a unit test exercising read_file through your custom editor subclass."],"tags":["python","abstract-method","not-implemented","code-editor"],"backgroundTag":"notimplemented-abstract-method","analyzedSha":"7fcbca7e62555ec2287ddb2f083caee805848ea6","analyzedAt":"2026-08-29T00:04:09.619Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}