Skip to content

inital magic comment snapshot implementation - #1404

Open
chiually wants to merge 20 commits into
pyta-uoft:masterfrom
chiually:snapshot-magic-comment
Open

chiually wants to merge 20 commits into
pyta-uoft:masterfrom
chiually:snapshot-magic-comment

Conversation

@chiually

@chiually chiually commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Proposed Changes

Currently, the snapshot function is called with the import call and a code addition for the function call. Instead, we want to be able to write the call to snapshot as a "magic comment" (ex. # pyta: snapshot()) that would be ignored when run normally but take the snapshot of memory when its file is run through the new run_with_snapshot_comments function.

Example

# demo.py
def func(n: int) -> None:
    nums = [n, n + 1]
    # pyta: snapshot(save=True, memory_viz_args=["--output=demo.svg"])


message = "Hello, world"
func(1)
# run_demo.py
from python_ta.debug import run_with_snapshot_comments

run_with_snapshot_comments("demo.py")
Screenshots of your changes (if applicable)

Type of Change

(Write an X or a brief description next to the type or types that best describe your changes.)

Type Applies?
🚨 Breaking change (fix or feature that would cause existing functionality to change)
✨ New feature (non-breaking change that adds functionality) X
🐛 Bug fix (non-breaking change that fixes an issue)
♻️ Refactoring (internal change to codebase, without changing functionality)
🚦 Test update (change that only adds or modifies tests)
📚 Documentation update (change that only updates documentation)
📦 Dependency update (change that updates a dependency)
🔧 Internal (change that only affects developers or continuous integration)

Checklist

(Complete each of the following items for your pull request. Indicate that you have completed an item by changing the [ ] into a [x] in the raw text, or by clicking on the checkbox in the rendered description on GitHub.)

Before opening your pull request:

  • I have performed a self-review of my changes.
    • Check that all changed files included in this pull request are intentional changes.
    • Check that all changes are relevant to the purpose of this pull request, as described above.
  • I have added tests for my changes, if applicable.
    • This is required for all bug fixes and new features.
  • I have updated the project documentation, if applicable.
    • This is required for new features.
  • I have updated the project Changelog (this is required for all changes).
  • If this is my first contribution, I have added myself to the list of contributors.

After opening your pull request:

  • I have verified that the CI tests have passed.
  • I have reviewed the test coverage changes reported by Coveralls.
  • I have requested a review from a project maintainer.

Questions and Comments

Some edits needed to be made to snapshot.py to stop the snapshot function from capturing the frame that calls run_with_snapshot_comments.

Currently, the magic comment only works for comments on its own line (as pictured in the example). Let me know what you think of the current implementation and I will add for inline comments as well (ex. x = 5 # hello)

Also when inserting the snapshot call, a standalone comment's indentation decides how deeply nested it is, and its line number decides which branch it goes in (ex. ifor the else). The ast module records where if,except and case lines are but not where else: and finally: lines are, so for this case we take the first line of code after the previous branch ends to be the else: or finally: line.

@coveralls

coveralls commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Report for CI Build 39895159561

Coverage remained the same at 91.587%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: No coverable lines changed in this PR.
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 4172
Covered Lines: 3821
Line Coverage: 91.59%
Coverage Strength: 21.25 hits per line

💛 - Coveralls

@chiually
chiually requested a review from david-yz-liu October 4, 2026 04:15
@david-yz-liu

Copy link
Copy Markdown
Contributor

Hi @chiually, I find the PR description quite long. Please rewrite it in your own words and shorten it. Focus on a high-level description and a concrete example. I don't need implementation details in the PR description; I will infer this by reviewing the code directly. Only point out anything which you believe is surprising.

Comment thread packages/python-ta/src/python_ta/debug/snapshot.py Outdated
Comment thread packages/python-ta/src/python_ta/debug/snapshot_comments.py Outdated
@chiually
chiually requested a review from david-yz-liu October 16, 2026 17:52

@david-yz-liu david-yz-liu 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.

@chiually Nice work, I left a few inline comments about the overall approach, but I think this is definitely on the right track. Please add test cases and update the documentation.

Comment thread packages/python-ta/src/python_ta/debug/snapshot_comments.py Outdated
Comment thread packages/python-ta/src/python_ta/debug/snapshot_comments.py Outdated
Comment thread packages/python-ta/src/python_ta/debug/snapshot_comments.py Outdated
Comment thread packages/python-ta/src/python_ta/debug/snapshot_comments.py Outdated
Comment thread packages/python-ta/src/python_ta/debug/snapshot_comments.py Outdated
Comment thread packages/python-ta/src/python_ta/debug/snapshot_comments.py Outdated
Comment thread packages/python-ta/src/python_ta/debug/snapshot_comments.py Outdated
Comment thread packages/python-ta/src/python_ta/debug/snapshot_comments.py Outdated
@chiually
chiually requested a review from david-yz-liu October 23, 2026 03:29
@chiually

Copy link
Copy Markdown
Contributor Author

I implemented your feedback and added two additional changes:

  • x = 1; # pyta: snapshot() used to become x = 1; ; __snapshot__(). Now it detects the existing ; and doesn't add a second one.
  • run_with_snapshots now adds the file's folder to sys.path to so that the file's imports resolve the same way they would if you ran the file directly with Python (allows imports from the file's own folder)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants