X Tutup
The Wayback Machine - https://web.archive.org/web/20220417185314/https://github.com/python/cpython/issues/89770
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

PEP 678: Make it possible to enrich an exception's error message #89770

Open
iritkatriel opened this issue Oct 25, 2021 · 6 comments
Open

PEP 678: Make it possible to enrich an exception's error message #89770

iritkatriel opened this issue Oct 25, 2021 · 6 comments
Labels
3.11 interpreter-core type-feature

Comments

@iritkatriel
Copy link
Member

@iritkatriel iritkatriel commented Oct 25, 2021

BPO 45607
Nosy @gvanrossum, @aroberge, @Zac-HD, @iritkatriel
PRs
  • #29880
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-12-03.22:02:13.816>
    created_at = <Date 2021-10-25.20:38:01.298>
    labels = ['interpreter-core', 'type-feature', '3.11']
    title = "Make it possible to enrich an exception's error message"
    updated_at = <Date 2021-12-03.22:02:13.816>
    user = 'https://github.com/iritkatriel'

    bugs.python.org fields:

    activity = <Date 2021-12-03.22:02:13.816>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-12-03.22:02:13.816>
    closer = 'iritkatriel'
    components = ['Interpreter Core']
    creation = <Date 2021-10-25.20:38:01.298>
    creator = 'iritkatriel'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45607
    keywords = ['patch']
    message_count = 4.0
    messages = ['404999', '405016', '407365', '407607']
    nosy_count = 4.0
    nosy_names = ['gvanrossum', 'aroberge', 'Zac Hatfield-Dodds', 'iritkatriel']
    pr_nums = ['29880']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue45607'
    versions = ['Python 3.11']

    @iritkatriel
    Copy link
    Member Author

    @iritkatriel iritkatriel commented Oct 25, 2021

    The requirement comes from Hypothesis, see
    #28569 (comment)

    It is necessary there to add a note to an exception describing which test case it comes from. The note should be printed by __str__ of this exception.

    class Explanation(Exception):
        __module__ = "builtins"
        def __str__(self) -> str:
            return f"\n{self.args[0]}"

    try:
    why = "Failed!"
    raise AssertionError(why)
    except Exception as e:
    msg = " You can reproduce this error by ...\n ..."
    raise Explanation(msg) from e

        # Ideally something more like:
        e.__note__ = msg
        raise

    @iritkatriel iritkatriel added interpreter-core type-feature labels Oct 25, 2021
    @Zac-HD
    Copy link
    Mannequin

    @Zac-HD Zac-HD mannequin commented Oct 26, 2021

    This code shows my current best workaround based on a wrapper exception, with the traceback below annotating the additional details that I'd prefer to omit for clarity:

    $ python example.py
    Traceback (most recent call last):
      File "example.py", line 8, in <module>
        raise AssertionError(why)
    AssertionError: Failed!
                                                                            # These lines are
    The above exception was the direct cause of the following exception:    # confusing for new 
                                                                            # users, and they
    Traceback (most recent call last):                                      # only exist due 
      File "example.py", line 10, in <module>                               # to implementation
        raise Explanation(msg) from e                                       # via the Explanation
    Explanation:                                                            # wrapper type :-(
        You can reproduce this error by ...
        ...

    The motivation for this is that we'd like to use ExceptionGroup to indicate that MultipleFailures is a group of exceptions, and replace our current print()-based method of reporting the details of the inner exceptions.

    @iritkatriel
    Copy link
    Member Author

    @iritkatriel iritkatriel commented Nov 30, 2021

    bpo-28953 is another use case for this feature.

    @iritkatriel
    Copy link
    Member Author

    @iritkatriel iritkatriel commented Dec 3, 2021

    New changeset 5bb7ef2 by Irit Katriel in branch 'main':
    bpo-45607: Make it possible to enrich exception displays via setting their __note__ field (GH-29880)
    5bb7ef2

    @iritkatriel iritkatriel closed this Dec 3, 2021
    @iritkatriel iritkatriel closed this Dec 3, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @iritkatriel iritkatriel reopened this Apr 12, 2022
    @iritkatriel
    Copy link
    Member Author

    @iritkatriel iritkatriel commented Apr 12, 2022

    Reopening to implement PEP 678.

    @iritkatriel iritkatriel changed the title Make it possible to enrich an exception's error message PEP 678: Make it possible to enrich an exception's error message Apr 13, 2022
    @iritkatriel
    Copy link
    Member Author

    @iritkatriel iritkatriel commented Apr 16, 2022

    The implementation is complete, I am leaving this open for one more documentation PR (but that should not block the release).

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 interpreter-core type-feature
    Projects
    Status: Release blocker
    Development

    No branches or pull requests

    1 participant
    X Tutup