-
-
Notifications
You must be signed in to change notification settings - Fork 934
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
Generic ExceptionHandler type #2403
Open
tekumara
wants to merge
25
commits into
encode:master
Choose a base branch
from
tekumara:exception-handler-typing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+24
−10
Open
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
d9037c5
generic exception
tekumara 7cecce7
ruff format
tekumara f35c796
bind exc_class_or_status_code to E
tekumara 2300839
Revert "ruff format"
tekumara 6cf35d6
ruff format starlette/types.py
tekumara ba30a49
s/E/ExceptionType/
tekumara e94fc1c
Merge branch 'master' of https://github.com/tekumara/starlette into e…
tekumara 77cca8b
contravariance is redundant because Callable is contravariant
tekumara 1aebedb
Merge branch 'master' into exception-handler-typing
tekumara da3f8bb
Merge branch 'master' into exception-handler-typing
tekumara 145a9b1
Merge branch 'master' into exception-handler-typing
tekumara c71d90c
Merge branch 'master' into exception-handler-typing
Kludex 64328f4
Merge branch 'master' of https://github.com/tekumara/starlette into e…
tekumara 6f89af7
Merge branch 'master' into exception-handler-typing
tekumara 1890a85
Merge branch 'master' into exception-handler-typing
tekumara adfafdf
Merge branch 'master' into exception-handler-typing
tekumara ac72313
Merge branch 'master' into exception-handler-typing
tekumara 25fafd2
Merge branch 'master' into exception-handler-typing
tekumara c0cbf4a
Merge branch 'master' of https://github.com/tekumara/starlette into e…
tekumara bd81d9f
ruff format
tekumara 292259c
overload __init__ to support multiple exception handlers
tekumara ebcd61f
use Callable for exception_handlers
tekumara c1b05dd
fix mypy lints
tekumara 9cfe2bd
fix ruff lints
tekumara aa2a879
Merge branch 'master' into exception-handler-typing
tekumara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,10 @@ | |
] | ||
Lifespan = typing.Union[StatelessLifespan[AppType], StatefulLifespan[AppType]] | ||
|
||
E = typing.TypeVar("E", bound=Exception, contravariant=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we call it |
||
|
||
HTTPExceptionHandler = typing.Callable[ | ||
["Request", Exception], typing.Union["Response", typing.Awaitable["Response"]] | ||
] | ||
WebSocketExceptionHandler = typing.Callable[ | ||
["WebSocket", Exception], typing.Awaitable[None] | ||
["Request", E], typing.Union["Response", typing.Awaitable["Response"]] | ||
] | ||
ExceptionHandler = typing.Union[HTTPExceptionHandler, WebSocketExceptionHandler] | ||
WebSocketExceptionHandler = typing.Callable[["WebSocket", E], typing.Awaitable[None]] | ||
ExceptionHandler = typing.Union[HTTPExceptionHandler[E], WebSocketExceptionHandler[E]] |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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.
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.
NB: this has now been tightened so that the Exception for
exc_class_or_status_code
must match the Exception in the handler Callable.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.
@Kludex isn't this a deprecated function?
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.
No. We never deprecated
add_exception_handler
... 🤔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.
BTW, to be more precise, the Exception in the handler Callable is still contravariant, so this passes as one would hope:
ie: the
ExceptionType
TypeVar changes the handler CallableException
to a type parameter. The value of this type parameter is provided at the call site, eg: in the example here itsException
in the first call, andMyException
in the second. But the contravariance of Callable remains.And also the Callable is still not covariant, so this fails as one might expect: