feat: actionable error when a memray Tracker is already active (pytest-memray interop)#89
Merged
Merged
Conversation
memray allows only one Tracker per process, so running pytest-memray's --memray and a benchmem fixture on the same test makes benchmem's nested Tracker raise a terse "No more than one Tracker instance can be active" RuntimeError. _track_once now catches that specific case and re-raises with the likely cause (pytest-memray on the same test) and the fix (run them on different tests/sessions). Any other RuntimeError propagates unchanged; if memray's wording changes we fall back to its original error. Mirrors the existing _compute_statistics guard's style. Closes #82 (part 3; parts 1-2 shipped in #86/#87). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Documentation build overview
5 files changed ·
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
Part 3 of #82 — the pytest-memray interop guard.
memray allows only one
Trackerper process. So if pytest-memray's--memrayis profiling a test and a benchmem fixture runs on that same test, benchmem's nestedTrackerraises memray's terse:_track_oncenow catches that specific case and re-raises with the likely cause and the fix:Scope / rationale
This is deliberately just a message upgrade — memray already fails loudly and safely, so the guard prevents nothing new; it makes the failure self-explanatory for the interop case. It matches the existing
_compute_statisticsguard, which already translates an internal-memray error into actionable guidance. We considered shipping only the doc caveat (memray's own error isn't terrible) but opted to keep the friendlier message, in keeping with the house style."more than one Tracker"); any otherRuntimeErrorpropagates unchanged, and if memray changes the text we fall back to its original error (graceful, low-consequence coupling, pinned by the test).Tests
test_nested_tracker_raises_actionable_error— opens an outermemray.Tracker(standing in for--memray) and assertsmeasure_memory(...)raises the actionableRuntimeError. Fulltest_memray.py: 7 passed.ruff format --diff,ruff check, andmypy src + testsall clean (the exact CI gates).Docs
README "With pytest-memray" caveat now notes benchmem fails that test with an actionable error rather than a terse memray one.
Closes #82.
🤖 Generated with Claude Code