Skip to content

fix: harden conversion path for ipynbs#8795

Merged
dmadisetti merged 1 commit intomainfrom
dm/mo-5557
Mar 20, 2026
Merged

fix: harden conversion path for ipynbs#8795
dmadisetti merged 1 commit intomainfrom
dm/mo-5557

Conversation

@dmadisetti
Copy link
Copy Markdown
Collaborator

📝 Summary

Catches Visitor errors on extracting a jupyter cell and just passes the value unchanged as a fallback. Previously conversion would loudly fail (see #8783), cause issues upstream on molab

Moreover, we had some loose asserts for sanity checking- but these should prompt the user to act opposed to loudly fail.

Copilot AI review requested due to automatic review settings March 20, 2026 18:32
@vercel
Copy link
Copy Markdown

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

Request Review

Copy link
Copy Markdown
Contributor

@mscolnick mscolnick left a comment

Choose a reason for hiding this comment

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

nice, great stuff!

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 hardens the Jupyter (.ipynb) → marimo IR conversion pipeline so that unsupported/edge-case notebook cells (notably import *) don’t cause the entire conversion to fail, improving robustness for Molab’s notebook importer (issue #8783).

Changes:

  • Catch ScopedVisitor/ImportStarError (as SyntaxError) during duplicate-definition renaming so star-import cells are preserved instead of crashing the transform.
  • Replace assert-based “sanity checks” in the source transform pipeline with a guarded runner that skips failed transforms and logs warnings.
  • Add unit + end-to-end tests ensuring notebooks with star imports convert successfully.

Reviewed changes

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

File Description
marimo/_convert/ipynb/to_ir.py Adds per-cell fallback for visitor errors and introduces _run_transform to skip failing/invalid transforms instead of asserting.
tests/_convert/ipynb/test_ipynb_to_ir.py Adds regression tests for star-import notebooks and the duplicate-definition transform behavior.

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

Comment on lines +1458 to +1465
except Exception:
logger.warning(
"Notebook conversion transform '%s' failed; "
"skipping this optimization. "
"Please report this at %s",
name,
_REPORT_URL,
exc_info=True,
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

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

In _run_transform, exc_info=True will log full tracebacks for exceptions raised while processing user-supplied notebook code. For exceptions like SyntaxError, the exception repr/traceback can include the offending source line, which may leak notebook contents into server logs (Molab/import flows). Consider logging the exception type/message without exc_info, or redacting/sampling so user code isn’t emitted to logs by default.

Suggested change
except Exception:
logger.warning(
"Notebook conversion transform '%s' failed; "
"skipping this optimization. "
"Please report this at %s",
name,
_REPORT_URL,
exc_info=True,
except Exception as exc:
logger.warning(
"Notebook conversion transform '%s' failed with %s: %s; "
"skipping this optimization. "
"Please report this at %s",
name,
type(exc).__name__,
exc,
_REPORT_URL,

Copilot uses AI. Check for mistakes.
Comment on lines 1492 to 1496
# Run comment-preserving transforms
for base_transform in comment_preserving_transforms:
transform = comment_preserver(base_transform)
new_sources = transform(sources)
assert len(new_sources) == len(sources), (
f"{base_transform.__name__} changed cell count"
)
sources = new_sources
sources = _run_transform(base_transform.__name__, transform, sources)

Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

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

_run_transform validates that a transform didn’t change cell count, but when used with CommentPreserver the wrapped transform has side effects (it updates the preserver’s internal comment index based on transformed_sources). If a transform returns a different-length list, _run_transform discards it and returns the original sources, but CommentPreserver has already been updated to the wrong length, which can corrupt comment preservation for subsequent transforms. Suggest moving the length-check before updating CommentPreserver state (e.g., add a guard in CommentPreserver.wrapper to skip _update_comments_for_transformed_sources when lengths differ, or perform the check outside the wrapper and only update state when accepted).

Copilot uses AI. Check for mistakes.
@dmadisetti dmadisetti added the bug Something isn't working label Mar 20, 2026
@dmadisetti dmadisetti merged commit 5af91b5 into main Mar 20, 2026
47 of 49 checks passed
@dmadisetti dmadisetti deleted the dm/mo-5557 branch March 20, 2026 19:20
@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-dev22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants