Allow async exception handlers to type-check #2949
Merged
+32
−16
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
ExceptionMiddleware
directly, 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: ignore
s, missing this simple error.The fix is just to reuse the existing
ExceptionHandler
type alias.I wasn't sure how to add tests for these changes. I added some code in
test_exceptions.py
that is just used to check there is not type error, let me know if there is another preferred approach.While using the
ExceptionHandler
alias in a few places, it unearthed another error that can be solved by usingTypeIs
instead ofTypeGuard
inis_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