From 3b74be6d46c36e6ad709f8124e1947305d761782 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Thu, 22 Aug 2024 16:00:04 +0200 Subject: [PATCH] Fix new Ruff lint rules --- src/pyodide/internal/asgi.py | 13 +++++++++++-- src/pyodide/internal/patches/httpx.py | 2 +- .../topLevelEntropy/entropy_import_context.py | 5 +++-- tools/cross/format.py | 3 ++- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/pyodide/internal/asgi.py b/src/pyodide/internal/asgi.py index 060ed74729f..5aacd1e37d3 100644 --- a/src/pyodide/internal/asgi.py +++ b/src/pyodide/internal/asgi.py @@ -7,6 +7,15 @@ ASGI = {"spec_version": "2.0", "version": "3.0"} +background_tasks = set() + + +def run_in_background(coro): + fut = ensure_future(coro) + background_tasks.add(fut) + fut.add_done_callback(background_tasks.discard) + + @Depends async def env(request: Request): return request.scope["env"] @@ -78,7 +87,7 @@ async def send(got): print("Application shutdown complete") raise RuntimeError(f"Unexpected lifespan event {got['type']}") - ensure_future( + run_in_background( app( { "asgi": ASGI, @@ -184,7 +193,7 @@ async def ws_receive(): return received env = {} - ensure_future(app(request_to_scope(req, env, ws=True), ws_receive, ws_send)) + run_in_background(app(request_to_scope(req, env, ws=True), ws_receive, ws_send)) return Response.new(None, status=101, webSocket=client) diff --git a/src/pyodide/internal/patches/httpx.py b/src/pyodide/internal/patches/httpx.py index 1ef76c61e5b..9e4ff509a1c 100644 --- a/src/pyodide/internal/patches/httpx.py +++ b/src/pyodide/internal/patches/httpx.py @@ -53,7 +53,7 @@ async def _send_single_request(self, request: Request) -> Response: await timer.async_start() if not isinstance(request.stream, AsyncByteStream): - raise RuntimeError( + raise TypeError( "Attempted to send an sync request with an AsyncClient instance." ) diff --git a/src/pyodide/internal/topLevelEntropy/entropy_import_context.py b/src/pyodide/internal/topLevelEntropy/entropy_import_context.py index e4b66431834..e9594beb146 100644 --- a/src/pyodide/internal/topLevelEntropy/entropy_import_context.py +++ b/src/pyodide/internal/topLevelEntropy/entropy_import_context.py @@ -26,7 +26,8 @@ "numpy.random.mtrand", "tempfile", "aiohttp.http_websocket", -] + RUST_PACKAGES + *RUST_PACKAGES, +] # Control number of allowed entropy calls. @@ -76,7 +77,7 @@ def get_entropy_import_context(name): # Initial import needs one entropy call to initialize # std::collections::HashMap hash seed return rust_package_context - raise Exception(f"Missing context for {name}") + raise RuntimeError(f"Missing context for {name}") @contextmanager diff --git a/tools/cross/format.py b/tools/cross/format.py index f43ce957b8b..96f9bcdc464 100644 --- a/tools/cross/format.py +++ b/tools/cross/format.py @@ -8,6 +8,7 @@ from argparse import ArgumentParser, Namespace from dataclasses import dataclass from pathlib import Path +from sys import exit from typing import Callable, Optional CLANG_FORMAT = os.environ.get("CLANG_FORMAT", "clang-format") @@ -80,7 +81,7 @@ def check_clang_format() -> None: exit(1) except FileNotFoundError: # Clang-format is not in the PATH - logging.error("clang-format not found in the PATH") + logging.exception("clang-format not found in the PATH") exit(1)