Skip to content

Commit d6dd65d

Browse files
committed
fix mypy findings
1 parent 506e402 commit d6dd65d

File tree

7 files changed

+53
-64
lines changed

7 files changed

+53
-64
lines changed

src/quart/app.py

+36-36
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@
125125
from .wrappers import Response
126126
from .wrappers import Websocket
127127

128-
try:
128+
if sys.version_info >= (3, 10):
129129
from typing import ParamSpec
130-
except ImportError:
130+
else:
131131
from typing_extensions import ParamSpec
132132

133133
# Python 3.14 deprecated asyncio.iscoroutinefunction, but suggested
@@ -154,7 +154,7 @@
154154
T_websocket = TypeVar("T_websocket", bound=WebsocketCallable)
155155
T_while_serving = TypeVar("T_while_serving", bound=WhileServingCallable)
156156

157-
T = TypeVar("T")
157+
T = TypeVar("T", bound=Any)
158158
P = ParamSpec("P")
159159

160160

@@ -458,10 +458,10 @@ async def update_template_context(self, context: dict) -> None:
458458
elif has_websocket_context():
459459
names.extend(reversed(websocket_ctx.websocket.blueprints)) # type: ignore
460460

461-
extra_context: dict = {}
461+
extra_context: dict[str, Any] = {}
462462
for name in names:
463463
for processor in self.template_context_processors[name]:
464-
extra_context.update(await self.ensure_async(processor)()) # type: ignore
464+
extra_context.update(await self.ensure_async(processor)()) # type: ignore[call-overload]
465465

466466
original = context.copy()
467467
context.update(extra_context)
@@ -1027,7 +1027,7 @@ async def handle_http_exception(
10271027
if handler is None:
10281028
return error
10291029
else:
1030-
return await self.ensure_async(handler)(error) # type: ignore
1030+
return await self.ensure_async(handler)(error) # type: ignore[return-value]
10311031

10321032
async def handle_user_exception(
10331033
self, error: Exception
@@ -1055,7 +1055,7 @@ async def handle_user_exception(
10551055
handler = self._find_error_handler(error, blueprints)
10561056
if handler is None:
10571057
raise error
1058-
return await self.ensure_async(handler)(error) # type: ignore
1058+
return await self.ensure_async(handler)(error) # type: ignore[return-value]
10591059

10601060
async def handle_exception(self, error: Exception) -> ResponseTypes:
10611061
"""Handle an uncaught exception.
@@ -1066,8 +1066,8 @@ async def handle_exception(self, error: Exception) -> ResponseTypes:
10661066
exc_info = sys.exc_info()
10671067
await got_request_exception.send_async(
10681068
self,
1069-
_sync_wrapper=self.ensure_async,
1070-
exception=error, # type: ignore
1069+
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
1070+
exception=error,
10711071
)
10721072
propagate = self.config["PROPAGATE_EXCEPTIONS"]
10731073

@@ -1088,7 +1088,7 @@ async def handle_exception(self, error: Exception) -> ResponseTypes:
10881088
handler = self._find_error_handler(server_error, request.blueprints)
10891089

10901090
if handler is not None:
1091-
server_error = await self.ensure_async(handler)(server_error) # type: ignore
1091+
server_error = await self.ensure_async(handler)(server_error) # type: ignore[assignment]
10921092

10931093
return await self.finalize_request(server_error, from_error_handler=True)
10941094

@@ -1102,8 +1102,8 @@ async def handle_websocket_exception(
11021102
exc_info = sys.exc_info()
11031103
await got_websocket_exception.send_async(
11041104
self,
1105-
_sync_wrapper=self.ensure_async,
1106-
exception=error, # type: ignore
1105+
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
1106+
exception=error,
11071107
)
11081108
propagate = self.config["PROPAGATE_EXCEPTIONS"]
11091109

@@ -1124,7 +1124,7 @@ async def handle_websocket_exception(
11241124
handler = self._find_error_handler(server_error, websocket.blueprints)
11251125

11261126
if handler is not None:
1127-
server_error = await self.ensure_async(handler)(server_error) # type: ignore
1127+
server_error = await self.ensure_async(handler)(server_error) # type: ignore[assignment]
11281128

11291129
return await self.finalize_websocket(server_error, from_error_handler=True)
11301130

@@ -1205,8 +1205,8 @@ async def do_teardown_request(
12051205

12061206
await request_tearing_down.send_async(
12071207
self,
1208-
_sync_wrapper=self.ensure_async,
1209-
exc=exc, # type: ignore
1208+
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
1209+
exc=exc,
12101210
)
12111211

12121212
async def do_teardown_websocket(
@@ -1229,8 +1229,8 @@ async def do_teardown_websocket(
12291229

12301230
await websocket_tearing_down.send_async(
12311231
self,
1232-
_sync_wrapper=self.ensure_async,
1233-
exc=exc, # type: ignore
1232+
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
1233+
exc=exc,
12341234
)
12351235

12361236
async def do_teardown_appcontext(self, exc: BaseException | None) -> None:
@@ -1239,8 +1239,8 @@ async def do_teardown_appcontext(self, exc: BaseException | None) -> None:
12391239
await self.ensure_async(function)(exc)
12401240
await appcontext_tearing_down.send_async(
12411241
self,
1242-
_sync_wrapper=self.ensure_async,
1243-
exc=exc, # type: ignore
1242+
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
1243+
exc=exc,
12441244
)
12451245

12461246
def app_context(self) -> AppContext:
@@ -1379,8 +1379,8 @@ async def _wrapper() -> None:
13791379
async def handle_background_exception(self, error: Exception) -> None:
13801380
await got_background_exception.send_async(
13811381
self,
1382-
_sync_wrapper=self.ensure_async,
1383-
exception=error, # type: ignore
1382+
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
1383+
exception=error,
13841384
)
13851385

13861386
self.log_exception(sys.exc_info())
@@ -1541,7 +1541,7 @@ async def preprocess_request(
15411541
for function in self.before_request_funcs[name]:
15421542
result = await self.ensure_async(function)()
15431543
if result is not None:
1544-
return result # type: ignore
1544+
return result # type: ignore[return-value]
15451545

15461546
return None
15471547

@@ -1567,7 +1567,7 @@ async def preprocess_websocket(
15671567
for function in self.before_websocket_funcs[name]:
15681568
result = await self.ensure_async(function)()
15691569
if result is not None:
1570-
return result # type: ignore
1570+
return result # type: ignore[return-value]
15711571

15721572
return None
15731573

@@ -1591,7 +1591,7 @@ async def dispatch_request(
15911591
return await self.make_default_options_response()
15921592

15931593
handler = self.view_functions[request_.url_rule.endpoint]
1594-
return await self.ensure_async(handler)(**request_.view_args) # type: ignore
1594+
return await self.ensure_async(handler)(**request_.view_args) # type: ignore[return-value]
15951595

15961596
async def dispatch_websocket(
15971597
self, websocket_context: WebsocketContext | None = None
@@ -1607,7 +1607,7 @@ async def dispatch_websocket(
16071607
self.raise_routing_exception(websocket_)
16081608

16091609
handler = self.view_functions[websocket_.url_rule.endpoint]
1610-
return await self.ensure_async(handler)(**websocket_.view_args) # type: ignore
1610+
return await self.ensure_async(handler)(**websocket_.view_args) # type: ignore[return-value]
16111611

16121612
async def finalize_request(
16131613
self,
@@ -1627,8 +1627,8 @@ async def finalize_request(
16271627
response = await self.process_response(response, request_context)
16281628
await request_finished.send_async(
16291629
self,
1630-
_sync_wrapper=self.ensure_async,
1631-
response=response, # type: ignore
1630+
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
1631+
response=response,
16321632
)
16331633
except Exception:
16341634
if not from_error_handler:
@@ -1657,8 +1657,8 @@ async def finalize_websocket(
16571657
response = await self.postprocess_websocket(response, websocket_context)
16581658
await websocket_finished.send_async(
16591659
self,
1660-
_sync_wrapper=self.ensure_async,
1661-
response=response, # type: ignore
1660+
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
1661+
response=response,
16621662
)
16631663
except Exception:
16641664
if not from_error_handler:
@@ -1681,7 +1681,7 @@ async def process_response(
16811681
names = [*(request_context or request_ctx).request.blueprints, None]
16821682

16831683
for function in (request_context or request_ctx)._after_request_functions:
1684-
response = await self.ensure_async(function)(response) # type: ignore
1684+
response = await self.ensure_async(function)(response) # type: ignore[assignment]
16851685

16861686
for name in names:
16871687
for function in reversed(self.after_request_funcs[name]):
@@ -1709,11 +1709,11 @@ async def postprocess_websocket(
17091709
names = [*(websocket_context or websocket_ctx).websocket.blueprints, None]
17101710

17111711
for function in (websocket_context or websocket_ctx)._after_websocket_functions:
1712-
response = await self.ensure_async(function)(response) # type: ignore
1712+
response = await self.ensure_async(function)(response) # type: ignore[assignment]
17131713

17141714
for name in names:
17151715
for function in reversed(self.after_websocket_funcs[name]):
1716-
response = await self.ensure_async(function)(response) # type: ignore
1716+
response = await self.ensure_async(function)(response) # type: ignore[assignment]
17171717

17181718
session_ = (websocket_context or websocket_ctx).session
17191719
if not self.session_interface.is_null_session(session_):
@@ -1768,8 +1768,8 @@ async def startup(self) -> None:
17681768
except Exception as error:
17691769
await got_serving_exception.send_async(
17701770
self,
1771-
_sync_wrapper=self.ensure_async,
1772-
exception=error, # type: ignore
1771+
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
1772+
exception=error,
17731773
)
17741774
self.log_exception(sys.exc_info())
17751775
raise
@@ -1798,8 +1798,8 @@ async def shutdown(self) -> None:
17981798
except Exception as error:
17991799
await got_serving_exception.send_async(
18001800
self,
1801-
_sync_wrapper=self.ensure_async,
1802-
exception=error, # type: ignore
1801+
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
1802+
exception=error,
18031803
)
18041804
self.log_exception(sys.exc_info())
18051805
raise

src/quart/ctx.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ async def push(self) -> None:
262262
self._cv_tokens.append(_cv_app.set(self))
263263
await appcontext_pushed.send_async(
264264
self.app,
265-
_sync_wrapper=self.app.ensure_async, # type: ignore
265+
_sync_wrapper=self.app.ensure_async, # type: ignore[arg-type]
266266
)
267267

268268
async def pop(self, exc: BaseException | None = _sentinel) -> None: # type: ignore
@@ -282,7 +282,7 @@ async def pop(self, exc: BaseException | None = _sentinel) -> None: # type: ign
282282

283283
await appcontext_popped.send_async(
284284
self.app,
285-
_sync_wrapper=self.app.ensure_async, # type: ignore
285+
_sync_wrapper=self.app.ensure_async, # type: ignore[arg-type]
286286
)
287287

288288
async def __aenter__(self) -> AppContext:

src/quart/templating.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ async def render_template_string(source: str, **context: Any) -> str:
7373
async def _render(template: Template, context: dict, app: Quart) -> str:
7474
await before_render_template.send_async(
7575
app,
76-
_sync_wrapper=app.ensure_async,
76+
_sync_wrapper=app.ensure_async, # type: ignore[arg-type]
7777
template=template,
78-
context=context, # type: ignore
78+
context=context,
7979
)
8080
rendered_template = await template.render_async(context)
8181
await template_rendered.send_async(
8282
app,
83-
_sync_wrapper=app.ensure_async,
83+
_sync_wrapper=app.ensure_async, # type: ignore[arg-type]
8484
template=template,
85-
context=context, # type: ignore
85+
context=context,
8686
)
8787
return rendered_template
8888

@@ -135,19 +135,19 @@ async def _stream(
135135
) -> AsyncIterator[str]:
136136
await before_render_template.send_async(
137137
app,
138-
_sync_wrapper=app.ensure_async,
138+
_sync_wrapper=app.ensure_async, # type: ignore[arg-type]
139139
template=template,
140-
context=context, # type: ignore
140+
context=context,
141141
)
142142

143143
async def generate() -> AsyncIterator[str]:
144144
async for chunk in template.generate_async(context):
145145
yield chunk
146146
await template_rendered.send_async(
147147
app,
148-
_sync_wrapper=app.ensure_async,
148+
_sync_wrapper=app.ensure_async, # type: ignore[arg-type]
149149
template=template,
150-
context=context, # type: ignore
150+
context=context,
151151
)
152152

153153
# If a request context is active, keep it while generating.

src/quart/testing/utils.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Any
44
from typing import AnyStr
55
from typing import cast
6+
from typing import Literal
67
from typing import overload
78
from typing import TYPE_CHECKING
89
from urllib.parse import unquote
@@ -28,11 +29,6 @@
2829
if TYPE_CHECKING:
2930
from ..app import Quart # noqa
3031

31-
try:
32-
from typing import Literal
33-
except ImportError:
34-
from typing_extensions import Literal # type: ignore
35-
3632
sentinel = object()
3733

3834

src/quart/typing.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import os
4+
import sys
45
from collections.abc import AsyncGenerator
56
from collections.abc import Awaitable
67
from collections.abc import Iterator
@@ -26,10 +27,10 @@
2627

2728
from .datastructures import FileStorage
2829

29-
try:
30+
if sys.version_info >= (3, 10):
3031
from typing import Protocol
31-
except ImportError:
32-
from typing_extensions import Protocol # type: ignore
32+
else:
33+
from typing_extensions import Protocol
3334

3435
if TYPE_CHECKING:
3536
from werkzeug.datastructures import Authorization # noqa: F401

src/quart/wrappers/request.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from collections.abc import Generator
66
from typing import Any
77
from typing import Callable
8+
from typing import Literal
89
from typing import NoReturn
910
from typing import overload
1011

@@ -21,11 +22,6 @@
2122
from ..globals import current_app
2223
from .base import BaseRequestWebsocket
2324

24-
try:
25-
from typing import Literal
26-
except ImportError:
27-
from typing_extensions import Literal # type: ignore
28-
2925
SERVER_PUSH_HEADERS_TO_COPY = {
3026
"accept",
3127
"accept-encoding",

src/quart/wrappers/response.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from os import PathLike
1212
from types import TracebackType
1313
from typing import Any
14+
from typing import Literal
1415
from typing import overload
1516
from typing import TYPE_CHECKING
1617

@@ -32,11 +33,6 @@
3233
if TYPE_CHECKING:
3334
from .request import Request
3435

35-
try:
36-
from typing import Literal
37-
except ImportError:
38-
from typing_extensions import Literal # type: ignore
39-
4036

4137
class ResponseBody(ABC):
4238
"""Base class wrapper for response body data.

0 commit comments

Comments
 (0)