Fix __notes__ leaking across chained exceptions (#3960)#4088
Open
kanchanthapa wants to merge 1 commit intoTextualize:masterfrom
Open
Fix __notes__ leaking across chained exceptions (#3960)#4088kanchanthapa wants to merge 1 commit intoTextualize:masterfrom
kanchanthapa wants to merge 1 commit intoTextualize:masterfrom
Conversation
When rendering a chained exception, Rich previously read __notes__ once from the outermost exception and reused the same list reference on every Stack built inside the while-loop. This caused notes added to one exception to appear on every exception in the chain, which contradicts CPython's native traceback module behavior (notes are only rendered on the specific exception they were added to via PEP 678's add_note()). Move the __notes__ assignment inside the extraction loop so each exception contributes its own notes. Covers __cause__, __context__, and ExceptionGroup chains. Adds test_notes_isolated_per_exception_in_chain covering both __cause__ and __context__ chaining.
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.
When rendering a chained exception, Rich previously read notes once from the outermost exception and reused the same list reference on every Stack built inside the while-loop. This caused notes added to one exception to appear on every exception in the chain, which contradicts CPython's native traceback module behavior (notes are only rendered on the specific exception they were added to via PEP 678's add_note()).
Move the notes assignment inside the extraction loop so each exception contributes its own notes. Covers cause, context, and ExceptionGroup chains.
Adds test_notes_isolated_per_exception_in_chain covering both cause and context chaining.
Type of changes
AI?
AI generated PRs may be accepted, but only if @willmcgugan has responded on an issue or discussion.
Checklist
Description
Please describe your changes here.
Fixes #3960.
When displaying a chained exception whose
__notes__were only added tothe outermost exception, Rich rendered those notes under every exception
in the chain instead of only on the one they were added to. This
contradicts CPython's built-in traceback behavior.
Root cause
Traceback.extract()readsexc_value.__notes__into a local variableonce, before the
while Trueloop that walks__cause__/__context__.Every
Stackbuilt inside the loop then shares the same list reference,so every stack ends up with the outermost exception's notes.
Fix
Move the
notes = getattr(exc_value, "__notes__", None) or []assignmentinside the loop so each iteration reads from the current
exc_value.Test
Added
test_notes_isolated_per_exception_in_chaincovering both__cause__(explicitraise ... from ...) and__context__(implicitchaining) cases.
QA
pytest— 953 passed, 25 skippedmypy -p rich— no issuesblack --check rich/traceback.py tests/test_traceback.py— unchangedrich/traceback.pyunchanged at 88%AI disclosure
Per
AI_POLICY.md: drafted with the assistance of Claude (Anthropic). Thefix approach matches the root-cause analysis posted in issue #3960 by the
reporter; I have reviewed every line and run the full QA pipeline locally.
Before / after
Before (bug):
Important: Link to an issue or discussion regarding these changes.