A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/python/cpython/issues/125825 below:

Add `.subgroup()` and `.split()` methods to `BaseException` · Issue #125825 · python/cpython · GitHub

As we scale up our use of except* and ExceptionGroups, we've noticed that isinstance() checks get noticeably more clumsy:

# often the isinstance is implicit due to an except-statement
not_found = isinstance(err, HTTPError) and err.status_code == 404

# becomes

if isinstance(err, BaseExceptionGroup):
    # often we'd want .split() to do something with the other case, but for simplicity:
    not_found = err.subgroup(lambda e: isinstance(e, HTTPError) and e.status_code == 404) is not None
else:
    # as above

Obviously we use except T as err: or except* T ... wherever possible, but there are cases where we need to inspect metadata beyond the error type and duplicating the logic often gets messy.

By adding .subgroup() and .split() methods to BaseException, which for non-groups act as if the exception was wrapped in a single-member group, we can avoid duplication and write only the necessarily-more-complicated code for groups to cover both cases.

(opening issue as discussed with @gpshead and @njsmith elsewhere)

Linked PRs

gpshead, zware and nineteendo


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4