Repository navigation
fix(testing): keep MemoryBackend active in async decorated functions - #582
Merged
leandrodamascena merged 5 commits intoNov 6, 2026
Merged
leandrodamascena merged 5 commits into
leandrodamascena merged 5 commits into
Conversation
The sync wrapper returned the coroutine and left the context before the coroutine ran. Use an async wrapper for coroutine functions, so the backend stays active until the function ends. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01G1VdxZmWX7QvJykW7HN8rR
Cover async success, an await inside the function, cleanup after an exception, restoring a previous client, and the sync decorator. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01G1VdxZmWX7QvJykW7HN8rR
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01G1VdxZmWX7QvJykW7HN8rR
leandrodamascena
approved these changes
Nov 6, 2026
leandrodamascena
left a comment
Member
There was a problem hiding this comment.
Thanks, Juan Pablo! Looks good to me. I confirmed that the six regression cases fail before the fix and pass here. Cancellation, repeated calls, and nested decorators also restore the previous client correctly, and the sync behavior is preserved.
All 1,213 unit tests and 212 examples passed locally, along with the documentation build, Ruff, and ty. I’ll approve the pending CI runs before we merge. Thanks for picking this up!
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.



Summary
@MemoryBackend()wrapped every function in a sync wrapper. For anasync def, the wrapper returned the coroutine and left the backend context before the coroutine ran. So the model failed withValueError: No client configured.Closes #560
Changes
MemoryBackend.__call__()now uses an async wrapper for coroutine functions. The wrapper awaits the function inside the context, so the backend stays active until the function ends. Sync functions use the same wrapper as before.tests/unit/test_memory_backend.pyfor async success, an await inside the function, cleanup after an exception, restoring a previous default client, the example from the issue withasyncio.run(), and the existing sync use.docs/guides/testing.md, and an example in the class docstring.Behavior to check
Tests
Built with
maturin developon Python 3.13.pytest tests/unit/test_memory_backend.py -q: 55 passed.pytest tests/unit -q: 1211 passed, 2 skipped.ruff checkandruff format --checkpass on the changed Python files.ty check python/pydynox/testing/memory.pyreports one diagnostic, on a@contextmanagerreturn type that this change does not touch.cargo testor the integration tests, because no Rust code changed and the integration tests need LocalStack.Assignment note: I commented on the issue first, as it asks, but I did not wait for the assignment before opening this PR. Please close it if someone else should take the issue.
Disclosure: Claude, an AI agent, wrote this change and ran the checks above from the account of the person opening this PR.
🤖 Generated with Claude Code
https://claude.ai/code/session_01G1VdxZmWX7QvJykW7HN8rR