diff --git a/.ruff.toml b/.ruff.toml index 7d379ff4623..fb6aec7ca3f 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -8,14 +8,22 @@ select = [ "E", # pycodestyles "W", # pycodestyles "F", # pyflakes + "FURB", # refurb "I", # isort + "PERF", # Perflint "PGH", # pygrep-hooks "PLC", # pylint conventions "PLE", # pylint errors + "PLR", # pylint refactor + "PTH", # Use pathlib + "RUF", # Ruff rules + "TRY", # Exception related lints "UP", # pyupgrade ] ignore = [ "E402", # module import not at top of file + "PLR2004", # Magic value used in comparison + "TRY003", # Avoid specifying long messages outside the exception class "UP038", # Use X | Y in isinstance check instead of (X, Y) ] 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)