Skip to content

Fix duplicated exception chain output for exceptions without a traceback - #14781

Open
SemTiOne wants to merge 2 commits into
pytest-dev:mainfrom
SemTiOne:fix-8321-chained-exception-dup
Open

Fix duplicated exception chain output for exceptions without a traceback#14781
SemTiOne wants to merge 2 commits into
pytest-dev:mainfrom
SemTiOne:fix-8321-chained-exception-dup

Conversation

@SemTiOne

@SemTiOne SemTiOne commented Jul 25, 2026

Copy link
Copy Markdown

When an exception in a chain has no __traceback__ of its own, repr_excinfo() falls back to traceback.format_exception(), which defaults to chain=True and walks the rest of the chain itself, duplicating what pytest's own chain-walking loop then prints again right after.

Fix: pass chain=False to that fallback call.

  • Include documentation when adding new features.
  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits. Please uncheck this if you prefer to squash the commits yourself.
  • Closes Exceptions without a traceback print multiple times if chained #8321
  • If AI agents were used, they are credited in Co-authored-by commit trailers.
  • Create a new changelog file in the changelog directory, with a name like <ISSUE NUMBER>.<TYPE>.rst. See changelog/README.rst for details.
  • Add yourself to AUTHORS in alphabetical order.

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jul 25, 2026
@SemTiOne
SemTiOne force-pushed the fix-8321-chained-exception-dup branch 2 times, most recently from 93a91a0 to be30f69 Compare August 2, 2026 10:15
Co-authored-by: Claude <noreply@anthropic.com>
@SemTiOne
SemTiOne force-pushed the fix-8321-chained-exception-dup branch from be30f69 to 79df156 Compare August 2, 2026 10:30
@SemTiOne

SemTiOne commented Aug 2, 2026

Copy link
Copy Markdown
Author

Hello @RonnyPfannschmidt, I would like you to take a look at this PR when you have a moment please. Let me know if you have any feedback. Thank you for your time 🙏

Comment thread src/_pytest/_code/code.py
# Fallback to native repr if the exception doesn't have a traceback:
# ExceptionInfo objects require a full traceback to work.
reprtraceback = ReprTracebackNative(format_exception(type(e), e, None))
reprtraceback = ReprTracebackNative(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

am i understanding it correct that this bandaids our lack of proper walking with disabling chaining to prevent running out of it

i think this works as a initial fix but needs a followup with more substantial work in excinfo wrt handling more detailed traces

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I agree this is a bandaid.

I wonder if we should make the traceback optional in ExceptionInfo? Or, perhaps we add a lightweight chain member representation? With repr_excinfo as the one and only chain walker, no native format_exception fallback is needed for traceback-less members. The ExceptionGroup fallback stays native for now and we no longer need the chain=False workaround. What do you think @RonnyPfannschmidt?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exceptions without a traceback print multiple times if chained

2 participants