-
-
Notifications
You must be signed in to change notification settings - Fork 962
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
Add proper type hints to add_exception_handler #1308
Conversation
I realized there are a lot of other callables in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, PRs improving the type hints are welcome.
As for this PR, can we also add the type hint on the starlette/exceptions.py
file as well (in the analogous signature)?
@@ -129,7 +131,7 @@ def add_middleware(self, middleware_class: type, **options: typing.Any) -> None: | |||
def add_exception_handler( | |||
self, | |||
exc_class_or_status_code: typing.Union[int, typing.Type[Exception]], | |||
handler: typing.Callable, | |||
handler: typing.Callable[[Request, Exception], typing.Union[Response, typing.Coroutine[typing.Any, typing.Any, Response]]], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we perhaps use Awaitable
instead? It looks better. 😗
handler: typing.Callable[[Request, Exception], typing.Union[Response, typing.Coroutine[typing.Any, typing.Any, Response]]], | |
handler: typing.Callable[[Request, Exception], typing.Union[Response, Awaitable[Response]]], |
@phillipuniverse Are you willing to address the changes requested? |
@aminalaee yes! Sorry, this fell off my radar a bit. I’ll get this back on track in the next couple of days. |
Thanks for the PR @phillipuniverse ! There's a discussion on #1456 about the idea we see here, so I'll close this PR. |
I lost a bit of time due to a missing return statement and type hint in an exception handler.
In my case I had registered an
HTTPException
exception handler without a return. Here's the exception I ended up with:Here's what my exception handler and registration looked like:
This is clearly an error but it would've been nice to get a type hint error on it before I went through and traced down the implementation.
Thanks for such a great library!