use bound typevar to accept Flask and Werkzeug Response classes

This commit is contained in:
Justin Bull 2022-05-19 13:34:46 -04:00 committed by David Lord
parent a4f63e0390
commit 8cb950671f
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
2 changed files with 8 additions and 2 deletions

View file

@ -7,6 +7,7 @@ Unreleased
- Inline some optional imports that are only used for certain CLI
commands. :pr:`4606`
- Relax type annotation for ``after_request`` functions. :issue:`4600`
Version 2.1.2

View file

@ -4,7 +4,7 @@ import typing as t
if t.TYPE_CHECKING:
from _typeshed.wsgi import WSGIApplication # noqa: F401
from werkzeug.datastructures import Headers # noqa: F401
from werkzeug.wrappers.response import Response # noqa: F401
from werkzeug.wrappers import Response # noqa: F401
# The possible types that are directly convertible or are a Response object.
ResponseValue = t.Union[
@ -35,8 +35,13 @@ ResponseReturnValue = t.Union[
"WSGIApplication",
]
# Allow any subclass of werkzeug.Response, such as the one from Flask,
# as a callback argument. Using werkzeug.Response directly makes a
# callback annotated with flask.Response fail type checking.
ResponseClass = t.TypeVar("ResponseClass", bound="Response")
AppOrBlueprintKey = t.Optional[str] # The App key is None, whereas blueprints are named
AfterRequestCallable = t.Callable[["Response"], "Response"]
AfterRequestCallable = t.Callable[[ResponseClass], ResponseClass]
BeforeFirstRequestCallable = t.Callable[[], None]
BeforeRequestCallable = t.Callable[[], t.Optional[ResponseReturnValue]]
TeardownCallable = t.Callable[[t.Optional[BaseException]], None]