GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||
GeezerVison is showing github.com — Support non-sequence iterables in the `ExceptionGroup` API, or document the design decision · Issue #129867 · python/cpython Closed as not planned Closed as not planned Copy link Labels interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement DescriptionFeature or enhancementProposal:Currently, the
or
Currently, this Python code would work: ExceptionGroup("list", [ValueError(), AttributeError()]) # OK ExceptionGroup("tuple", (ValueError(), AttributeError())) # OK class Niche: def __getitem__(self, i): if i == 0: return ValueError() if i == 1: return AttributeError() raise StopIteration ExceptionGroup("niche", Niche()) # OK Note AFAIK, assert ExceptionGroup("", excs := [ValueError()]).exceptions is not excs # OK assert ExceptionGroup("", excs := (ValueError(),)).exceptions is excs # OK assert ExceptionGroup("", excs := type("", (tuple,), {})([ValueError(),])).exceptions is not excs # OK This is a minimal example of a use case I would desire, which is not currently supported: def cb1(): raise ValueError def cb2(): raise AttributeError errors = {} callbacks = {"module1": cb1, "module2": cb2} for mod, cb in callbacks.items(): try: cb() except Exception as exc: errors[mod] = exc if errors: raise ExceptionGroup(f"got errors in {', '.join(errors)}", errors.values()) 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 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 diff --git a/Objects/exceptions.c b/Objects/exceptions.c index 154cde93168..f97eb33953f 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -871,7 +871,8 @@ BaseExceptionGroup_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return NULL; } - if (!PySequence_Check(exceptions)) { + PyTypeObject *t = Py_TYPE(exceptions); + if (t->tp_iter == NULL) { PyErr_SetString( PyExc_TypeError, "second argument (exceptions) must be a sequence"); only causes 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 Reacted by j-g00da and trag1c Activity
MetadataMetadataAssigneesLabelsinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement ProjectsMilestoneRelationshipsDevelopmentIssue actions | ||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |