Skip to content

Isolate each call of a function decorated with a MockVWS instance - #3453

Open
adamtheturtle wants to merge 1 commit into
mainfrom
adamtheturtle/issue-3390
Open

Isolate each call of a function decorated with a MockVWS instance#3453
adamtheturtle wants to merge 1 commit into
mainfrom
adamtheturtle/issue-3390

Conversation

@adamtheturtle

Copy link
Copy Markdown
Member

MockVWS inherited from ContextDecorator, 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.activate and @mock.patch do (#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 with block still shares its state with every other use of the same instance, and test_targets_persist_between_calls from #3450 is replaced by tests for the new per-call behaviour.

Along the way the constructor arguments held for this purpose became a frozen _MockVWSOptions dataclass rather than a dict[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

`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>
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.

1 participant