fix: respect enabled=False in iterator protocols (__iter__ / __aiter__)#643
Merged
Conversation
When Retrying(enabled=False) was used with the context-manager iterator pattern (`for attempt in retrying` / `async for attempt in retrying`), the `enabled` flag was silently ignored and the full retry machinery ran (multiple attempts, stop/wait evaluated) instead of executing the body exactly once and propagating any exception directly. The `wraps()`-based path already handled `enabled=False` correctly, so this patch brings the iterator API in line with it: - BaseRetrying.__iter__: if not enabled, yield one AttemptManager and re-raise the stored exception if the attempt failed. - AsyncRetrying.__aiter__ / __anext__: same semantics via a one-shot flag that stops after the first yielded AttemptManager. Four regression tests are added (two sync, two async) covering the failure and success cases for each protocol.
jd
approved these changes
Jun 25, 2026
Contributor
|
Queued — the merge queue status continues in this comment ↓. |
Contributor
Merge Queue Status
This pull request spent 8 seconds in the queue, including 1 second running CI. Required conditions to merge |
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
When
Retrying(enabled=False)is used with the context-manager iterator pattern —for attempt in retrying:orasync for attempt in retrying:— theenabledflag is silently ignored and the full retry machinery runs (multiple attempts, stop/wait evaluated). This is inconsistent with thewraps()-based path, which already handlesenabled=Falsecorrectly by running the function exactly once and propagating any exception directly.Reproducer:
Fix
BaseRetrying.__iter__: whennot self.enabled, yield oneAttemptManager, then re-raise the stored exception if the attempt failed — mirroringwraps().AsyncRetrying.__aiter__/__anext__: same semantics via a one-shot done-flag.Four regression tests are added (sync failure, sync success, async failure, async success).
This pull request was prepared with the assistance of AI, under my direction and review.