Title: ExceptionGroup should be generic · Issue #91549 · python/cpython · GitHub
Open Graph Title: ExceptionGroup should be generic · Issue #91549 · python/cpython
X Title: ExceptionGroup should be generic · Issue #91549 · python/cpython
Description: Thanks to PEP 654, 3.11 will have the new except* syntax. Let's take this sample: try: do_failing_things() except* ValueError as eg: reveal_type(eg) In the except* block, eg is an ExceptionGroup instance that in its exceptions attribute ...
Open Graph Description: Thanks to PEP 654, 3.11 will have the new except* syntax. Let's take this sample: try: do_failing_things() except* ValueError as eg: reveal_type(eg) In the except* block, eg is an ExceptionGroup in...
X Description: Thanks to PEP 654, 3.11 will have the new except* syntax. Let's take this sample: try: do_failing_things() except* ValueError as eg: reveal_type(eg) In the except* block, eg is an ExceptionGrou...
Opengraph URL: https://github.com/python/cpython/issues/91549
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ExceptionGroup should be generic","articleBody":"Thanks to PEP 654, 3.11 will have the new `except*` syntax. Let's take this sample:\r\n\r\n```python\r\ntry:\r\n do_failing_things()\r\nexcept* ValueError as eg:\r\n reveal_type(eg)\r\n```\r\n\r\nIn the `except*` block, `eg` is an `ExceptionGroup` instance that in its `exceptions` attribute contains either `ValueError` instances or more nested `ExceptionGroup`s containing `ValueError`s. I expect that many `except*` blocks will do operations specific to the exception type they operate on, but right now there's no way to represent that information in the type system.\r\n\r\nTo fix this, we can make `ExceptionGroup` (and `BaseExceptionGroup`) generic. In the example, `eg` would be of type `ExceptionGroup[ValueError]` (to static type checkers), indicating that all the contained exceptions are `ValueError`s.\r\n\r\nRecommendations:\r\n- In CPython, add the C equivalent of `__class_getitem__ = GenericAlias` to `ExceptionGroup` and `BaseExceptionGroup`.\r\n- In typeshed, make `ExceptionGroup` (and base) generic over a `TypeVar(\"ExceptionT\", bound=Exception)`, and type its `exceptions` attribute as `tuple[ExceptionT | ExceptionGroup[ExceptionT], ...]`.\r\n- In type checkers, for code of the form `except* T as eg:`, infer `eg` to be of type `ExceptionGroup[T]`. (Or `BaseExceptionGroup[T]` if `T` does not inherit from `Exception`.)\r\n\r\nIf we're in agreement, I can implement the first two recommendations.\r\n\r\nLinks:\r\n- https://peps.python.org/pep-0654/#except (spec)\r\n- https://github.com/python/typeshed/blob/7f9be7fd00ff86750a3d983681a9ef94b5511e2c/stdlib/builtins.pyi#L1752 (current typeshed stubs)\r\n\r\ncc @iritkatriel @gvanrossum for PEP 654, @srittau @AlexWaygood for typeshed, @erictraut as a type checker maintainer.","author":{"url":"https://github.com/JelleZijlstra","@type":"Person","name":"JelleZijlstra"},"datePublished":"2022-04-14T22:32:35Z","url":"https://github.com/python/cpython/issues/91549"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e56555a8-cac5-f05e-7a4e-e2f47360deb0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B3EE:4D1C6:25F5E9B:33B6D5F:6B003E05 |
| html-safe-nonce | c724b3013921b1bd041fa903835aaf641cda0cb6b139d375511e4c1156382d56 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCM0VFOjREMUM2OjI1RjVFOUI6MzNCNkQ1Rjo2QjAwM0UwNSIsInZpc2l0b3JfaWQiOiIzMDYyMTI5NTY4NjM5ODkyOTk3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 20f9b0aa042d86be0788d8891e63a5e1fded4fadd5e939f410ca1e08b90fd73d |
| hovercard-subject-tag | issue:1205069889 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/91549/issue_layout |
| twitter:image | https://opengraph.githubassets.com/78297e14c7f20b93e22a4b03b8668a35a4300496b7f1fb669b354e043b3d64f9/python/cpython/issues/91549 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/78297e14c7f20b93e22a4b03b8668a35a4300496b7f1fb669b354e043b3d64f9/python/cpython/issues/91549 |
| og:image:alt | Thanks to PEP 654, 3.11 will have the new except* syntax. Let's take this sample: try: do_failing_things() except* ValueError as eg: reveal_type(eg) In the except* block, eg is an ExceptionGroup in... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JelleZijlstra |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width