Make _CellsView behave like a read-only ordered dict#8778
Conversation
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
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
@mscolnick I honestly wonder about something like "cells_dict" or "cells_ordered_dict" to be super explicit to the model. |
There was a problem hiding this comment.
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
NotebookCellDatato useidinstead ofcell_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_id → id. |
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) |
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 |
|
🚀 Development release published. You may be able to view the changes at https://marimo.app?v=0.21.2-dev15 |
Code mode agents naturally reach for
ctx.cells.items()to iterate over cells, which fails because_CellsViewonly supported indexing and plain iteration over values. This addskeys(),values(),items(), and__contains__so the view behaves like a read-only ordered dict, and changes__iter__to yield cell IDs (keys) instead ofNotebookCellDatavalues, matching standard dict semantics.