Skip to content

Commit

Permalink
fix mypy findings
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed Nov 18, 2024
1 parent 674e57d commit 8139d96
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 64 deletions.
72 changes: 36 additions & 36 deletions src/quart/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@
from .wrappers import Response
from .wrappers import Websocket

try:
if sys.version_info >= (3, 10):
from typing import ParamSpec
except ImportError:
else:
from typing_extensions import ParamSpec

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

T = TypeVar("T")
T = TypeVar("T", bound=Any)
P = ParamSpec("P")


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

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

original = context.copy()
context.update(extra_context)
Expand Down Expand Up @@ -1027,7 +1027,7 @@ async def handle_http_exception(
if handler is None:
return error
else:
return await self.ensure_async(handler)(error) # type: ignore
return await self.ensure_async(handler)(error) # type: ignore[return-value]

async def handle_user_exception(
self, error: Exception
Expand Down Expand Up @@ -1055,7 +1055,7 @@ async def handle_user_exception(
handler = self._find_error_handler(error, blueprints)
if handler is None:
raise error
return await self.ensure_async(handler)(error) # type: ignore
return await self.ensure_async(handler)(error) # type: ignore[return-value]

async def handle_exception(self, error: Exception) -> ResponseTypes:
"""Handle an uncaught exception.
Expand All @@ -1066,8 +1066,8 @@ async def handle_exception(self, error: Exception) -> ResponseTypes:
exc_info = sys.exc_info()
await got_request_exception.send_async(
self,
_sync_wrapper=self.ensure_async,
exception=error, # type: ignore
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
exception=error,
)
propagate = self.config["PROPAGATE_EXCEPTIONS"]

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

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

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

Expand All @@ -1102,8 +1102,8 @@ async def handle_websocket_exception(
exc_info = sys.exc_info()
await got_websocket_exception.send_async(
self,
_sync_wrapper=self.ensure_async,
exception=error, # type: ignore
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
exception=error,
)
propagate = self.config["PROPAGATE_EXCEPTIONS"]

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

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

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

Expand Down Expand Up @@ -1205,8 +1205,8 @@ async def do_teardown_request(

await request_tearing_down.send_async(
self,
_sync_wrapper=self.ensure_async,
exc=exc, # type: ignore
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
exc=exc,
)

async def do_teardown_websocket(
Expand All @@ -1229,8 +1229,8 @@ async def do_teardown_websocket(

await websocket_tearing_down.send_async(
self,
_sync_wrapper=self.ensure_async,
exc=exc, # type: ignore
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
exc=exc,
)

async def do_teardown_appcontext(self, exc: BaseException | None) -> None:
Expand All @@ -1239,8 +1239,8 @@ async def do_teardown_appcontext(self, exc: BaseException | None) -> None:
await self.ensure_async(function)(exc)
await appcontext_tearing_down.send_async(
self,
_sync_wrapper=self.ensure_async,
exc=exc, # type: ignore
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
exc=exc,
)

def app_context(self) -> AppContext:
Expand Down Expand Up @@ -1379,8 +1379,8 @@ async def _wrapper() -> None:
async def handle_background_exception(self, error: Exception) -> None:
await got_background_exception.send_async(
self,
_sync_wrapper=self.ensure_async,
exception=error, # type: ignore
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
exception=error,
)

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

return None

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

return None

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

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

async def dispatch_websocket(
self, websocket_context: WebsocketContext | None = None
Expand All @@ -1607,7 +1607,7 @@ async def dispatch_websocket(
self.raise_routing_exception(websocket_)

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

async def finalize_request(
self,
Expand All @@ -1627,8 +1627,8 @@ async def finalize_request(
response = await self.process_response(response, request_context)
await request_finished.send_async(
self,
_sync_wrapper=self.ensure_async,
response=response, # type: ignore
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
response=response,
)
except Exception:
if not from_error_handler:
Expand Down Expand Up @@ -1657,8 +1657,8 @@ async def finalize_websocket(
response = await self.postprocess_websocket(response, websocket_context)
await websocket_finished.send_async(
self,
_sync_wrapper=self.ensure_async,
response=response, # type: ignore
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
response=response,
)
except Exception:
if not from_error_handler:
Expand All @@ -1681,7 +1681,7 @@ async def process_response(
names = [*(request_context or request_ctx).request.blueprints, None]

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

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

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

for name in names:
for function in reversed(self.after_websocket_funcs[name]):
response = await self.ensure_async(function)(response) # type: ignore
response = await self.ensure_async(function)(response) # type: ignore[assignment]

session_ = (websocket_context or websocket_ctx).session
if not self.session_interface.is_null_session(session_):
Expand Down Expand Up @@ -1768,8 +1768,8 @@ async def startup(self) -> None:
except Exception as error:
await got_serving_exception.send_async(
self,
_sync_wrapper=self.ensure_async,
exception=error, # type: ignore
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
exception=error,
)
self.log_exception(sys.exc_info())
raise
Expand Down Expand Up @@ -1798,8 +1798,8 @@ async def shutdown(self) -> None:
except Exception as error:
await got_serving_exception.send_async(
self,
_sync_wrapper=self.ensure_async,
exception=error, # type: ignore
_sync_wrapper=self.ensure_async, # type: ignore[arg-type]
exception=error,
)
self.log_exception(sys.exc_info())
raise
Expand Down
4 changes: 2 additions & 2 deletions src/quart/ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ async def push(self) -> None:
self._cv_tokens.append(_cv_app.set(self))
await appcontext_pushed.send_async(
self.app,
_sync_wrapper=self.app.ensure_async, # type: ignore
_sync_wrapper=self.app.ensure_async, # type: ignore[arg-type]
)

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

await appcontext_popped.send_async(
self.app,
_sync_wrapper=self.app.ensure_async, # type: ignore
_sync_wrapper=self.app.ensure_async, # type: ignore[arg-type]
)

async def __aenter__(self) -> AppContext:
Expand Down
16 changes: 8 additions & 8 deletions src/quart/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ async def render_template_string(source: str, **context: Any) -> str:
async def _render(template: Template, context: dict, app: Quart) -> str:
await before_render_template.send_async(
app,
_sync_wrapper=app.ensure_async,
_sync_wrapper=app.ensure_async, # type: ignore[arg-type]
template=template,
context=context, # type: ignore
context=context,
)
rendered_template = await template.render_async(context)
await template_rendered.send_async(
app,
_sync_wrapper=app.ensure_async,
_sync_wrapper=app.ensure_async, # type: ignore[arg-type]
template=template,
context=context, # type: ignore
context=context,
)
return rendered_template

Expand Down Expand Up @@ -135,19 +135,19 @@ async def _stream(
) -> AsyncIterator[str]:
await before_render_template.send_async(
app,
_sync_wrapper=app.ensure_async,
_sync_wrapper=app.ensure_async, # type: ignore[arg-type]
template=template,
context=context, # type: ignore
context=context,
)

async def generate() -> AsyncIterator[str]:
async for chunk in template.generate_async(context):
yield chunk
await template_rendered.send_async(
app,
_sync_wrapper=app.ensure_async,
_sync_wrapper=app.ensure_async, # type: ignore[arg-type]
template=template,
context=context, # type: ignore
context=context,
)

# If a request context is active, keep it while generating.
Expand Down
6 changes: 1 addition & 5 deletions src/quart/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Any
from typing import AnyStr
from typing import cast
from typing import Literal
from typing import overload
from typing import TYPE_CHECKING
from urllib.parse import unquote
Expand All @@ -28,11 +29,6 @@
if TYPE_CHECKING:
from ..app import Quart # noqa

try:
from typing import Literal
except ImportError:
from typing_extensions import Literal # type: ignore

sentinel = object()


Expand Down
7 changes: 4 additions & 3 deletions src/quart/typing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os
import sys
from collections.abc import AsyncGenerator
from collections.abc import Awaitable
from collections.abc import Iterator
Expand All @@ -26,10 +27,10 @@

from .datastructures import FileStorage

try:
if sys.version_info >= (3, 10):
from typing import Protocol
except ImportError:
from typing_extensions import Protocol # type: ignore
else:
from typing_extensions import Protocol

if TYPE_CHECKING:
from werkzeug.datastructures import Authorization # noqa: F401
Expand Down
6 changes: 1 addition & 5 deletions src/quart/wrappers/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections.abc import Generator
from typing import Any
from typing import Callable
from typing import Literal
from typing import NoReturn
from typing import overload

Expand All @@ -21,11 +22,6 @@
from ..globals import current_app
from .base import BaseRequestWebsocket

try:
from typing import Literal
except ImportError:
from typing_extensions import Literal # type: ignore

SERVER_PUSH_HEADERS_TO_COPY = {
"accept",
"accept-encoding",
Expand Down
6 changes: 1 addition & 5 deletions src/quart/wrappers/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from os import PathLike
from types import TracebackType
from typing import Any
from typing import Literal
from typing import overload
from typing import TYPE_CHECKING

Expand All @@ -32,11 +33,6 @@
if TYPE_CHECKING:
from .request import Request

try:
from typing import Literal
except ImportError:
from typing_extensions import Literal # type: ignore


class ResponseBody(ABC):
"""Base class wrapper for response body data.
Expand Down

0 comments on commit 8139d96

Please sign in to comment.