Skip to content

ExceptionGroup repr is misleading if original exception sequence is mutated #141732

Description

@gschaffner

Bug report

Bug description:

The exception listing in repr(exc_grp) diverges from exc_grp.exceptions when the initial sequence of exceptions passed to the constructor has been mutated.

Expected behavior: The exceptions listed in repr(exc_grp) match exc_grp.exceptions, which is immutable.

Reproducer:

exceptions = list[Exception]()
exceptions.append(ValueError())
exc_grp = ExceptionGroup("", exceptions)
print(f"{exc_grp.exceptions=!r}")
print(f"{exc_grp=!r}")
print()
exceptions.clear()
print(f"{exc_grp.exceptions=!r}")
print(f"{exc_grp=!r}")

Output:

exc_grp.exceptions=(ValueError(),)
exc_grp=ExceptionGroup('', [ValueError()])

exc_grp.exceptions=(ValueError(),)
exc_grp=ExceptionGroup('', [])

Expected output:

exc_grp.exceptions=(ValueError(),)
exc_grp=ExceptionGroup('', [ValueError()])

exc_grp.exceptions=(ValueError(),)
exc_grp=ExceptionGroup('', [ValueError()])

CPython versions tested on:

3.11, 3.12, 3.13, 3.14

Operating systems tested on:

Linux

Linked PRs

Activity

  1. gschaffner commented on Nov 19, 2025

    @gschaffner
    Author

    I noticed that PyPy behaves the way I expected here; they use self.exceptions in BaseExceptionGroup.__repr__ instead of self.args[1]:

    https://github.com/pypy/pypy/blob/release-pypy3.11-v7.3.20/pypy/module/exceptions/app_group.py#L92-L93

    On CPython, it looks like BaseExceptionGroup.__repr__ resolves to BaseException.__repr__, so the repr ends up using self.args[1] instead of self.exceptions.

    I skimmed the PEP and didn't see anything that indicated that either repr implementation is in violation of the PEP.

  2. dr-carlos commented on Nov 19, 2025

    @dr-carlos
    Contributor

    I skimmed the PEP and didn't see anything that indicated that either repr implementation is in violation of the PEP.

    The PEP does reference the repr() of ExceptionGroups but not explicitly specify anything:

    >>> try:
    ...     raise ExceptionGroup("problem", [BlockingIOError()])
    ... except* OSError as e:   # Would catch the error
    ...     print(repr(e))
    ... except* BlockingIOError: # Would never run
    ...     print('never')
    ...
    ExceptionGroup('problem', [BlockingIOError()])

    But I agree that mutating an argument which is only used to create an immutable tuple shouldn't change the repr.

  3. serhiy-storchaka commented on Nov 19, 2025

    @serhiy-storchaka
    Member

    This may be a part of more complex problem. See #137889.

  4. added a commit that references this issue on Dec 7, 2025
  5. added a commit that references this issue on Dec 7, 2025
  6. added a commit that references this issue on Dec 8, 2025
  7. hugovk commented on Feb 3, 2026

    @hugovk
    Member

    This has been merged in main and 3.13 but cannot be merged for 3.14 because of failing CI: #142388.

    Please can you check the backport?

  8. dr-carlos commented on Feb 3, 2026

    @dr-carlos
    Contributor

    This has been merged in main and 3.13 but cannot be merged for 3.14 because of failing CI: #142388.

    Please can you check the backport?

    Yes, will do!
    Opened #144445

  9. added a commit that references this issue on Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions