Skip to content

Refactor session extensions to use EventAwareExtension base class#8678

Merged
mscolnick merged 7 commits intomainfrom
ms/extension-cleanup
Mar 30, 2026
Merged

Refactor session extensions to use EventAwareExtension base class#8678
mscolnick merged 7 commits intomainfrom
ms/extension-cleanup

Conversation

@mscolnick
Copy link
Copy Markdown
Contributor

  • Updated various session extensions (e.g., ScratchCellListener, RecentsTrackerListener, CachingExtension) to inherit from EventAwareExtension, enabling automatic subscription to the event bus.
  • Introduced ExtensionRegistry for managing session extensions, replacing direct list manipulations.
  • Refactored event emission methods in SessionEventBus for improved clarity and error handling.
  • Adjusted related tests to ensure compatibility with the new extension structure.

This change enhances the modularity and maintainability of session extensions.

- Updated various session extensions (e.g., ScratchCellListener, RecentsTrackerListener, CachingExtension) to inherit from EventAwareExtension, enabling automatic subscription to the event bus.
- Introduced ExtensionRegistry for managing session extensions, replacing direct list manipulations.
- Refactored event emission methods in SessionEventBus for improved clarity and error handling.
- Adjusted related tests to ensure compatibility with the new extension structure.

This change enhances the modularity and maintainability of session extensions.
Copilot AI review requested due to automatic review settings March 13, 2026 14:35
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 13, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment Mar 30, 2026 4:01pm

Request Review

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors Marimo session extensions to reduce boilerplate and improve safety by centralizing event-bus subscription logic and introducing a small extensions registry abstraction.

Changes:

  • Introduce EventAwareExtension (auto subscribe/unsubscribe) and ExtensionRegistry (typed lookup + snapshot iteration).
  • Refactor SessionEventBus emission to use shared _emit/_emit_async helpers with listener-list snapshotting.
  • Update multiple extensions and tests to align with the new base class/registry behavior.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/_session/extensions/test_extensions.py Updates lifecycle assertions and adds coverage for EventAwareExtension, ExtensionRegistry, and emit snapshot safety.
marimo/_session/session.py Switches extensions storage to ExtensionRegistry and simplifies notification flush via typed lookup.
marimo/_session/file_watcher_integration.py Migrates file watcher extension to EventAwareExtension and simplifies attach/detach logic.
marimo/_session/extensions/types.py Adds EventAwareExtension + ExtensionRegistry definitions.
marimo/_session/extensions/extensions.py Migrates core session extensions to EventAwareExtension; adds NotificationListenerExtension.flush().
marimo/_session/events.py Refactors event dispatch with snapshot iteration and shared error handling.
marimo/_server/session/listeners.py Migrates RecentsTrackerListener to EventAwareExtension.
marimo/_server/scratchpad.py Migrates ScratchCellListener to EventAwareExtension.
marimo/_internal/session/extensions.py Re-exports new extension types in the internal API.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mscolnick mscolnick marked this pull request as ready for review March 17, 2026 03:41
Add EventAwareExtension and ExtensionRegistry to the snapshot.
Use _canonicalize_path in on_attach to match on_detach and
on_session_notebook_renamed, preventing potential watcher leaks
when the raw path string differs from the normalized one.
manzt
manzt previously approved these changes Mar 30, 2026
Copy link
Copy Markdown
Collaborator

@manzt manzt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some good DRY-ness! Nice.

from marimo._session.events import SessionEventBus
from marimo._session.session import Session

_T = TypeVar("_T")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is bounded by being a SessionExtension:

Suggested change
_T = TypeVar("_T")
_T = TypeVar("_T", bound=SessionExtension)


@property
def session(self) -> Session:
assert self._session is not None
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert self._session is not None
if self._session is None:
raise RuntimeError("Extension is not attached to a session")


@property
def event_bus(self) -> SessionEventBus:
assert self._event_bus is not None
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert self._event_bus is not None
if self._event_bus is None:
raise RuntimeError("Extension is not attached to a session")

Comment on lines +544 to +547
with pytest.raises(AssertionError):
ext.session # noqa: B018
with pytest.raises(AssertionError):
ext.event_bus # noqa: B018
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If changes above are accepted:

Suggested change
with pytest.raises(AssertionError):
ext.session # noqa: B018
with pytest.raises(AssertionError):
ext.event_bus # noqa: B018
with pytest.raises(RuntimeError):
ext.session # noqa: B018
with pytest.raises(RuntimeError):
ext.event_bus # noqa: B018

- Bound _T TypeVar to SessionExtension
- Use RuntimeError instead of assert for detached property access
@mscolnick mscolnick merged commit 8f0ebe0 into main Mar 30, 2026
51 of 61 checks passed
@mscolnick mscolnick deleted the ms/extension-cleanup branch March 30, 2026 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants