oraios/serena · error · NotImplementedError

This method must be overridden for each subclass

Error message

This method must be overridden for each subclass

What it means

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.

Source

Thrown at src/serena/code_editor.py:59

            Fully resets the contents of the file.

            :param contents: the new contents
            """

        @abstractmethod
        def delete_text_between_positions(self, start_pos: PositionInFile, end_pos: PositionInFile) -> None:
            pass

        @abstractmethod
        def insert_text_at_position(self, pos: PositionInFile, text: str) -> None:
            pass

    @contextmanager
    def _open_file_context(self, relative_path: str) -> Iterator["CodeEditor.EditedFile"]:
        """
        Context manager for opening a file
        """
        raise NotImplementedError("This method must be overridden for each subclass")

    def read_file(self, relative_path: str, lines: tuple[int, int] | None = None) -> str:
        """
        Reads the content of a file.

        :param relative_path: the relative path of the file to read
        :param lines: tuple of (first_line, last_line) to read only a specific line range (0-based, inclusive)
        :return: the content of the file
        """
        with self._open_file_context(relative_path) as file:
            contents = file.get_contents()
            if lines is None:
                return contents
            else:
                first_line, last_line = lines
                return TextUtils.get_text_in_lines_range(contents, first_line, last_line)

    @contextmanager

View on GitHub (pinned to 7fcbca7e62)

Solutions

  1. Use a concrete editor subclass (e.g. the LS-backed editor serena instantiates for real projects) instead of the base class.
  2. If you wrote a subclass, implement `_open_file_context` to yield an EditedFile for the given relative path.
  3. Check that project initialization created the correct editor type for the project's language.
  4. Upgrade serena if a bundled subclass is missing the override (likely a version bug).

Example fix

// before
class MyEditor(CodeEditor):
    pass  # _open_file_context not implemented
// after
class MyEditor(CodeEditor):
    @contextmanager
    def _open_file_context(self, relative_path):
        with open(...) as f:
            yield self.EditedFile(...)
Defensive patterns

Strategy: type-guard

Type guard

from inspect import isabstract
from serena.code_editor import CodeEditor

def has_file_context(editor: CodeEditor) -> bool:
    cls = type(editor)
    return not isabstract(cls) and cls._open_file_context is not CodeEditor._open_file_context

if not has_file_context(editor):
    raise RuntimeError('Editor does not implement _open_file_context; use a concrete subclass')

Try / catch

try:
    with editor.read_file('src/main.py') as content:
        ...
except NotImplementedError:
    print('This editor subclass does not support file I/O; use the concrete project editor')

Prevention

When it happens

Trigger: 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`.

Common situations: 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.

Related errors


AI-assisted analysis of oraios/serena@7fcbca7e62 (2026-08-29). Data as JSON: /api/errors/a8eec60ec885d39f. Report an issue: GitHub.