-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[question] Proper way to implement ExceptionGroup.derive
method with the existing overloads?
#9922
Comments
Hi Kevin, hope you're well! I'm paging @sobolevn on this one, who authored our current stack of overloads and test cases for |
I am sorry, my example was not correct. You can not have exception types there. Here are correct ones: >>> BaseExceptionGroup('a', [SystemExit()]).derive([ValueError()])
ExceptionGroup('a', [ValueError()])
>>> ExceptionGroup('a', [ValueError()]).derive([SystemExit()])
BaseExceptionGroup('a', [SystemExit()]) This is why we have two overloads. And |
But isn't that specifically the base classes not what derive is meant to do when implemented by the user, as outlined in the linked example?
https://docs.python.org/3/library/exceptions.html#BaseExceptionGroup.derive Just trying to understand how the base classes returning the exception group should be overloaded by the end user given the base exception group overload, specifically. |
Yes, this is just the base classes. Custom types can return whatever they like. |
I'm sorry, I was still trying to understand fully what the situation was. Based on your feedback, my understanding is that the example cannot currently be typed given the current overloads, as the separation of BaseException from Exception requires BaseExceptions to return BaseExceptionGroup which isn't a requirement of a custom exception group. Is that a correct understanding of your feedback? Happy to help brainstorm solutions, I want to make sure I understand how it's supposed to work in a custom error group to understand it if there is an opportunity to change type hints or if instead it's more of a documentation change where the example should call out that handling BaseExceptions should be / may be necessary separate of the Exceptions (at least for type uses). Thanks for your patience |
Yes.
I can think of several solutions.
|
I think I lean towards option 1 of those two solutions, if for no other reason than it aligns with the example use case outlined in the documentation. An alternative I was wondering about is whether it would make sense to return a Looking at the types of I haven't attempted to implement it yet, so there may be a gotcha I'm not aware of, but was wondering if this would make sense or not. What is your opinion on this idea? |
I ran into this issue as well when writing a custom |
ExceptionGroup.derive
method with the existing overrides?ExceptionGroup.derive
method with the existing overloads?
Good afternoon,
I was working to implement some custom exception groups in an application that I work on and have had a really hard time getting the types for the
derive
function to work correctly. Based on reading the documentation linked below,derive
is a required method when overriding an exception group to return an instance of it's own class.Reference
https://docs.python.org/3/library/exceptions.html#BaseExceptionGroup.derive
Reproduction / Example
To reproduce this error, I'll use the example shows in the BaseExceptionGroup page, but with type hints:
Mypy output:
Given that this is working off an exception group rather than a base exception group, I'm unclear on what the proper way to fix this would be and was wondering if the team here was able to provide an example that satisfies mypy / typeshed's overrides for
BaseExceptionGroup
'sderive
function.Thoughts
Would it potentially make sense to add a derive method to
ExceptionGroup
which returnsSelf
rather than a hardcodedExceptionGroup
? This seems to align with how the documentation describes using this behavior, but I'm not confident I understand fully to submit a pull request with recommended change(s).Thanks for your time and help, hope you all are having a great day.
The text was updated successfully, but these errors were encountered: