1
1
from __future__ import annotations
2
2
3
+ import collections .abc as cabc
3
4
import os
4
5
import sys
5
6
import typing as t
6
7
import weakref
7
- from collections .abc import Iterator as _abc_Iterator
8
8
from datetime import timedelta
9
9
from inspect import iscoroutinefunction
10
10
from itertools import chain
54
54
from .wrappers import Response
55
55
56
56
if t .TYPE_CHECKING : # pragma: no cover
57
+ from _typeshed .wsgi import StartResponse
58
+ from _typeshed .wsgi import WSGIEnvironment
59
+
57
60
from .testing import FlaskClient
58
61
from .testing import FlaskCliRunner
59
62
@@ -200,11 +203,11 @@ class Flask(App):
200
203
201
204
#: The class that is used for request objects. See :class:`~flask.Request`
202
205
#: for more information.
203
- request_class = Request
206
+ request_class : type [ Request ] = Request
204
207
205
208
#: The class that is used for response objects. See
206
209
#: :class:`~flask.Response` for more information.
207
- response_class = Response
210
+ response_class : type [ Response ] = Response
208
211
209
212
#: the session interface to use. By default an instance of
210
213
#: :class:`~flask.sessions.SecureCookieSessionInterface` is used here.
@@ -216,11 +219,11 @@ def __init__(
216
219
self ,
217
220
import_name : str ,
218
221
static_url_path : str | None = None ,
219
- static_folder : str | os .PathLike | None = "static" ,
222
+ static_folder : str | os .PathLike [ str ] | None = "static" ,
220
223
static_host : str | None = None ,
221
224
host_matching : bool = False ,
222
225
subdomain_matching : bool = False ,
223
- template_folder : str | os .PathLike | None = "templates" ,
226
+ template_folder : str | os .PathLike [ str ] | None = "templates" ,
224
227
instance_path : str | None = None ,
225
228
instance_relative_config : bool = False ,
226
229
root_path : str | None = None ,
@@ -282,7 +285,7 @@ def get_send_file_max_age(self, filename: str | None) -> int | None:
282
285
if isinstance (value , timedelta ):
283
286
return int (value .total_seconds ())
284
287
285
- return value
288
+ return value # type: ignore[no-any-return]
286
289
287
290
def send_static_file (self , filename : str ) -> Response :
288
291
"""The view function used to serve files from
@@ -447,13 +450,13 @@ def raise_routing_exception(self, request: Request) -> t.NoReturn:
447
450
or request .routing_exception .code in {307 , 308 }
448
451
or request .method in {"GET" , "HEAD" , "OPTIONS" }
449
452
):
450
- raise request .routing_exception # type: ignore
453
+ raise request .routing_exception # type: ignore[misc]
451
454
452
455
from .debughelpers import FormDataRoutingRedirect
453
456
454
457
raise FormDataRoutingRedirect (request )
455
458
456
- def update_template_context (self , context : dict ) -> None :
459
+ def update_template_context (self , context : dict [ str , t . Any ] ) -> None :
457
460
"""Update the template context with some commonly used variables.
458
461
This injects request, session, config and g into the template
459
462
context as well as everything template context processors want
@@ -481,7 +484,7 @@ def update_template_context(self, context: dict) -> None:
481
484
482
485
context .update (orig_ctx )
483
486
484
- def make_shell_context (self ) -> dict :
487
+ def make_shell_context (self ) -> dict [ str , t . Any ] :
485
488
"""Returns the shell context for an interactive shell for this
486
489
application. This runs all the registered shell context
487
490
processors.
@@ -724,7 +727,7 @@ def handle_http_exception(
724
727
handler = self ._find_error_handler (e , request .blueprints )
725
728
if handler is None :
726
729
return e
727
- return self .ensure_sync (handler )(e )
730
+ return self .ensure_sync (handler )(e ) # type: ignore[no-any-return]
728
731
729
732
def handle_user_exception (
730
733
self , e : Exception
@@ -756,7 +759,7 @@ def handle_user_exception(
756
759
if handler is None :
757
760
raise
758
761
759
- return self .ensure_sync (handler )(e )
762
+ return self .ensure_sync (handler )(e ) # type: ignore[no-any-return]
760
763
761
764
def handle_exception (self , e : Exception ) -> Response :
762
765
"""Handle an exception that did not have an error handler
@@ -849,7 +852,7 @@ def dispatch_request(self) -> ft.ResponseReturnValue:
849
852
return self .make_default_options_response ()
850
853
# otherwise dispatch to the handler for that endpoint
851
854
view_args : dict [str , t .Any ] = req .view_args # type: ignore[assignment]
852
- return self .ensure_sync (self .view_functions [rule .endpoint ])(** view_args )
855
+ return self .ensure_sync (self .view_functions [rule .endpoint ])(** view_args ) # type: ignore[no-any-return]
853
856
854
857
def full_dispatch_request (self ) -> Response :
855
858
"""Dispatches the request and on top of that performs request
@@ -913,7 +916,7 @@ def make_default_options_response(self) -> Response:
913
916
rv .allow .update (methods )
914
917
return rv
915
918
916
- def ensure_sync (self , func : t .Callable ) -> t .Callable :
919
+ def ensure_sync (self , func : t .Callable [..., t . Any ] ) -> t .Callable [..., t . Any ] :
917
920
"""Ensure that the function is synchronous for WSGI workers.
918
921
Plain ``def`` functions are returned as-is. ``async def``
919
922
functions are wrapped to run and wait for the response.
@@ -928,7 +931,7 @@ def ensure_sync(self, func: t.Callable) -> t.Callable:
928
931
return func
929
932
930
933
def async_to_sync (
931
- self , func : t .Callable [..., t .Coroutine ]
934
+ self , func : t .Callable [..., t .Coroutine [ t . Any , t . Any , t . Any ] ]
932
935
) -> t .Callable [..., t .Any ]:
933
936
"""Return a sync function that will run the coroutine function.
934
937
@@ -1166,7 +1169,7 @@ def make_response(self, rv: ft.ResponseReturnValue) -> Response:
1166
1169
1167
1170
# make sure the body is an instance of the response class
1168
1171
if not isinstance (rv , self .response_class ):
1169
- if isinstance (rv , (str , bytes , bytearray )) or isinstance (rv , _abc_Iterator ):
1172
+ if isinstance (rv , (str , bytes , bytearray )) or isinstance (rv , cabc . Iterator ):
1170
1173
# let the response class set the status and headers instead of
1171
1174
# waiting to do it manually, so that the class can handle any
1172
1175
# special logic
@@ -1240,7 +1243,7 @@ def preprocess_request(self) -> ft.ResponseReturnValue | None:
1240
1243
rv = self .ensure_sync (before_func )()
1241
1244
1242
1245
if rv is not None :
1243
- return rv
1246
+ return rv # type: ignore[no-any-return]
1244
1247
1245
1248
return None
1246
1249
@@ -1353,7 +1356,7 @@ def app_context(self) -> AppContext:
1353
1356
"""
1354
1357
return AppContext (self )
1355
1358
1356
- def request_context (self , environ : dict ) -> RequestContext :
1359
+ def request_context (self , environ : WSGIEnvironment ) -> RequestContext :
1357
1360
"""Create a :class:`~flask.ctx.RequestContext` representing a
1358
1361
WSGI environment. Use a ``with`` block to push the context,
1359
1362
which will make :data:`request` point at this request.
@@ -1425,7 +1428,9 @@ def test_request_context(self, *args: t.Any, **kwargs: t.Any) -> RequestContext:
1425
1428
finally :
1426
1429
builder .close ()
1427
1430
1428
- def wsgi_app (self , environ : dict , start_response : t .Callable ) -> t .Any :
1431
+ def wsgi_app (
1432
+ self , environ : WSGIEnvironment , start_response : StartResponse
1433
+ ) -> cabc .Iterable [bytes ]:
1429
1434
"""The actual WSGI application. This is not implemented in
1430
1435
:meth:`__call__` so that middlewares can be applied without
1431
1436
losing a reference to the app object. Instead of doing this::
@@ -1473,7 +1478,9 @@ def wsgi_app(self, environ: dict, start_response: t.Callable) -> t.Any:
1473
1478
1474
1479
ctx .pop (error )
1475
1480
1476
- def __call__ (self , environ : dict , start_response : t .Callable ) -> t .Any :
1481
+ def __call__ (
1482
+ self , environ : WSGIEnvironment , start_response : StartResponse
1483
+ ) -> cabc .Iterable [bytes ]:
1477
1484
"""The WSGI server calls the Flask application object as the
1478
1485
WSGI application. This calls :meth:`wsgi_app`, which can be
1479
1486
wrapped to apply middleware.
0 commit comments