Skip to content

chore: allow running and linting docs notebooks#493

Open
mckornfield wants to merge 2 commits into
mainfrom
docs-allow-running-linting/mck
Open

chore: allow running and linting docs notebooks#493
mckornfield wants to merge 2 commits into
mainfrom
docs-allow-running-linting/mck

Conversation

@mckornfield

@mckornfield mckornfield commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Added make docs-check-python-snippets to lint Python fenced-code snippets in Markdown/MDX.
    • Added make docs-run-notebook to run Fern documentation notebooks with marker-based selection.
    • Improved developer setup with an automatic “Next steps” reminder for virtualenv activation.
  • Bug Fixes

    • Enhanced snippet validation with more precise per-doc line/column reporting and mapped type-check diagnostics.
    • Improved notebook/notebook-source selection and snippet expansion behavior, including skip markers.
  • Documentation

    • Updated contributor docs and command guides to reflect snippet-based linting and the new notebook runner/targets.
  • Tests

    • Expanded pytest coverage for snippet linting and notebook runner selection/materialization.

Signed-off-by: Matt Kornfield <mkornfield@nvidia.com>
@mckornfield mckornfield requested review from a team as code owners June 26, 2026 22:04
@github-actions github-actions Bot added the chore label Jun 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 6d24f151-5f36-44e2-8be5-282ab52b9d7a

📥 Commits

Reviewing files that changed from the base of the PR and between ed535ab and 276228b.

📒 Files selected for processing (10)
  • .claude/commands/docs-lint.md
  • .claude/commands/docs-test.md
  • Makefile
  • docs/AGENTS.md
  • docs/_scripts/lint_python_snippets.py
  • docs/_scripts/test_lint_python_snippets.py
  • docs/fern/scripts/README.md
  • docs/fern/scripts/run_notebooks.py
  • docs/fern/scripts/test_run_notebooks.py
  • pytest.ini
✅ Files skipped from review due to trivial changes (3)
  • docs/AGENTS.md
  • docs/fern/scripts/README.md
  • .claude/commands/docs-lint.md
🚧 Files skipped from review as they are similar to previous changes (4)
  • docs/_scripts/test_lint_python_snippets.py
  • docs/fern/scripts/test_run_notebooks.py
  • docs/_scripts/lint_python_snippets.py
  • docs/fern/scripts/run_notebooks.py

📝 Walkthrough

Walkthrough

Adds bootstrap reminders, docs helper targets, a Python snippet linter with tests, and a Fern notebook runner with tests and usage docs.

Changes

Documentation tooling

Layer / File(s) Summary
Bootstrap reminder and docs targets
Makefile, docs/AGENTS.md
Adds bootstrap activation reminder output, new docs helper make targets, and updated docs target references.
Docs command updates
.claude/commands/docs-lint.md, .claude/commands/docs-test.md
Updates the docs-lint and docs-test command guides to reference the new snippet linter and notebook runner paths and markers.
Python snippet linting
docs/_scripts/lint_python_snippets.py
Implements Markdown/MDX discovery, fenced Python snippet extraction, syntax checking, optional type checking, result display, and CLI handling.
Snippet lint tests
docs/_scripts/test_lint_python_snippets.py, pytest.ini
Adds tests for doc discovery, snippet extraction, skip markers, syntax diagnostics, IPython magics, type-check mapping, and pytest discovery.
Notebook runner and docs
docs/fern/scripts/run_notebooks.py, docs/fern/scripts/README.md
Implements Fern MDX and notebook resolution, marker-based selection, notebook execution, cleanup, CLI handling, and runner usage docs.
Notebook runner tests
docs/fern/scripts/test_run_notebooks.py
Adds tests for notebook resolution, selection rules, MDX materialization, and temp file cleanup.

Sequence Diagram(s)

sequenceDiagram
  participant Makefile
  participant Lint as lint_python_snippets.py
  participant Ast as ast.parse
  participant Ty as ty
  Makefile->>Lint: uv run --frozen python docs/_scripts/lint_python_snippets.py DOCS_PATH
  Lint->>Ast: parse fenced Python snippets
  Lint->>Ty: uv run --frozen ty check
  Ty-->>Lint: diagnostics
  Lint-->>Makefile: exit code
Loading
sequenceDiagram
  participant Makefile
  participant Runner as run_notebooks.py
  participant Notebook as NotebookSelection
  participant Executor as nmp.testing.notebooks.execute_notebook
  Makefile->>Runner: uv run --frozen python docs/fern/scripts/run_notebooks.py $(ARGS) $(DOCS_PATH)
  Runner->>Runner: select_notebooks(paths)
  Runner->>Notebook: resolve notebook/source pairs
  Runner->>Executor: execute selected notebook
  Executor-->>Runner: result
  Runner-->>Makefile: exit code
Loading

Suggested reviewers

  • benmccown
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title matches the PR’s main change: adding support to run and lint docs notebooks/snippets.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch docs-allow-running-linting/mck

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 5

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/_scripts/lint_python_snippets.py`:
- Around line 260-263: The temp file naming in PreparedTypeCheckFile creation is
vulnerable to collisions because the sanitized doc_path-based name in the
temp_path construction can overlap for different documents, and prefix-based
matching can misroute diagnostics when temp paths share prefixes. Update the
path generation and lookup logic around PreparedTypeCheckFile, temp_path, and
the diagnostic mapping code in the affected snippet to use a collision-proof
unique identifier per document and exact temp_path equality when resolving
diagnostics, so each source document maps to only its own temporary file and
line mapping.

In `@docs/AGENTS.md`:
- Around line 15-16: The Makefile examples in the docs use the wrong variable
name, so update the documented invocations for docs-check-python-snippets and
docs-run-notebook to use DOCS_PATH instead of DOC_PATH. Keep the wording aligned
with the actual Makefile contract and ensure the examples match the target names
so users can copy them without hitting the empty-variable guard.

In `@docs/fern/scripts/README.md`:
- Around line 84-86: The README examples for make docs-run-notebook use the
wrong variable name, so the commands won’t pick up the notebook path. Update
both example invocations in README to use DOCS_PATH, matching the
docs-run-notebook target and the variable referenced by Makefile; use the
docs-run-notebook command examples as the anchor for the fix.

In `@docs/fern/scripts/run_notebooks.py`:
- Around line 247-250: The MDX preprocessing in run_notebooks() happens before
the per-notebook failure handling, so a materialize_mdx_as_markdown() error can
stop the whole batch. Move the .mdx conversion inside the existing try block for
each notebook selection so include expansion or file I/O failures are caught,
the notebook is marked failed, and processing continues for the remaining items.
Use the existing run_notebooks flow and the selection.path / run_path handling
to keep the logic localized.
- Around line 33-36: Both notebook URL regexes are too strict because they only
allow a single path segment after blob/, so branch refs with slashes do not
match. Update COLAB_NOTEBOOK_RE and FERN_NOTEBOOK_RE in run_notebooks.py to
accept multi-segment refs before the docs/... notebook path, while still
capturing the notebook path in the existing named path group. Keep the rest of
the matching behavior unchanged so notebook links from .mdx pages continue to
resolve.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 00a5b4ad-15c8-4326-902a-b5046d93d3e0

📥 Commits

Reviewing files that changed from the base of the PR and between 3bb3d92 and ed535ab.

📒 Files selected for processing (7)
  • Makefile
  • docs/AGENTS.md
  • docs/_scripts/lint_python_snippets.py
  • docs/_scripts/test_lint_python_snippets.py
  • docs/fern/scripts/README.md
  • docs/fern/scripts/run_notebooks.py
  • docs/fern/scripts/test_run_notebooks.py

Comment on lines +260 to +263
safe_name = re.sub(r"[^A-Za-z0-9_.-]+", "_", str(doc_path.with_suffix("")))
temp_path = temp_dir / f"{safe_name}.py"
temp_path.write_text("\n".join(source_lines), encoding="utf-8")
return PreparedTypeCheckFile(doc_path=doc_path, temp_path=temp_path, line_mapping=tuple(line_mapping))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Make temp paths collision-proof and match diagnostics exactly.

Flattened sanitized names can collide (a/b.md vs a_b.md), overwriting temp files and mappings. Prefix matching can also map diagnostics to the wrong doc when temp paths share prefixes.

Proposed fix
+import hashlib
 import argparse
-    safe_name = re.sub(r"[^A-Za-z0-9_.-]+", "_", str(doc_path.with_suffix("")))
-    temp_path = temp_dir / f"{safe_name}.py"
+    safe_stem = re.sub(r"[^A-Za-z0-9_.-]+", "_", doc_path.with_suffix("").name)[:80] or "snippet"
+    digest = hashlib.sha256(str(doc_path.resolve()).encode("utf-8")).hexdigest()[:12]
+    temp_path = temp_dir / f"{safe_stem}-{digest}.py"
         for line in output.splitlines():
-            matched_prepared: PreparedTypeCheckFile | None = None
-            matched_temp_path: Path | None = None
-            for temp_path, prepared in temp_to_prepared.items():
-                if line.startswith(str(temp_path)):
-                    matched_prepared = prepared
-                    matched_temp_path = temp_path
-                    break
-
-            if matched_prepared is None or matched_temp_path is None:
+            match = TY_OUTPUT_RE.match(line)
+            if match is None:
                 if line.strip():
                     unmatched_lines.append(line)
                 continue
-
-            match = TY_OUTPUT_RE.match(line)
-            if match is None:
-                results[matched_prepared.doc_path].append(
-                    line.replace(str(matched_temp_path), str(matched_prepared.doc_path))
-                )
+
+            matched_prepared = temp_to_prepared.get(Path(match.group(1)))
+            if matched_prepared is None:
+                if line.strip():
+                    unmatched_lines.append(line)
                 continue

Also applies to: 327-351

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/_scripts/lint_python_snippets.py` around lines 260 - 263, The temp file
naming in PreparedTypeCheckFile creation is vulnerable to collisions because the
sanitized doc_path-based name in the temp_path construction can overlap for
different documents, and prefix-based matching can misroute diagnostics when
temp paths share prefixes. Update the path generation and lookup logic around
PreparedTypeCheckFile, temp_path, and the diagnostic mapping code in the
affected snippet to use a collision-proof unique identifier per document and
exact temp_path equality when resolving diagnostics, so each source document
maps to only its own temporary file and line mapping.

Comment thread docs/AGENTS.md Outdated
Comment thread docs/fern/scripts/README.md Outdated
Comment on lines +33 to +36
COLAB_NOTEBOOK_RE = re.compile(
r"https://colab\.research\.google\.com/github/[^/]+/[^/]+/blob/[^/]+/(?P<path>docs/[^)\"'\s]+\.ipynb)"
)
FERN_NOTEBOOK_RE = re.compile(r"colabUrl=[\"'].*?/blob/[^/]+/(?P<path>docs/[^\"']+\.ipynb)[\"']")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Allow slashes in Colab refs.

Both regexes assume the blob/<ref>/... segment has no /. Branch names like docs-allow-running-linting/mck will not match, so .mdx pages that rely on the linked notebook path fail to resolve.

Suggested fix
 COLAB_NOTEBOOK_RE = re.compile(
-    r"https://colab\.research\.google\.com/github/[^/]+/[^/]+/blob/[^/]+/(?P<path>docs/[^)\"'\s]+\.ipynb)"
+    r"https://colab\.research\.google\.com/github/[^/]+/[^/]+/blob/.+?/(?P<path>docs/[^)\"'\s]+\.ipynb)"
 )
-FERN_NOTEBOOK_RE = re.compile(r"colabUrl=[\"'].*?/blob/[^/]+/(?P<path>docs/[^\"']+\.ipynb)[\"']")
+FERN_NOTEBOOK_RE = re.compile(r"colabUrl=[\"'].*?/blob/.+?/(?P<path>docs/[^\"']+\.ipynb)[\"']")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
COLAB_NOTEBOOK_RE = re.compile(
r"https://colab\.research\.google\.com/github/[^/]+/[^/]+/blob/[^/]+/(?P<path>docs/[^)\"'\s]+\.ipynb)"
)
FERN_NOTEBOOK_RE = re.compile(r"colabUrl=[\"'].*?/blob/[^/]+/(?P<path>docs/[^\"']+\.ipynb)[\"']")
COLAB_NOTEBOOK_RE = re.compile(
r"https://colab\.research\.google\.com/github/[^/]+/[^/]+/blob/.+?/(?P<path>docs/[^)\"'\s]+\.ipynb)"
)
FERN_NOTEBOOK_RE = re.compile(r"colabUrl=[\"'].*?/blob/.+?/(?P<path>docs/[^\"']+\.ipynb)[\"']")
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/fern/scripts/run_notebooks.py` around lines 33 - 36, Both notebook URL
regexes are too strict because they only allow a single path segment after
blob/, so branch refs with slashes do not match. Update COLAB_NOTEBOOK_RE and
FERN_NOTEBOOK_RE in run_notebooks.py to accept multi-segment refs before the
docs/... notebook path, while still capturing the notebook path in the existing
named path group. Keep the rest of the matching behavior unchanged so notebook
links from .mdx pages continue to resolve.

Comment thread docs/fern/scripts/run_notebooks.py
@github-actions

github-actions Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor
Suite Lines Covered Line Rate Branch Rate
Unit Tests 21917/28725 76.3% 61.0%
Integration Tests 12582/27405 45.9% 19.2%

@tylersbray tylersbray left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving but do the code rabbit stuff please.

Comment thread docs/AGENTS.md Outdated
Comment thread docs/fern/scripts/README.md Outdated
Signed-off-by: Matt Kornfield <mkornfield@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants