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

ExceptionGroup wrapper does not appear to function with except* #735

Closed
dstufft opened this issue Oct 9, 2023 · 2 comments · Fixed by #736
Closed

ExceptionGroup wrapper does not appear to function with except* #735

dstufft opened this issue Oct 9, 2023 · 2 comments · Fixed by #736

Comments

@dstufft
Copy link
Member

dstufft commented Oct 9, 2023

It looks like even on Python 3.11.5, the ExceptionGroup shim is being used, see for instance:

❯ python3.11 -c "import packaging.metadata; print(packaging.metadata.ExceptionGroup.__doc__); print(packaging.metadata.ExceptionGroup); print(__builtins__.ExceptionGroup)"
A minimal implementation of :external:exc:`ExceptionGroup` from Python 3.11.

        If :external:exc:`ExceptionGroup` is already defined by Python itself,
        that version is used instead.

<class 'packaging.metadata.ExceptionGroup'>
<class 'ExceptionGroup'>

This is causing errors not to function correctly with except*.

@dstufft
Copy link
Member Author

dstufft commented Oct 9, 2023

Not going to lie, I don't really understand what's happening here.

If I edit packaging/metadata.py and drop a print(type(__builtins__)) it tells me that the type is a dict, but if I do python3.11 -c "print(type(__builtins__))" it tells me the type is a module.

If I change the shim to get activated using:

try:
    ExceptionGroup = ExceptionGroup
except NameError:

    class ExceptionGroup(Exception):  # type: ignore[no-redef]  # noqa: N818
        """A minimal implementation of :external:exc:`ExceptionGroup` from Python 3.11.

        If :external:exc:`ExceptionGroup` is already defined by Python itself,
        that version is used instead.
        """

        message: str
        exceptions: List[Exception]

        def __init__(self, message: str, exceptions: List[Exception]) -> None:
            self.message = message
            self.exceptions = exceptions

        def __repr__(self) -> str:
            return f"{self.__class__.__name__}({self.message!r}, {self.exceptions!r})"

Everything seems to work?

@pradyunsg
Copy link
Member

I think the following would be clearer (separating the name lookup vs reassignment as a global) and I'm onboard for moving to a try-except regardless of the specific form.

try:
   ExceptionGroup
except NameError:
   ...
else:
   ExceptionGroup = ExceptionGroup  # all the linter ignores

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants