Repository navigation
fix: keep the exception chain when reraising a TryAgain cause - #764
Open
Enesuygurs wants to merge 1 commit into
Open
Enesuygurs wants to merge 1 commit into
Enesuygurs wants to merge 1 commit into
Conversation
RetryError.reraise() surfaced the exception behind a TryAgain with "raise ... from None", which overwrote that exception's own __cause__ and suppressed its __context__, so the traceback no longer showed what led to it. Raise it as is, like the non-TryAgain path already does.
Contributor
|
Tick the box to add this pull request to the merge queue (same as
|
This branch has not been deployed
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.
Problem
Since #640,
reraise=Truesurfaces the exception that triggered aTryAgaininstead of theTryAgainitself. It does this withraise cause... from None, which mutates that exception: its__cause__is reset toNoneand__suppress_context__is forced toTrue. The traceback then loses whatever led to the error.The same applies to an implicit context (
raise KeyErrorinsideexcept OSError): the "During handling of the above exception" section is suppressed. Retrying the same function withoutTryAgainkeeps the chain intact, because that path (raise self.last_attempt.result()) does not usefrom None.Fix
Drop
from NoneinRetryError.reraise()and raise the exception as is. Its own__cause__and__context__are left untouched, which matches the non-TryAgainpath. Behaviour is otherwise unchanged: the same exception type and instance are raised, and a bareTryAgainstill reraisesTryAgain. A reno release note is included.Note: #730 edits the line two above this one in the same hunk. Whichever of the two lands second will need a trivial rebase.
Tests
I added regression tests in
tests/test_tenacity.pyfor the explicit chain (raise ... from e) and the implicit context, and one intests/test_asyncio.pyfor the explicit chain withAsyncRetrying. They fail without the change and pass with it.pytest: 188 passed, 15 subtests passedruff check ./ruff format --check .: passmypy: no issues found in 19 source filessphinx-build -Wdoctest and html: pass