Add source code context to assertion failures in terminal environment #156
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR enhances the terminal environment to provide source code context for algorithmic errors (assertion failures), bringing them to parity with runtime and syntax errors which already show helpful context.
Problem
Previously, when an assertion like
assert_equal(call("add", 1, 2), 5)failed, students would only see:While runtime errors already provided much better context:
Solution
This PR adds source code context to assertion failures by:
Creating source context utilities (
pedal/utilities/source_context.py) that can locate function definitions in the student's AST and extract relevant source code lines.Enhancing RuntimeAssertionFeedback to detect function calls in assertion contexts and automatically include the function definition location and source code.
Maintaining backward compatibility - assertions without function calls work exactly as before.
Result
Now when the same assertion fails, students see:
This provides students with immediate context about WHERE in their code the problem is occurring, making debugging significantly easier.
Implementation Details
find_function_definition()to locate functions in student AST by nameget_source_line_context()to extract source code around a given locationRuntimeAssertionFeedbackmessage template to include{source_context}fieldThe enhancement is minimal and surgical - it only activates when an assertion fails and a function call is detected in the context, with no performance impact on passing tests.
Fixes #118.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.