Isolate each call of a function decorated with a MockVWS instance - #3453
Open
adamtheturtle wants to merge 1 commit into
Open
Isolate each call of a function decorated with a MockVWS instance#3453adamtheturtle wants to merge 1 commit into
adamtheturtle wants to merge 1 commit into
Conversation
`MockVWS` inherited from `ContextDecorator`, so every use of one instance shared a single set of databases and targets. Decorating two test functions with one instance therefore made them affect each other, which is the opposite of what the decorator idiom implies and of what `@responses.activate` and `@mock.patch` do. Each call of a decorated function now gets its own target manager and its own API fakes. The databases added to the instance are available inside the call, and, because targets are stored on the caller's own database object, their targets are snapshotted and restored around the call so that a database can still be inspected while the call runs. Using an instance as a context manager is unchanged: a `with` block still shares its state with every other use of the same instance. Along the way: - Replace the constructor arguments held for this purpose with a frozen `_MockVWSOptions` dataclass, so the options are read as typed attributes rather than unpacked from a `dict[str, Any]`. - Keep the started mocks as a stack, so that a decorated function calling another decorated function no longer leaves the outer call's mocking stopped. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
MockVWSinherited fromContextDecorator, so every use of one instance shared a single set of databases and targets, and decorating two test functions with one instance made them affect each other — the opposite of what@responses.activateand@mock.patchdo (#3390).Each call of a decorated function now gets its own target manager and API fakes; the databases added to the instance are available inside the call, and because targets live on the caller's own database object, their targets are snapshotted and restored around the call so a database can still be inspected while the call runs.
Using an instance as a context manager is unchanged: a
withblock still shares its state with every other use of the same instance, andtest_targets_persist_between_callsfrom #3450 is replaced by tests for the new per-call behaviour.Along the way the constructor arguments held for this purpose became a frozen
_MockVWSOptionsdataclass rather than adict[str, Any], and the started mocks became a stack so that a decorated function calling another decorated function no longer leaves the outer call's mocking stopped.Decorator use is now documented in
docs/source/basic-example.rst, where the example is executed by Sybil.🤖 Generated with Claude Code