Repository navigation
Closed
Description
import trio
class MyExceptionGroup(ExceptionGroup):
pass
async def main():
async with trio.open_nursery():
raise MyExceptionGroup("hello", [ValueError("bad")])
trio.run(main)output:
+ Exception Group Traceback (most recent call last):
| File "/home/graingert/projects/demo.py", line 11, in <module>
| trio.run(main)
| File "/home/graingert/.virtualenvs/anyio_fixture_methods/lib/python3.12/site-packages/trio/_core/_run.py", line 2306, in run
| raise runner.main_task_outcome.error
| File "/home/graingert/projects/demo.py", line 8, in main
| async with trio.open_nursery():
| File "/home/graingert/.virtualenvs/anyio_fixture_methods/lib/python3.12/site-packages/trio/_core/_run.py", line 959, in __aexit__
| raise combined_error_from_nursery
| ExceptionGroup: Exceptions from Trio nursery (1 sub-exception)
+-+---------------- 1 ----------------
| Exception Group Traceback (most recent call last):
| File "/home/graingert/projects/demo.py", line 9, in main
| raise MyExceptionGroup("hello", [ValueError("bad")])
| ExceptionGroup: hello (1 sub-exception)
+-+---------------- 1 ----------------
| ValueError: bad
+------------------------------------expected output:
+ Exception Group Traceback (most recent call last):
| File "/home/graingert/projects/demo.py", line 11, in <module>
| trio.run(main)
| File "/home/graingert/.virtualenvs/anyio_fixture_methods/lib/python3.12/site-packages/trio/_core/_run.py", line 2306, in run
| raise runner.main_task_outcome.error
| File "/home/graingert/projects/demo.py", line 8, in main
| async with trio.open_nursery():
| File "/home/graingert/.virtualenvs/anyio_fixture_methods/lib/python3.12/site-packages/trio/_core/_run.py", line 959, in __aexit__
| raise combined_error_from_nursery
| ExceptionGroup: Exceptions from Trio nursery (1 sub-exception)
+-+---------------- 1 ----------------
| Exception Group Traceback (most recent call last):
| File "/home/graingert/projects/demo.py", line 9, in main
| raise MyExceptionGroup("hello", [ValueError("bad")])
| MyExceptionGroup: hello (1 sub-exception)
+-+---------------- 1 ----------------
| ValueError: bad
+------------------------------------Metadata
Metadata
Assignees
Labels
No labels
Activity
graingert commentedon Dec 29, 2024
The problem is that splitting Cancelled from an ExceptionGroup tree converts custom EG subclasses into regular EGs:
trio/src/trio/_core/_run.py
Lines 641 to 648 in 3de7996
A5rocks commentedon Dec 29, 2024
Do we need to provide our own
.split?Also, is this something we should report upstream to CPython?
I think I get the logic for why this doesn't return subtypes though:
This way Python can preserve that ExceptionGroups are the norm unless someone passes in a not-Exception. I don't think this property is worth making every subclass make their own
.splitmethod though? IMO the left part of the tuple could be an ExceptionGroup/BaseExceptionGroup and the right part could be the original type -- though maybe with special casing for BaseExceptionGroup -> ExceptionGroup if possible.graingert commentedon Dec 29, 2024
I think the fix is using a custom split that doesn't recreate ExceptionGroups if the children aren't changing.
Probably it is worth reporting
TeamSpen210 commentedon Dec 29, 2024
For custom subclasses, you need to define derive(), so it knows how to create instances - it's not safe obviously to assume the constructor args are the same.
A5rocks commentedon Dec 29, 2024
Should we warn about that and provide a sane alternative?
jakkdl commentedon Dec 29, 2024
defining an
ExceptionGroupsubclass without aderivecould maybe be a linter warning? (either flake8-async or flake8-bugbear)I don't think trio should create a custom
splithereZac-HD commentedon Mar 27, 2025
Closing this issue because the solution is to define
MyExceptionGroup.derive(), and we've also opened an issue forflake8-asyncto warn if you forgot to do so.