-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Using Generic Exceptions (ExceptionGroups in particular) with raises in a type-safe manner is not ideal #13115
Comments
Yep, I think calling |
I'll take a shot at it. The only hesitation is that it may look somewhat misleading in that we would still actually catch all i.e. the following satisfying the logic of raises without a single
There are some similarities to the standard I personally still think practicality beats purity here and that the narrower focus and implied experience-level of pytest users (especially users of Of course you can dig deeper and recognize a parametrized |
Hmm, that's a good point, and actually does make me more nervous about just unwrapping the generic - "tests check less than the developer expected" is a classic way for bugs to sneak through to production. We've actually been working on #12504 via |
For all my uses just always using the full The real annoyance and problem for me/us here is really only that ExceptionGroup is generic but without a default type-argument which causes pyright/pylance to complain about the resulting type of the return value being incomplete/unspecified on the stricter setting and there being no really good place to add it as you can't simply type-annotate the result of a context-manager. I do realize that is a quite narrow and specific concern though with annoying/ugly but fully functional workarounds so this is not really something pressing. Loose thought without thinking too deep would be if it would be worth it if runtime could accept just the explicit common types |
Yep, I'd be happy to accept a PR for that minimal version anytime. |
I added a PR with discussed fixes (Only accepts specifically A bit larger patch than I would've liked due to the need to handle them in tuples as well but seems to work well both on versions with native support and without. Was a bit unsure about where to add the tests so some feedback on the PR would be welcome. |
As I understand the arguments for not extending
pytest.raises
to automatically unwrapExceptionGroups
as discussed in #11538 I've tried to use theExceptionInfo.group_contains()
method which works well enough for the runtime parts.However since the return value of
raises()
(ExceptionInfo
) is generic in the exception type, the stricter type-checkers will complain about the unknown type-argument of theExceptionGroup
.If trying to rectify this by supplying the type-argument it fails various checks in
raises
that checks that the argument is an instance oftype
whichGenericAlias
(that you get when indexing a generic class) is not.The correct casting that is required to get this correct would have to be something like below.
This could probably be fixed by handling generic Exceptions (or at least ExceptionGroups) explicitly in raises using https://docs.python.org/3/library/typing.html#typing.get_origin.
The text was updated successfully, but these errors were encountered: