PEP-654: add number of sub-exceptions to BaseException.__str__ (GH-2356)

This commit is contained in:
Irit Katriel 2022-02-22 18:30:27 +00:00 committed by GitHub
parent d7224f2a2d
commit 4e06e44f8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 27 deletions

View File

@ -186,30 +186,31 @@ contains only those exceptions for which the condition is true:
... ) ... )
... ] ... ]
... ) ... )
>>> import traceback
>>> traceback.print_exception(eg) >>> traceback.print_exception(eg)
| ExceptionGroup: one | ExceptionGroup: one (3 sub-exceptions)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| TypeError: 1 | TypeError: 1
+---------------- 2 ---------------- +---------------- 2 ----------------
| ExceptionGroup: two | ExceptionGroup: two (2 sub-exceptions)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| TypeError: 2 | TypeError: 2
+---------------- 2 ---------------- +---------------- 2 ----------------
| ValueError: 3 | ValueError: 3
+------------------------------------ +------------------------------------
+---------------- 3 ---------------- +---------------- 3 ----------------
| ExceptionGroup: three | ExceptionGroup: three (1 sub-exception)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| OSError: 4 | OSError: 4
+------------------------------------ +------------------------------------
>>> type_errors = eg.subgroup(lambda e: isinstance(e, TypeError)) >>> type_errors = eg.subgroup(lambda e: isinstance(e, TypeError))
>>> traceback.print_exception(type_errors) >>> traceback.print_exception(type_errors)
| ExceptionGroup: one | ExceptionGroup: one (2 sub-exceptions)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| TypeError: 1 | TypeError: 1
+---------------- 2 ---------------- +---------------- 2 ----------------
| ExceptionGroup: two | ExceptionGroup: two (1 sub-exception)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| TypeError: 2 | TypeError: 2
+------------------------------------ +------------------------------------
@ -240,23 +241,23 @@ If both the subgroup and its complement are needed, the
>>> type_errors, other_errors = eg.split(lambda e: isinstance(e, TypeError)) >>> type_errors, other_errors = eg.split(lambda e: isinstance(e, TypeError))
>>> traceback.print_exception(type_errors) >>> traceback.print_exception(type_errors)
| ExceptionGroup: one | ExceptionGroup: one (2 sub-exceptions)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| TypeError: 1 | TypeError: 1
+---------------- 2 ---------------- +---------------- 2 ----------------
| ExceptionGroup: two | ExceptionGroup: two (1 sub-exception)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| TypeError: 2 | TypeError: 2
+------------------------------------ +------------------------------------
>>> traceback.print_exception(other_errors) >>> traceback.print_exception(other_errors)
| ExceptionGroup: one | ExceptionGroup: one (2 sub-exceptions)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| ExceptionGroup: two | ExceptionGroup: two (1 sub-exception)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| ValueError: 3 | ValueError: 3
+------------------------------------ +------------------------------------
+---------------- 2 ---------------- +---------------- 2 ----------------
| ExceptionGroup: three | ExceptionGroup: three (1 sub-exception)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| OSError: 4 | OSError: 4
+------------------------------------ +------------------------------------
@ -387,7 +388,7 @@ in the following example:
>>> raise ExceptionGroup("two", [f(2), eg]) >>> raise ExceptionGroup("two", [f(2), eg])
+ Exception Group Traceback (most recent call last): + Exception Group Traceback (most recent call last):
| File "<stdin>", line 1, in <module> | File "<stdin>", line 1, in <module>
| ExceptionGroup: two | ExceptionGroup: two (2 sub-exceptions)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| Traceback (most recent call last): | Traceback (most recent call last):
| File "<stdin>", line 3, in f | File "<stdin>", line 3, in f
@ -395,7 +396,7 @@ in the following example:
+---------------- 2 ---------------- +---------------- 2 ----------------
| Exception Group Traceback (most recent call last): | Exception Group Traceback (most recent call last):
| File "<stdin>", line 2, in <module> | File "<stdin>", line 2, in <module>
| ExceptionGroup: one | ExceptionGroup: one (1 sub-exception)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| Traceback (most recent call last): | Traceback (most recent call last):
| File "<stdin>", line 3, in f | File "<stdin>", line 3, in f
@ -802,29 +803,29 @@ merged with the unhandled ``TypeErrors``.
... ...
*ValueError: ExceptionGroup('eg', [ValueError(1), ExceptionGroup('nested', [ValueError(6)])]) *ValueError: ExceptionGroup('eg', [ValueError(1), ExceptionGroup('nested', [ValueError(6)])])
*OSError: ExceptionGroup('eg', [OSError(3), ExceptionGroup('nested', [OSError(4)])]) *OSError: ExceptionGroup('eg', [OSError(3), ExceptionGroup('nested', [OSError(4)])])
| ExceptionGroup | ExceptionGroup: (2 sub-exceptions)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| Exception Group Traceback (most recent call last): | Exception Group Traceback (most recent call last):
| File "<stdin>", line 15, in <module> | File "<stdin>", line 15, in <module>
| File "<stdin>", line 2, in <module> | File "<stdin>", line 2, in <module>
| ExceptionGroup: eg | ExceptionGroup: eg (2 sub-exceptions)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| ValueError: 1 | ValueError: 1
+---------------- 2 ---------------- +---------------- 2 ----------------
| ExceptionGroup: nested | ExceptionGroup: nested (1 sub-exception)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| ValueError: 6 | ValueError: 6
+------------------------------------ +------------------------------------
+---------------- 2 ---------------- +---------------- 2 ----------------
| Exception Group Traceback (most recent call last): | Exception Group Traceback (most recent call last):
| File "<stdin>", line 2, in <module> | File "<stdin>", line 2, in <module>
| ExceptionGroup: eg | ExceptionGroup: eg (3 sub-exceptions)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| TypeError: 2 | TypeError: 2
+---------------- 2 ---------------- +---------------- 2 ----------------
| OSError: 3 | OSError: 3
+---------------- 3 ---------------- +---------------- 3 ----------------
| ExceptionGroup: nested | ExceptionGroup: nested (2 sub-exceptions)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| OSError: 4 | OSError: 4
+---------------- 2 ---------------- +---------------- 2 ----------------
@ -848,11 +849,11 @@ it into the new ``ExceptionGroup``.
... except* ValueError: ... except* ValueError:
... raise ExceptionGroup("two", [KeyError('x'), KeyError('y')]) ... raise ExceptionGroup("two", [KeyError('x'), KeyError('y')])
... ...
| ExceptionGroup | ExceptionGroup: (2 sub-exceptions)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| Exception Group Traceback (most recent call last): | Exception Group Traceback (most recent call last):
| File "<stdin>", line 2, in <module> | File "<stdin>", line 2, in <module>
| ExceptionGroup: one | ExceptionGroup: one (1 sub-exception)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| ValueError: a | ValueError: a
+------------------------------------ +------------------------------------
@ -861,7 +862,7 @@ it into the new ``ExceptionGroup``.
| |
| Exception Group Traceback (most recent call last): | Exception Group Traceback (most recent call last):
| File "<stdin>", line 4, in <module> | File "<stdin>", line 4, in <module>
| ExceptionGroup: two | ExceptionGroup: two (2 sub-exceptions)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| KeyError: 'x' | KeyError: 'x'
+---------------- 2 ---------------- +---------------- 2 ----------------
@ -870,7 +871,7 @@ it into the new ``ExceptionGroup``.
+---------------- 2 ---------------- +---------------- 2 ----------------
| Exception Group Traceback (most recent call last): | Exception Group Traceback (most recent call last):
| File "<stdin>", line 2, in <module> | File "<stdin>", line 2, in <module>
| ExceptionGroup: one | ExceptionGroup: one (1 sub-exception)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| TypeError: b | TypeError: b
+------------------------------------ +------------------------------------
@ -891,7 +892,7 @@ chaining:
... except* TypeError as e: ... except* TypeError as e:
... raise ValueError('bad value') from e ... raise ValueError('bad value') from e
... ...
| ExceptionGroup | ExceptionGroup: (1 sub-exception)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| Traceback (most recent call last): | Traceback (most recent call last):
| File "<stdin>", line 2, in <module> | File "<stdin>", line 2, in <module>
@ -937,11 +938,11 @@ direct child of the new exception group created for that:
... except* ValueError: ... except* ValueError:
... raise KeyError('x') ... raise KeyError('x')
... ...
| ExceptionGroup | ExceptionGroup: (1 sub-exception)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| Exception Group Traceback (most recent call last): | Exception Group Traceback (most recent call last):
| File "<stdin>", line 2, in <module> | File "<stdin>", line 2, in <module>
| ExceptionGroup: eg | ExceptionGroup: eg (1 sub-exception)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| ValueError: a | ValueError: a
+------------------------------------ +------------------------------------
@ -958,11 +959,11 @@ direct child of the new exception group created for that:
... except* ValueError: ... except* ValueError:
... raise KeyError('x') ... raise KeyError('x')
... ...
| ExceptionGroup | ExceptionGroup: (2 sub-exceptions)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| Exception Group Traceback (most recent call last): | Exception Group Traceback (most recent call last):
| File "<stdin>", line 2, in <module> | File "<stdin>", line 2, in <module>
| ExceptionGroup: eg | ExceptionGroup: eg (1 sub-exception)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| ValueError: a | ValueError: a
+------------------------------------ +------------------------------------
@ -975,7 +976,7 @@ direct child of the new exception group created for that:
+---------------- 2 ---------------- +---------------- 2 ----------------
| Exception Group Traceback (most recent call last): | Exception Group Traceback (most recent call last):
| File "<stdin>", line 2, in <module> | File "<stdin>", line 2, in <module>
| ExceptionGroup: eg | ExceptionGroup: eg (1 sub-exception)
+-+---------------- 1 ---------------- +-+---------------- 1 ----------------
| TypeError: b | TypeError: b
+------------------------------------ +------------------------------------