-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
bpo-46729: improved str() for BaseExceptionGroup #31294
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
Conversation
@1st1 See #31270 (comment) . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels weird to report the number of nodes in the tree without any indication of the tree structure. I would be okay if the number just was the count of the immediate subexceptions (i.e., len(eg.exceptions)
). If you want to show the number of leaves: (a) don't count interior nodes!; (b) add something to the message indicating that the tree isn't flat (if it isn't). Maybe make the message "N sub-exceptions" if N == len(eg.exceptions)
but "N sub-exceptions in tree of depth D" if there are sub-sub (etc.) exceptions?
I'd go with just the number of directly contained exceptions, because a nested group is really just one error from some group operation. But I wonder if any count is really useful information here. We said iteration is irrelevant because it's not about how many but about what types of exceptions are included. Maybe we should drop the number and indicate in some other way that this is a group of exceptions? |
Yury’s MultiError had the set of exception names in the message (as well as the count). Maybe that’s an idea? |
That could be for the entire tree (leaves only), OR only the first level (so it might show EG in there). If too many, use ‘…’ . |
Yury had the whole traceback in str(), right? The str() appears in the standard traceback, so the types would be repetitive. Also we would want to dedup them, so we would need to construct a set of the types. We don’t want to allocate memory while rendering an exception, so we will need to do this in the constructor and save the set. |
Yeah, but the first line has this appended the the message argument: "N sub errors: (TYPES)" where N is len(errors) and TYPES is the set of error types -- both only using the immediate child level.
But the traceback is pretty verbose -- the first line of the message contains a useful summary. (Admittedly mostly used by tests.)
Or the string -- perhaps more work but a simpler type. Then again maybe we should just stick with what we have. |
On second thought I don’t think it’s a problem to put the number as a kind of summary and indication that it’s a group. The types appear in the repr() so maybe that’s enough. In the original implementation str() just printed the msg and then in the traceback we added a line under the msg like “with x sub-exceptions”. We removed that later because we thought it was clutter. It makes more sense to put it on the exception str() for when that’s used in other contexts, and also it doesn’t add a line to the traceback. @1st1 do you have a view? |
If the str() would just be |
I made a draft PR for the PEP update: python/peps#2356 Should I now ask the SC to approve it? |
Draft PR to discuss options for a better Exception Group str().
Initial suggestion is to add "(group of X exceptions)", but this seems a bit too long.
https://bugs.python.org/issue46729