Skip to content
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

Documentation for except* clause and ExceptionGroup can be improved . #98172

Closed
mrolle45 opened this issue Oct 11, 2022 · 4 comments
Closed

Documentation for except* clause and ExceptionGroup can be improved . #98172

mrolle45 opened this issue Oct 11, 2022 · 4 comments
Labels
docs Documentation in the Doc dir

Comments

@mrolle45
Copy link

except*

except * currently reads:

The except* clause(s) are used for handling ExceptionGroups. The exception type for matching is interpreted as in the case of except, but in the case of exception groups we can have partial matches when the type matches some of the exceptions in the group. This means that multiple except* clauses can execute, each handling part of the exception group. Each clause executes once and handles an exception group of all matching exceptions. Each exception in the group is handled by at most one except* clause, the first that matches it.

try:
raise ExceptionGroup("eg",
[ValueError(1), TypeError(2), OSError(3), OSError(4)])
except* TypeError as e:
print(f'caught {type(e)} with nested {e.exceptions}')
except* OSError as e:
print(f'caught {type(e)} with nested {e.exceptions}')

caught <class 'ExceptionGroup'> with nested (TypeError(2),)
caught <class 'ExceptionGroup'> with nested (OSError(3), OSError(4))

  • Exception Group Traceback (most recent call last):
    | File "", line 2, in
    | ExceptionGroup: eg
    +-+---------------- 1 ----------------
    | ValueError: 1
    +------------------------------------

The following text appears in the same box as the example code, so you want to remove whatever caused that to occur in the source for the doc.

Any remaining exceptions that were not handled by any :keyword:!except*
clause are re-raised at the end, combined into an exception group along with
all exceptions that were raised from within :keyword:!except* clauses.
An :keyword:!except* clause must have a matching type,
and this type cannot be a subclass of :exc:BaseExceptionGroup.
It is not possible to mix :keyword:except and :keyword:!except*
in the same :keyword:try.
:keyword:break, :keyword:continue and :keyword:return
cannot appear in an :keyword:!except* clause.

What is needed to be explained:

  • When the try body raises a single exception, not an ExceptionGroup, it will be handled by at most one handler (the first one that matches).
  • The except* clause handles an exception group of all matching exceptions which were not already matched in an earlier except* clause.
  • Note that ExceptionGroup is a subclass of BaseExceptionGroup, and so cannot be the type in an except* clause.

ExceptionGroup

ExceptionGroup extends both BaseExceptionGroup and Exception.

BaseException

  • BaseExceptionGroup
    • ExceptionGroup
  • ...
  • Exception
    • ExceptionGroup
  • ...
@mrolle45 mrolle45 added the docs Documentation in the Doc dir label Oct 11, 2022
@JelleZijlstra
Copy link
Member

I opened #98173 to fix the formatting issue.

@iritkatriel iritkatriel changed the title except* clause and ExceptionGroup need more work. Documentation for except* clause and ExceptionGroup can be improved . Oct 11, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 11, 2022
(cherry picked from commit 5ecf961)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
miss-islington added a commit that referenced this issue Oct 11, 2022
(cherry picked from commit 5ecf961)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@JelleZijlstra
Copy link
Member

I fixed the formatting issue, thanks for pointing that out!

Your points about the substance of the docs remain. Feel free to submit a PR with improvements.

carljm added a commit to carljm/cpython that referenced this issue Oct 14, 2022
* main: (38 commits)
  pythongh-98251: Allow venv to pass along PYTHON* variables to pip and ensurepip when they do not impact path resolution (pythonGH-98259)
  Bpo-41246: IOCP Proactor avoid callback code duplication (python#21399)
  bpo-46364: Use sockets for stdin of asyncio only on AIX (python#30596)
  pythongh-98178: syslog() is not thread-safe on macOS (python#98213)
  Mark all targets in `Doc/Makefile` as `PHONY` (pythonGH-98189)
  pythongh-97982: Factorize PyUnicode_Count() and unicode_count() code (python#98025)
  pythongh-96265: Formatting changes for faq/general (python#98129)
  tutorial: remove "with single quotes" (python#98204)
  pythongh-97669: Remove Tools/scripts/startuptime.py (python#98214)
  signalmodule.c uses _PyErr_WriteUnraisableMsg() (python#98217)
  pythongh-97669: Fix test_tools reference leak (python#98216)
  pythongh-97669: Create Tools/patchcheck/ directory (python#98186)
  pythongh-65046: Link to logging cookbook from asyncio docs (python#98207)
  Formatting fixes in contextlib docs (python#98111)
  pythongh-95276: Add callable entry to the glossary (python#95738)
  pythongh-96130: Rephrase use of "typecheck" verb for clarity (python#98144)
  Fix some incorrect indentation around the main switch (python#98177)
  pythongh-98172: Fix formatting in `except*` docs (python#98173)
  pythongh-97982: Remove asciilib_count() (python#98164)
  pythongh-95756: Free and NULL-out code caches when needed (pythonGH-98181)
  ...
carljm added a commit to carljm/cpython that referenced this issue Oct 14, 2022
* main: (37 commits)
  pythongh-98251: Allow venv to pass along PYTHON* variables to pip and ensurepip when they do not impact path resolution (pythonGH-98259)
  Bpo-41246: IOCP Proactor avoid callback code duplication (python#21399)
  bpo-46364: Use sockets for stdin of asyncio only on AIX (python#30596)
  pythongh-98178: syslog() is not thread-safe on macOS (python#98213)
  Mark all targets in `Doc/Makefile` as `PHONY` (pythonGH-98189)
  pythongh-97982: Factorize PyUnicode_Count() and unicode_count() code (python#98025)
  pythongh-96265: Formatting changes for faq/general (python#98129)
  tutorial: remove "with single quotes" (python#98204)
  pythongh-97669: Remove Tools/scripts/startuptime.py (python#98214)
  signalmodule.c uses _PyErr_WriteUnraisableMsg() (python#98217)
  pythongh-97669: Fix test_tools reference leak (python#98216)
  pythongh-97669: Create Tools/patchcheck/ directory (python#98186)
  pythongh-65046: Link to logging cookbook from asyncio docs (python#98207)
  Formatting fixes in contextlib docs (python#98111)
  pythongh-95276: Add callable entry to the glossary (python#95738)
  pythongh-96130: Rephrase use of "typecheck" verb for clarity (python#98144)
  Fix some incorrect indentation around the main switch (python#98177)
  pythongh-98172: Fix formatting in `except*` docs (python#98173)
  pythongh-97982: Remove asciilib_count() (python#98164)
  pythongh-95756: Free and NULL-out code caches when needed (pythonGH-98181)
  ...
@iritkatriel
Copy link
Member

For except*:

When the try body raises a single exception, not an ExceptionGroup, it will be handled by at most one handler (the first one that matches).

Agreed, I will add this.

The except* clause handles an exception group of all matching exceptions which were not already matched in an earlier except* clause.

This is already stated: "Each exception in the group is handled by at most one :keyword:!except* clause, the first that matches it."

Note that ExceptionGroup is a subclass of BaseExceptionGroup, and so cannot be the type in an except* clause.

I don't think this is needed here (this is a language reference, not a tutorial, and the exception hierarchy is documented).

ExceptionGroup extends both BaseExceptionGroup and Exception.

This is why BaseExceptionGroup is in square brackets after ExceptionGroup:

"ExceptionGroup [BaseExceptionGroup]"

This is a text diagram, not sure how else we could do it.

@iritkatriel
Copy link
Member

Thank you @mrolle45 .

miss-islington added a commit that referenced this issue Oct 21, 2022
(cherry picked from commit 8367ca1)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
pablogsal pushed a commit that referenced this issue Oct 22, 2022
(cherry picked from commit 5ecf961)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

3 participants