[PR #1686] fix(tinytorch): strip solution blocks when generating student notebook via tito module start #11867

Open
opened 2026-05-12 19:31:28 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/harvard-edge/cs249r_book/pull/1686
Author: @Shashank-Tripathi-07
Created: 5/6/2026
Status: 🔄 Open

Base: devHead: fix/tinytorch-module-start-strips-solutions


📝 Commits (1)

  • bf9ce85 fix(tinytorch): strip solution blocks when generating student notebook via tito module start

📊 Changes

1 file changed (+38 additions, -8 deletions)

View changed files

📝 tinytorch/tito/commands/module/workflow.py (+38 -8)

📄 Description

Bug

Closes #1684

tito module start 01 was passing the full src/01_tensor/01_tensor.py -- including all ### BEGIN SOLUTION / ### END SOLUTION blocks -- directly to jupytext. Students received notebooks with every answer already implemented, defeating the purpose of the exercises.

The docstring in _create_module_from_src even acknowledged this explicitly:

"Full src/ (including ### BEGIN SOLUTION ... ### END SOLUTION blocks) is passed through to jupytext so notebooks match the source-of-truth"

Root cause

_create_module_from_src in workflow.py called convert_py_to_notebook on the raw source path with no stripping step. Solution stripping was not implemented anywhere in the tito module path.

Fix

Before passing to jupytext, strip solution blocks in memory and write to a temp directory:

lines = src_file.read_text(encoding="utf-8").splitlines(keepends=True)
stripped = []
in_solution = False
for line in lines:
    if line.strip() == "### BEGIN SOLUTION":
        in_solution = True
        continue
    if line.strip() == "### END SOLUTION":
        in_solution = False
        continue
    if not in_solution:
        stripped.append(line)

The stripped source is written to a tempfile.TemporaryDirectory, jupytext converts it, and the temp dir is cleaned up automatically. The original src/ files are never modified.

Verification

After the fix, tito module start 01 generates modules/01_tensor/tensor.ipynb with empty pass stubs and TODO comments -- no implemented solutions.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/harvard-edge/cs249r_book/pull/1686 **Author:** [@Shashank-Tripathi-07](https://github.com/Shashank-Tripathi-07) **Created:** 5/6/2026 **Status:** 🔄 Open **Base:** `dev` ← **Head:** `fix/tinytorch-module-start-strips-solutions` --- ### 📝 Commits (1) - [`bf9ce85`](https://github.com/harvard-edge/cs249r_book/commit/bf9ce8562db93a5224052ae9989c84923c7a3590) fix(tinytorch): strip solution blocks when generating student notebook via tito module start ### 📊 Changes **1 file changed** (+38 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `tinytorch/tito/commands/module/workflow.py` (+38 -8) </details> ### 📄 Description ## Bug Closes #1684 `tito module start 01` was passing the full `src/01_tensor/01_tensor.py` -- including all `### BEGIN SOLUTION` / `### END SOLUTION` blocks -- directly to jupytext. Students received notebooks with every answer already implemented, defeating the purpose of the exercises. The docstring in `_create_module_from_src` even acknowledged this explicitly: > "Full `src/` (including `### BEGIN SOLUTION` ... `### END SOLUTION` blocks) is passed through to jupytext so notebooks match the source-of-truth" ## Root cause `_create_module_from_src` in `workflow.py` called `convert_py_to_notebook` on the raw source path with no stripping step. Solution stripping was not implemented anywhere in the `tito module` path. ## Fix Before passing to jupytext, strip solution blocks in memory and write to a temp directory: ```python lines = src_file.read_text(encoding="utf-8").splitlines(keepends=True) stripped = [] in_solution = False for line in lines: if line.strip() == "### BEGIN SOLUTION": in_solution = True continue if line.strip() == "### END SOLUTION": in_solution = False continue if not in_solution: stripped.append(line) ``` The stripped source is written to a `tempfile.TemporaryDirectory`, jupytext converts it, and the temp dir is cleaned up automatically. The original `src/` files are never modified. ## Verification After the fix, `tito module start 01` generates `modules/01_tensor/tensor.ipynb` with empty `pass` stubs and TODO comments -- no implemented solutions. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-05-12 19:31:28 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/cs249r_book#11867