Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Python styleguide ExceptionGroup update for docstrings #907

Copy link
Copy link
Open
@gtkacz

Description

@gtkacz
Issue body actions

PEP 654 introduced exception groups: "a combination of multiple unrelated exceptions". This changes how exceptions are handled by the user:

def some_method():
    ex1 = ValueError("value_error")
    ex2 = TypeError("type_error")

    raise ExceptionGroup("exception_group", [ex1, ex2])

try:
    some_method()
except (ValueError, TypeError):
    print("This will not run")
except Exception:
    print("This will run")

try:
    some_method()
except ExceptionGroup:
    print("This will run")

try:
    some_method()
except* (ValueError, TypeError):
    print("This will run")

Making it important to warn users that they should either catch ExceptionGroup or use except*.

The relevant styleguide section doesn't yet mention exception groups. What would be the correct way to document this?

The docstring below may lead users to not catch ExceptionGroup or use except*, causing their code to break.

def some_method():
    """
    My method.

    Raises:
        ValueError: Some value error.
        TypeError: Some type error.
    """

The docstring below doesn't make sense for two main reasons:

  1. The PEP itself describes an exception group as "a combination of multiple unrelated exceptions". How would one describe an exception group that doesn't throw anything in particular?
  2. The user may want to catch different exception types raised by the exception groups to handle them separately. To do so they would need to open the method itself, defeating the purpose of documenting the exception type.
def some_method():
    """
    My method.

    Raises:
        ExceptionGroup: ???
    """

I think we may need entirely new guidelines for exception groups. Maybe something like:

def some_method():
    """
    My method.

    Raises:
        ExceptionGroup:
            ValueError: Some value error.
            TypeError: Some type error.
    """

or

def some_method():
    """
    My method.

    Raises:
        *ValueError: Some value error.
        *TypeError: Some type error.
    """

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.