8000 TRY003 docs should include an example for `ExceptionGroup` as well · Issue #68 · guilatrova/tryceratops · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

TRY003 docs should include an example for ExceptionGroup as well #68

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

Open
lanzz opened this issue Aug 24, 2023 · 0 comments
Open

TRY003 docs should include an example for ExceptionGroup as well #68

lanzz opened this issue Aug 24, 2023 · 0 comments

Comments

@lanzz
Copy link
lanzz commented Aug 24, 2023

Disclaimer: I'm actually using ruff, which ports this violation from tryceratops, but looking at the source it looks like tryceratops will also emit a TRY003 violation when raising ExceptionGroup with a literal message.

The example given for Exception will not work with ExceptionGroup, because ExceptionGroup.__new__ still expects the message argument even if __init__ is overridden:

class MyExceptionGroup(ExceptionGroup):

    def __init__(self, excs):
        super().__init__('Baked-in message', excs)

ex = MyExceptionGroup([Exception()])

# TypeError: BaseExceptionGroup.__new__() takes exactly 2 arguments (1 given)

Overriding __new__ instead of __init__ works:

class MyExceptionGroup(ExceptionGroup):

    def __new__(cls, excs):
        return super().__new__(cls, 'Baked-in message', excs)

ex = MyExceptionGroup([Exception()])
str(ex)    # 'Baked-in message (1 sub-exception)'

But I'm struggling with proper type annotations for MyExceptionGroup:

import typing

ExceptionT_co = typing.TypeVar('ExceptionT_co', bound=Exception, covariant=True)


class MyExceptionGroup(ExceptionGroup[ExceptionT_co]):

    def __new__(cls, excs: typing.Sequence[ExceptionT_co]) -> typing.Self:
        return super().__new__(cls, 'Baked-in message', excs)


# line 9: error: Value of type variable "Self" of "__new__" of "ExceptionGroup" cannot be "Self"  [type-var]
# line 9: error: Argument 3 to "__new__" of "ExceptionGroup" has incompatible type "Sequence[ExceptionT_co]"; expected "Sequence[_ExceptionT_co]"  [arg-type]

_ExceptionT_co is defined as TypeVar('_ExceptionT_co', bound=Exception, covariant=True) in typeshed, which is literally the same type definition as the one I'm trying to use, but mypy still won't accept it, and I'm totally stumped about the "value of type variable Self cannot be Self" error.

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

No branches or pull requests

1 participant
0