Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
dmadisetti
left a comment
There was a problem hiding this comment.
The original issue reports:
If you then delete the new import cell, the result is once again incorrect, but different:
<builtin doc string>
I don't think this PR captures this case, just wires down the doc string for module creation.
marimo/_runtime/patches.py
Outdated
| if not header: | ||
| return None | ||
| try: | ||
| tree = ast.parse(header) |
There was a problem hiding this comment.
ast_parse since ast can emit stdout
Addresses review feedback from dmadisetti: ast.parse can emit stdout, so use the project's ast_parse wrapper which suppresses SyntaxWarnings.
There was a problem hiding this comment.
Pull request overview
This PR fixes marimo’s execution namespace so that __doc__ reflects the notebook’s actual module docstring (from the notebook header) instead of always returning "Created for the marimo kernel.".
Changes:
- Adds
extract_docstring_from_header()and threads the extracted docstring throughAppMetadata. - Extends
create_main_module()/patch_main_module()to accept an optionaldocused to initialize__main__.__doc__. - Updates kernel/script/pyodide runners and adds tests covering docstring vs default behavior.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/_ast/test_app.py | Adds regression tests ensuring __doc__ matches the notebook header docstring (or default when absent). |
| marimo/_runtime/patches.py | Introduces header docstring extraction and supports passing a custom module __doc__ when creating/patching __main__. |
| marimo/_runtime/commands.py | Extends AppMetadata with a docstring field to carry header docstrings to the kernel. |
| marimo/_runtime/runtime.py | Passes app_metadata.docstring into patch_main_module() so kernel __main__.__doc__ is correct. |
| marimo/_runtime/app/script_runner.py | Extracts header docstring once and passes it into create_main_module() for script-mode execution. |
| marimo/_runtime/app/kernel_runner.py | Passes extracted header docstring into create_main_module() for embedded/kernel-runner execution. |
| marimo/_server/session_manager.py | Populates AppMetadata.docstring from the loaded notebook header when creating sessions. |
| marimo/_server/export/init.py | Populates AppMetadata.docstring during export-driven session creation. |
| marimo/_pyodide/bootstrap.py | Populates AppMetadata.docstring for Pyodide sessions so WASM kernels get the right __doc__. |
| marimo/_pyodide/pyodide_session.py | Passes app_metadata.docstring into patch_main_module() for Pyodide kernel initialization. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
When a cell that redefines __doc__ is deleted, the variable was removed from the module dict, causing Python to fall through to builtins.__doc__. Now __doc__ is restored to the notebook's original docstring from app_metadata.
ast.parse raises ValueError on inputs with null bytes. Catch it alongside SyntaxError to gracefully return None.
Fix unused `outputs` variable in test_run_with_docstring and test_run_with_no_docstring (F841 lint warning).
When a marimo notebook has a module-level docstring (e.g.,
"""This is a test"""), cells referencing__doc__would return"Created for the marimo kernel."instead of the notebook's actual docstring.Root cause
create_main_module()inpatches.pyalways hardcodesdoc="Created for the marimo kernel."when creating the__main__module. Thenotebook's header docstring (parsed and stored in
App._header) wasnever propagated to the execution namespace.
Changes
extract_docstring_from_header()utility inpatches.pythatuses
ast.get_docstring()to extract the actual string value from theraw header text.
docparameter tocreate_main_module()andpatch_main_module()to allow setting a custom__doc__.docstringfield toAppMetadataso it can be threaded fromnotebook loading through to kernel creation.
AppMetadatacreation sites (session manager, export,pyodide bootstrap) to populate
docstringfrom the app's header.AppScriptRunnerandAppKernelRunnerto pass the docstringdirectly to
create_main_module().Closes #8634