Auto-import documented objects into autodoc doctest REPLs#18
Conversation
When pyrepl_autodoc_packages is set, generate a silent bootstrap script that imports the documented object from autodoc desc_signature metadata before replaying doctest input. This lets unqualified doctest names like example_generator work without changing docstrings. Bootstrap scripts are written alongside replay files under _static/pyrepl/ and emitted as src= on autodoc py-repl widgets. Co-authored-by: chrizzftd <chrizzFTD@users.noreply.github.com>
Documentation build overview
|
|
@cursoragent review |
PR Review: Auto-import documented objects into autodoc doctest REPLs (#18)Verdict: Approve with minor notes. The change is focused, fits existing machinery, and solves the real gap left by PR #17. RTD preview built successfully. What works wellCorrect problem/solution split. Wheel install ( Minimal, testable surface area. Import derivation is isolated in def autodoc_bootstrap_source(
module: str | None,
fullname: str | None,
objtype: str | None,
) -> str | None:
"""Return a silent import script for the documented autodoc object."""
if not module:
return None
if objtype == "module" or not fullname:
return f"import {module}\n"
if "." in fullname:
root = fullname.split(".", 1)[0]
return f"from {module} import {root}\n"
return f"from {module} import {fullname}\n"Replay stays faithful. Imports go into a separate bootstrap file, not prepended to replay scripts — doctest docstrings remain unchanged and the interactive UX only shows doctest input. Gating is sensible. Bootstrap only when both autodoc Good test coverage for the happy path.
The Minor issues / suggestions1. Wheel must match the documented package (document, don’t block merge)
2. Bootstrap copy is nested under replay metadata loop replay_dest = outdir / "_static" / "pyrepl"
for docname, metadata in app.env.metadata.items():
raw = metadata.get(REPLAY_FILES_KEY)
if not raw:
continue
...
raw_bootstrap = metadata.get(BOOTSTRAP_FILES_KEY)
if raw_bootstrap:
bootstrap_files = json.loads(raw_bootstrap)
for name, content in bootstrap_files.items():
(replay_dest / name).write_text(content, encoding="utf-8")Bootstrap files are only written when replay files exist for that docname. That’s always true today because they’re registered together, but a separate loop over 3. Known edge cases (acceptable for v1, worth documenting)
None of these are regressions; they’re inherent limits of metadata-only import guessing. A short “Limitations” bullet in the PR or README would help. 4. Optional test gap (non-blocking) No test for 5. Silent bootstrap failures are invisible to users If the import fails at runtime (wrong wheel, missing symbol), pyrepl-web suppresses bootstrap output and the user hits Code quality
Summary
Recommendation: merge once PR #17 lands (or merge stacked). The Optional follow-ups (separate PRs): limitations docs, |
45128d7
into
cursor/autodoc-packages-config-c412
PR #18 merged auto-import bootstrap that emits silent src= bootstrap scripts alongside packages=. The prior cleanup commit removed the src parameter from make_pyrepl_raw, causing TypeError in transform_doctest_blocks and failing CI/RTD builds. Co-authored-by: chrizzftd <chrizzFTD@users.noreply.github.com>


Summary
Completes the autodoc REPL workflow from PR #17 by automatically importing the documented object into the browser REPL namespace before doctest replay.
When
pyrepl_autodoc_packagesis set, each autodoc doctest REPL now gets a silent generated bootstrap script (emitted assrc=) derived from autodocdesc_signaturemetadata. Doctest examples can keep using unqualified names likeexample_generatorwithout changing docstrings.Problem
PR #17 installs the documented package via
micropip.install(), but replay still failed with:Wheel install only makes
import packagepossible; it does not bind documented names into the REPL globals.Solution
At build time, derive an import statement from autodoc metadata:
autofunction:: pkg.mod.funcfrom pkg.mod import funcautoclass:: pkg.Widgetfrom pkg import Widgetfrom pkg import Outer(first segment offullname)automodule:: pkg.modimport pkg.modBootstrap scripts are written to
_static/pyrepl/{docname}-{n}-bootstrap.pyand run silently before interactive replay.Example output
For
pyrepl_test_pkg.demo.example_generator:Bootstrap (
example-1-bootstrap.py):Replay (
example-1.py, unchanged):Design notes
pyrepl_autodoc_importoptionpyrepl_autodoc_packagesis set:src:bootstrap in pyrepl-webTests
tests/test_autodoc_bootstrap_source.py— unit tests for import derivationtests/test_autodoc_bootstrap.py— asserts bootstrapsrc=, metadata, and written files for function and class casespyrepl_autodoc_packagesis unset