Skip to content

Preserve iterator matchers for every request - #848

Open
anticipate218 wants to merge 2 commits into
getsentry:masterfrom
anticipate218:fix/reuse-iterator-matchers
Open

anticipate218 wants to merge 2 commits into
getsentry:masterfrom
anticipate218:fix/reuse-iterator-matchers

Conversation

@anticipate218

@anticipate218 anticipate218 commented Nov 12, 2026 •

Copy link
Copy Markdown

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update
  • Other

Description

The documented match iterable can be a generator or iterator. Currently, matching consumes it, so later requests can bypass callbacks that were already consumed. For example, after one request with the required header succeeds, subsequent requests with a wrong header also receive the mocked response. An earlier failed attempt can similarly leave only part of the callback chain.

Materialize the iterable as a tuple when the response is created, as the existing URL-query path already does. All attempts then run the same callback collection. This also lets a response built with generator matchers be registered twice through the registry's existing deep-copy path. The original iterable is consumed at construction; later changes to an input list do not change the response's callbacks. The callback objects themselves are retained.

Related Issues

No existing issue identified for this matcher-collection lifecycle bug. The iterator request-body change in #840 concerns request content and is a different path.

PR checklist

Before submitting this pull request, I have done the following:

  • Read the contributing guidelines
  • Ran tox and pre-commit checks locally: their wrappers and full interpreter matrix were not run; direct checks are listed below.
  • Added my changes to the CHANGES file

Added/updated tests?

  • Yes
  • No, and this is why
  • I need help with writing tests

Validation on Windows / CPython 3.13.5:

  • Final regression: 4 failures and 4 passing list/tuple controls before the fix; all 8 pass after it. Covers static and callback responses, repeated successful and failed attempts, and repeated response registration.
  • Complete pytest suite with the configured async/coverage options: 269 passed, 18 deprecation warnings. The unchanged base has 261 passed and the same warnings.
  • The changed production line and all 50 new test statements are covered. The same 8 pre-existing uncovered lines remain, so overall coverage still displays 99%.
  • Independent request/registry comparisons: 32 combinations passed.
  • Direct applicable checks using the pinned Black 23.3.0, isort 5.12.0, Flake8 7.1.1, pyupgrade 3.10.1, blacken-docs 1.14.0 and pre-commit-hooks 4.4.0 passed. Hook copies remained byte-identical.
  • Both normal and namespace-package Mypy runs report the same 6 diagnostics in unchanged code as the base. No new type errors remain.

Other interpreters and operating systems were not run. The isolated virtualenv has local test-tool additions and read-only access to existing site packages.

AI assistance: OpenAI Codex investigated the implementation and history, prepared the patch and tests, and ran validation with independent automated review. No human code review is claimed.

Maintenance validation (2026-11-17)

Merged upstream master while retaining the original iterator-matcher fix. Only CHANGES needed conflict resolution; the new Python support entry and this fix are both retained. On Windows CPython 3.11.18, the full suite passes: 269 passed (18 deprecation warnings). Both mypy modes and all applicable pre-commit hooks pass. Other Python versions and operating systems were not run in this maintenance check.

Snapshot the documented matcher iterable when constructing a response so every attempt checks the same callbacks. Cover generator reuse after success and failure and repeated response registration.

Assisted-by: OpenAI Codex
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