Skip to content

Make _CellsView behave like a read-only ordered dict#8778

Merged
manzt merged 1 commit intomainfrom
push-kowopywztnzl
Mar 20, 2026
Merged

Make _CellsView behave like a read-only ordered dict#8778
manzt merged 1 commit intomainfrom
push-kowopywztnzl

Conversation

@manzt
Copy link
Copy Markdown
Collaborator

@manzt manzt commented Mar 19, 2026

Code mode agents naturally reach for ctx.cells.items() to iterate over cells, which fails because _CellsView only supported indexing and plain iteration over values. This adds keys(), values(), items(), and __contains__ so the view behaves like a read-only ordered dict, and changes __iter__ to yield cell IDs (keys) instead of NotebookCellData values, matching standard dict semantics.

for cid, cell in ctx.cells.items(): ...
"my_cell" in ctx.cells  # membership by ID or name

Code mode agents naturally reach for `ctx.cells.items()` to iterate
over cells, which fails because `_CellsView` only supported indexing
and plain iteration over values. This adds `keys()`, `values()`,
`items()`, and `__contains__` so the view behaves like a read-only
ordered dict, and changes `__iter__` to yield cell IDs (keys) instead
of `NotebookCellData` values, matching standard dict semantics.

    for cid, cell in ctx.cells.items(): ...
    "my_cell" in ctx.cells  # membership by ID or name
Copilot AI review requested due to automatic review settings March 19, 2026 18:23
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 19, 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 19, 2026 6:23pm

Request Review

@manzt manzt added the enhancement New feature or request label Mar 19, 2026
@manzt
Copy link
Copy Markdown
Collaborator Author

manzt commented Mar 19, 2026

@mscolnick I honestly wonder about something like "cells_dict" or "cells_ordered_dict" to be super explicit to the model.

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

This PR updates the code-mode AsyncCodeModeContext.cells view (_CellsView) to behave more like a read-only ordered dict, enabling standard dict-style access patterns (e.g., items(), keys(), and membership tests) that code-mode agents commonly expect.

Changes:

  • Added _CellsView.keys(), .values(), .items(), and __contains__, and changed __iter__ to yield cell IDs (keys).
  • Updated NotebookCellData to use id instead of cell_id.
  • Expanded/updated tests to cover dict-like iteration and membership semantics.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
marimo/_code_mode/_context.py Makes _CellsView dict-like (keys/values/items/contains) and changes iteration to yield IDs; renames NotebookCellData.cell_idid.
tests/_code_mode/test_cells_view.py Updates existing assertions and adds new test coverage for iteration/keys/values/items/contains behavior.

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


def __contains__(self, key: object) -> bool:
if isinstance(key, int):
return 0 <= key < len(self)
@mscolnick
Copy link
Copy Markdown
Contributor

@mscolnick I honestly wonder about something like "cells_dict" or "cells_ordered_dict" to be super explicit to the model.

I feel like the docs would be better if it's already inspecting. I like the addition of the list-like properties to view too

@manzt manzt merged commit 3e2c6fa into main Mar 20, 2026
48 of 49 checks passed
@manzt manzt deleted the push-kowopywztnzl branch March 20, 2026 15:01
@github-actions
Copy link
Copy Markdown

🚀 Development release published. You may be able to view the changes at https://marimo.app?v=0.21.2-dev15

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