-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Support non-sequence iterables in the ExceptionGroup
API, or document the design decision
#129867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Is this a theoretical question or is there a use case for non-sequence iterable? PEP 654 specifies sequence, so if there is a compelling use case I believe it would require a new PEP or an amendment of this PEP. |
Hi @iritkatriel, I included the use case in this issue. |
I think there can be more of them. Generally, IMO: This is an additive change compatible with the PEP (I see it would be necessary to amend it, yes). It's not breaking unless someone bases their workflow on try:
ExceptionGroup("", {ValueError()})
except TypeError:
main_logic() |
The reality is that we have a process that we need to follow when we make changes to builtins, and this includes specifying the change in a PEP and putting the proposal under quite a bit of scrutiny. Had you pointed to a discrepancy between the implementation and the PEP, we would have fixed it as a bug. Since the implementation matches the PEP in this case, this is a feature request involving a change to a builtin type, and the burden of motivating the change there. This feature would require a language spec change (impacting other python implementations too). One thing we would need to decide is whether the language spec should require that all iterables are supported, or merely allow it. I noticed the code snippet you posted, but it looked more like "here is some code that would work if you add the feature", rather than an actual problem that came up in real life programming. Sorry I jumped to that conclusion, but now I'm curious -is someone really building a dict of exceptions and then creating an exception group, and then ... doing what with the dict? |
Uh oh!
There was an error while loading. Please reload this page.
Feature or enhancement
Proposal:
Currently, the
ExceptionGroup
API only supports sequences of exceptions to its second parameterexcs
.This ticket suggests either:
or
Currently, this Python code would work:
Note
AFAIK,
ExceptionGroup
objects do not store the sequence passed to them, unless it's a tuple object (excluding tuple subclasses). They export a new tuple (created from the passed-in sequence) or passed-in tuple via.exceptions
.Quick illustration:
This is a minimal example of a use case I would desire, which is not currently supported:
Important
While this can be simply worked around by converting the values' view into a sequence, the question is more fundamental: why?
There are some other variants of this on GitHub, too (including my humble one).
It was an intentional decision to only support sequences, justifying the very first part of the constructor test:
cpython/Lib/test/test_exception_group.py
Lines 39 to 42 in 1bccd6c
Before writing this issue, I did a research in some places that seemed like they could explain why only sequences made a cut. Namely, I:
I didn't have the time to read them very in depth, so I might have overlooked something; I only found this thread relevant:
Which leads me to think that supporting
Iterable
was initially planned (the author meantIterable[Exception]
instead ofIterable[ExceptionGroup]
), but then implicitly ignored for the sake of predicted use cases only being with sequences (lists/tuples).Please note that the considerations about variadic constructor of
BaseExceptionGroup
are not relevant to this ticket.Before writing this, I also reached out to @ZeroIntensity and @Eclips4 to discuss the topic.
@Eclips4 confirmed that he saw no hard requirement for the exception sequence to be a sequence specifically, and applying this simple patch
only causes
test_bad_EG_construction__bad_excs_sequence
to fail.CC @iritkatriel @gvanrossum @1st1
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
The text was updated successfully, but these errors were encountered: