1
1
from __future__ import annotations
2
2
3
3
import sys
4
- import typing
5
4
import warnings
5
+ from collections .abc import Awaitable , Mapping , Sequence
6
+ from typing import Any , Callable , TypeVar
6
7
7
8
if sys .version_info >= (3 , 10 ): # pragma: no cover
8
9
from typing import ParamSpec
20
21
from starlette .types import ASGIApp , ExceptionHandler , Lifespan , Receive , Scope , Send
21
22
from starlette .websockets import WebSocket
22
23
23
- AppType = typing . TypeVar ("AppType" , bound = "Starlette" )
24
+ AppType = TypeVar ("AppType" , bound = "Starlette" )
24
25
P = ParamSpec ("P" )
25
26
26
27
@@ -30,11 +31,11 @@ class Starlette:
30
31
def __init__ (
31
32
self : AppType ,
32
33
debug : bool = False ,
33
- routes : typing . Sequence [BaseRoute ] | None = None ,
34
- middleware : typing . Sequence [Middleware ] | None = None ,
35
- exception_handlers : typing . Mapping [typing . Any , ExceptionHandler ] | None = None ,
36
- on_startup : typing . Sequence [typing . Callable [[], typing . Any ]] | None = None ,
37
- on_shutdown : typing . Sequence [typing . Callable [[], typing . Any ]] | None = None ,
34
+ routes : Sequence [BaseRoute ] | None = None ,
35
+ middleware : Sequence [Middleware ] | None = None ,
36
+ exception_handlers : Mapping [Any , ExceptionHandler ] | None = None ,
37
+ on_startup : Sequence [Callable [[], Any ]] | None = None ,
38
+ on_shutdown : Sequence [Callable [[], Any ]] | None = None ,
38
39
lifespan : Lifespan [AppType ] | None = None ,
39
40
) -> None :
40
41
"""Initializes the application.
@@ -79,7 +80,7 @@ def __init__(
79
80
def build_middleware_stack (self ) -> ASGIApp :
80
81
debug = self .debug
81
82
error_handler = None
82
- exception_handlers : dict [typing . Any , typing . Callable [[Request , Exception ], Response ]] = {}
83
+ exception_handlers : dict [Any , Callable [[Request , Exception ], Response ]] = {}
83
84
84
85
for key , value in self .exception_handlers .items ():
85
86
if key in (500 , Exception ):
@@ -102,7 +103,7 @@ def build_middleware_stack(self) -> ASGIApp:
102
103
def routes (self ) -> list [BaseRoute ]:
103
104
return self .router .routes
104
105
105
- def url_path_for (self , name : str , / , ** path_params : typing . Any ) -> URLPath :
106
+ def url_path_for (self , name : str , / , ** path_params : Any ) -> URLPath :
106
107
return self .router .url_path_for (name , ** path_params )
107
108
108
109
async def __call__ (self , scope : Scope , receive : Receive , send : Send ) -> None :
@@ -111,7 +112,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
111
112
self .middleware_stack = self .build_middleware_stack ()
112
113
await self .middleware_stack (scope , receive , send )
113
114
114
- def on_event (self , event_type : str ) -> typing . Callable : # type: ignore[type-arg]
115
+ def on_event (self , event_type : str ) -> Callable : # type: ignore[type-arg]
115
116
return self .router .on_event (event_type ) # pragma: no cover
116
117
117
118
def mount (self , path : str , app : ASGIApp , name : str | None = None ) -> None :
@@ -140,14 +141,14 @@ def add_exception_handler(
140
141
def add_event_handler (
141
142
self ,
142
143
event_type : str ,
143
- func : typing . Callable , # type: ignore[type-arg]
144
+ func : Callable , # type: ignore[type-arg]
144
145
) -> None : # pragma: no cover
145
146
self .router .add_event_handler (event_type , func )
146
147
147
148
def add_route (
148
149
self ,
149
150
path : str ,
150
- route : typing . Callable [[Request ], typing . Awaitable [Response ] | Response ],
151
+ route : Callable [[Request ], Awaitable [Response ] | Response ],
151
152
methods : list [str ] | None = None ,
152
153
name : str | None = None ,
153
154
include_in_schema : bool = True ,
@@ -157,19 +158,19 @@ def add_route(
157
158
def add_websocket_route (
158
159
self ,
159
160
path : str ,
160
- route : typing . Callable [[WebSocket ], typing . Awaitable [None ]],
161
+ route : Callable [[WebSocket ], Awaitable [None ]],
161
162
name : str | None = None ,
162
163
) -> None : # pragma: no cover
163
164
self .router .add_websocket_route (path , route , name = name )
164
165
165
- def exception_handler (self , exc_class_or_status_code : int | type [Exception ]) -> typing . Callable : # type: ignore[type-arg]
166
+ def exception_handler (self , exc_class_or_status_code : int | type [Exception ]) -> Callable : # type: ignore[type-arg]
166
167
warnings .warn (
167
168
"The `exception_handler` decorator is deprecated, and will be removed in version 1.0.0. "
168
169
"Refer to https://www.starlette.io/exceptions/ for the recommended approach." ,
169
170
DeprecationWarning ,
170
171
)
171
172
172
- def decorator (func : typing . Callable ) -> typing . Callable : # type: ignore[type-arg]
173
+ def decorator (func : Callable ) -> Callable : # type: ignore[type-arg]
173
174
self .add_exception_handler (exc_class_or_status_code , func )
174
175
return func
175
176
@@ -181,7 +182,7 @@ def route(
181
182
methods : list [str ] | None = None ,
182
183
name : str | None = None ,
183
184
include_in_schema : bool = True ,
184
- ) -> typing . Callable : # type: ignore[type-arg]
185
+ ) -> Callable : # type: ignore[type-arg]
185
186
"""
186
187
We no longer document this decorator style API, and its usage is discouraged.
187
188
Instead you should use the following approach:
@@ -195,7 +196,7 @@ def route(
195
196
DeprecationWarning ,
196
197
)
197
198
198
- def decorator (func : typing . Callable ) -> typing . Callable : # type: ignore[type-arg]
199
+ def decorator (func : Callable ) -> Callable : # type: ignore[type-arg]
199
200
self .router .add_route (
200
201
path ,
201
202
func ,
@@ -207,7 +208,7 @@ def decorator(func: typing.Callable) -> typing.Callable: # type: ignore[type-ar
207
208
208
209
return decorator
209
210
210
- def websocket_route (self , path : str , name : str | None = None ) -> typing . Callable : # type: ignore[type-arg]
211
+ def websocket_route (self , path : str , name : str | None = None ) -> Callable : # type: ignore[type-arg]
211
212
"""
212
213
We no longer document this decorator style API, and its usage is discouraged.
213
214
Instead you should use the following approach:
@@ -221,13 +222,13 @@ def websocket_route(self, path: str, name: str | None = None) -> typing.Callable
221
222
DeprecationWarning ,
222
223
)
223
224
224
- def decorator (func : typing . Callable ) -> typing . Callable : # type: ignore[type-arg]
225
+ def decorator (func : Callable ) -> Callable : # type: ignore[type-arg]
225
226
self .router .add_websocket_route (path , func , name = name )
226
227
return func
227
228
228
229
return decorator
229
230
230
- def middleware (self , middleware_type : str ) -> typing . Callable : # type: ignore[type-arg]
231
+ def middleware (self , middleware_type : str ) -> Callable : # type: ignore[type-arg]
231
232
"""
232
233
We no longer document this decorator style API, and its usage is discouraged.
233
234
Instead you should use the following approach:
@@ -242,7 +243,7 @@ def middleware(self, middleware_type: str) -> typing.Callable: # type: ignore[t
242
243
)
243
244
assert middleware_type == "http" , 'Currently only middleware("http") is supported.'
244
245
245
- def decorator (func : typing . Callable ) -> typing . Callable : # type: ignore[type-arg]
246
+ def decorator (func : Callable ) -> Callable : # type: ignore[type-arg]
246
247
self .add_middleware (BaseHTTPMiddleware , dispatch = func )
247
248
return func
248
249
0 commit comments