Question presented to candidate:
"Forget asyncio for a moment. Suppose you validate a form or process a batch, and you want to report every failure at once instead of stopping at the first one. How do you raise an ExceptionGroup yourself, and how do you take one apart with except*, split() and subgroup()?"
What a strong answer should cover:
- Raising: collect exceptions in a list and
raise ExceptionGroup("message", errors); the list must be a non-empty sequence of exception instances;ExceptionGrouponly acceptsExceptionsubclasses,BaseExceptionGrouptakes any and picks the right class (Python 3.11+, PEP 654). - Catching:
except* Thandles the matching part of a group (every leaf, nesting kept); severalexcept*clauses can each run once; unhandled parts and errors raised in handlers are re-raised as a group; a naked exception is wrapped. - Splitting:
eg.split(cond)returns(match, rest),eg.subgroup(cond)returns only the match;condis a type, a tuple of types or a predicate (which also sees the groups themselves); no match givesNone; nesting,__traceback__,__cause__,__context__and__notes__are kept. - Subclasses: override
derive()sosplit/subgrouprebuild your class with its extra fields; otherwise they produce plainExceptionGroups. - Rules:
exceptandexcept*cannot mix in onetry;except* ExceptionGroupis aTypeError; nobreak/continue/returnin anexcept*block.
Code / implementation expected: Yes β raise a group of validation errors, catch it with except*, and show split() on a nested group.