Skip to content

ci: check the Python inside %%capture-style cells - #58

Merged
NirDiamant merged 1 commit into
mainfrom
fix/smoke-cell-magics
Oct 31, 2026
Merged

NirDiamant merged 1 commit into
mainfrom
fix/smoke-cell-magics

Conversation

@NirDiamant

@NirDiamant NirDiamant commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

The notebook smoke check skipped every cell that starts with a cell magic, so a syntax error inside a %%capture cell passed CI. This compiles the body of %%capture, %%time, %%timeit and %%prun cells and still skips %%bash, %%writefile and other non-Python magics. Found by the Qodo review on GenAI_Agents #182. Checked locally: the check passes on main's notebooks.

🤖 Generated with Claude Code


PR Summary by Qodo

Compile Python bodies in supported notebook cell magics

🐞 Bug fix 🧪 Tests 🕐 Less than 10 minutes


AI Description

• Catch syntax errors previously missed inside %%capture, %%time, %%timeit, and %%prun cells.
• Continue skipping non-Python cell magics while preserving the smoke check’s compile-only behavior.
Diagram

graph TD
  CI["CI smoke check"] --> Scan["Notebook cells"] --> Kind{"Cell magic?"}
  Kind -->|supported Python magic| Strip["Remove directive"] --> Filter["Filter IPython lines"] --> Compile["Compile notebook"]
  Kind -->|ordinary code| Filter
  Kind -->|other magic| Skip["Skip cell"]
Loading
High-Level Assessment

The small allowlist fits a compile-only smoke check: it covers the specified Python-wrapping magics without treating arbitrary magic bodies as Python. Using IPython’s input transformation would add dependency and behavior complexity for this narrow check.

Files changed (1) +9 / -4

Bug fix (1) +9 / -4
notebook-smoke.ymlCheck Python code inside supported cell magics +9/-4

Check Python code inside supported cell magics

• The notebook smoke check now recognizes %%capture, %%time, %%timeit, and %%prun, removes their directive lines, and compiles their Python bodies alongside other code cells. It continues to skip other cell magics and filter IPython line magics and shell escapes.

.github/workflows/notebook-smoke.yml

Summary by CodeRabbit

  • Bug Fixes
    • Notebook compilation now checks the Python body of cells using capture, time, timeit, and prun magics, while continuing to skip cells with other magics.
    • Leading whitespace no longer prevents cell magics from being recognized.

The smoke check skipped every cell that starts with a cell magic, so a
syntax error inside a %%capture cell passed CI (Qodo review note on
GenAI_Agents #182). Cells under %%bash, %%writefile and other non-Python
magics are still skipped.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Caution

Review failed

The pull request is closed.

Walkthrough

The notebook smoke workflow now strips leading whitespace before detecting a cell magic. It checks the bodies of %%capture, %%time, %%timeit, and %%prun cells as Python, while skipping other cell magics and retaining filters for line magics and shell escapes.

Changes: Notebook smoke check

Layer / File(s) Summary
Cell magic filtering
.github/workflows/notebook-smoke.yml
The workflow detects cell magics after stripping leading whitespace. It checks the bodies of capture, time, timeit, and prun cells as Python, skips cells with other magics, and retains filtering for line magics and shell escapes.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Possibly related PRs

Merge Risk: Low · up to 50fae

This updates the notebook smoke check to compile the Python bodies of four cell magics. No current notebook triggers a failure, but a valid future notebook with a %%prun header statement or a late future import could fail CI. The check does not affect production behavior, so these can be fixed as follow-ups.

Finishing Touches
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@qodo-code-review

qodo-code-review Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (3) 📘 Rule violations (0) 📜 Skill insights (0)



🟠 Medium

1. Indented notebook cells pass the check 🐞 Bug ≡ Correctness
Description
cell.source.lstrip() now removes leading whitespace from the first line of every code cell before
compilation. In a first cell containing     if True:\n        value = 1, the workflow compiles a
dedented if statement even though compiling the original cell source raises IndentationError.
Code

.github/workflows/notebook-smoke.yml[48]

+                      body = cell.source.lstrip().splitlines()
Evidence
The changed extraction strips whitespace from the whole source before splitting it, while the
compiled payload is assembled from those altered lines. The example's first line therefore loses its
unexpected indentation before it reaches compile().

.github/workflows/notebook-smoke.yml[45-62]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The new unconditional `lstrip()` changes ordinary code-cell source before the syntax check and can conceal invalid initial indentation.
## Fix Focus Areas
- .github/workflows/notebook-smoke.yml[48-55]
## Recommended Fix
Split the original cell source without stripping it. If leading whitespace must be ignored when identifying a cell-magic header, apply `lstrip()` only to the line being inspected, not to the source passed onward for compilation.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


2. Valid magic cells can fail the check 🐞 Bug ≡ Correctness
Description
The line filter removes lines starting with % or ! from the newly included magic bodies without
distinguishing IPython commands from text inside Python strings. For example, a %%time body
containing text = """\n%""" is valid Python, but removing its closing line leaves an unterminated
string for compile().
Code

.github/workflows/notebook-smoke.yml[58]

+                          line for line in body
Evidence
The changed source for the filter is now body, including selected magic bodies. The predicate
removes %""", and the workflow compiles the remaining unterminated string.

.github/workflows/notebook-smoke.yml[48-62]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
Applying the existing prefix-based line filter to newly included magic bodies can delete string-literal content and make valid cells fail compilation.
## Fix Focus Areas
- .github/workflows/notebook-smoke.yml[48-62]
## Recommended Fix
Make IPython-command removal aware of Python string boundaries so `%` and `!` lines inside strings remain intact. Add a selected-magic test containing a multiline string whose closing line starts with `%`.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


3. Some invalid magic cells pass the check 🐞 Bug ≡ Correctness
Description
cells.append() collects the newly admitted magic bodies, but the workflow compiles all cells as
one string rather than checking each body independently. Two cells containing %%time\nx = ( and
%%capture\n1) each have invalid Python bodies, yet their combined source compiles successfully.
Code

.github/workflows/notebook-smoke.yml[R52-55]

+                          if magic not in {"capture", "time", "timeit", "prun"}:
+                              continue
+                          # These wrap ordinary Python, so check the body.
+                          body = body[1:]
Evidence
The new branch adds selected magic bodies to cells, and the unchanged final call compiles those
bodies only after joining them with other cells. The stated two-cell example becomes x = (\n\n1),
which compiles despite neither cell body compiling on its own.

.github/workflows/notebook-smoke.yml[48-62]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The newly included cell-magic bodies can complete each other's incomplete Python when the workflow compiles their concatenated source, hiding errors in individual cells.
## Fix Focus Areas
- .github/workflows/notebook-smoke.yml[48-62]
## Recommended Fix
After stripping an eligible magic header and handling IPython-only lines, compile each processed code cell independently rather than compiling the joined notebook. Keep failures attributed to the notebook, preferably with the cell index.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗



Context sources
❌ Tickets: failed to load — Ticket collection could not establish complete evidence
✅ REVIEW.md
Review mode: Auto: ⚖️ Balanced: CI logic changes parsing behavior and warrants a careful single-pass review.

Tip of the day
💡 Did you know, you can keep summaries lean with Findings visible per group, which tucks the rest behind a View link

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗


Powered by Qodo

Comment on lines +52 to +55
if magic not in {"capture", "time", "timeit", "prun"}:
continue
# These wrap ordinary Python, so check the body.
body = body[1:]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 Medium

1. Some invalid magic cells pass the check 🐞 Bug ≡ Correctness

cells.append() collects the newly admitted magic bodies, but the workflow compiles all cells as
one string rather than checking each body independently. Two cells containing %%time\nx = ( and
%%capture\n1) each have invalid Python bodies, yet their combined source compiles successfully.
Agent Prompt
## Issue description
The newly included cell-magic bodies can complete each other's incomplete Python when the workflow compiles their concatenated source, hiding errors in individual cells.
## Fix Focus Areas
- .github/workflows/notebook-smoke.yml[48-62]
## Recommended Fix
After stripping an eligible magic header and handling IPython-only lines, compile each processed code cell independently rather than compiling the joined notebook. Keep failures attributed to the notebook, preferably with the cell index.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗

# IPython line magics and shell escapes are not Python; drop them.
lines = [
line for line in cell.source.splitlines()
line for line in body

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 Medium

2. Valid magic cells can fail the check 🐞 Bug ≡ Correctness

The line filter removes lines starting with % or ! from the newly included magic bodies without
distinguishing IPython commands from text inside Python strings. For example, a %%time body
containing text = """\n%""" is valid Python, but removing its closing line leaves an unterminated
string for compile().
Agent Prompt
## Issue description
Applying the existing prefix-based line filter to newly included magic bodies can delete string-literal content and make valid cells fail compilation.
## Fix Focus Areas
- .github/workflows/notebook-smoke.yml[48-62]
## Recommended Fix
Make IPython-command removal aware of Python string boundaries so `%` and `!` lines inside strings remain intact. Add a selected-magic test containing a multiline string whose closing line starts with `%`.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗

# A cell magic such as %%bash makes the whole cell non-Python; skip it.
if cell.source.lstrip().startswith("%%"):
continue
body = cell.source.lstrip().splitlines()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 Medium

3. Indented notebook cells pass the check 🐞 Bug ≡ Correctness

cell.source.lstrip() now removes leading whitespace from the first line of every code cell before
compilation. In a first cell containing     if True:\n        value = 1, the workflow compiles a
dedented if statement even though compiling the original cell source raises IndentationError.
Agent Prompt
## Issue description
The new unconditional `lstrip()` changes ordinary code-cell source before the syntax check and can conceal invalid initial indentation.
## Fix Focus Areas
- .github/workflows/notebook-smoke.yml[48-55]
## Recommended Fix
Split the original cell source without stripping it. If leading whitespace must be ignored when identifying a cell-magic header, apply `lstrip()` only to the line being inspected, not to the source passed onward for compilation.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗

@NirDiamant
NirDiamant merged commit 4b2d7d0 into main Oct 31, 2026
2 of 3 checks passed
@NirDiamant
NirDiamant deleted the fix/smoke-cell-magics branch October 31, 2026 00:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant