Allow async exception handlers to type-check#2949
Merged
Kludex merged 7 commits intoKludex:masterfrom Jun 13, 2025
Merged
Conversation
jonathanberthias
commented
Jun 2, 2025
Kludex
approved these changes
Jun 13, 2025
Owner
|
Thanks! 🙏 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
There has been a lot of discussion around the exception handlers' type annotations, e.g., #2403 and #2048, but there is a simple case that isn't currently handled and can be fixed easily.
When building the
ExceptionMiddlewaredirectly, only sync handlers are accepted, even though the code can perfectly well handle async handlers. I think this was not noticed before because not many users instantiate this middleware directly, and when they do, the errors linked to the exceptions' variance always led to adding# type: ignores, missing this simple error.The fix is just to reuse the existing
ExceptionHandlertype alias.I wasn't sure how to add tests for these changes. I added some code in
test_exceptions.pythat is just used to check there is not type error, let me know if there is another preferred approach.While using the
ExceptionHandleralias in a few places, it unearthed another error that can be solved by usingTypeIsinstead ofTypeGuardinis_async_callable. This fixes a few false positives from MyPy, though is not directly related to the initial change in this PR so I totally understand if you prefer I leave it out.Checklist