From 3ce83b9bd94735f7a16ca537f6c08bd354ac6fdf Mon Sep 17 00:00:00 2001 From: Zhiwei Liang Date: Tue, 7 Apr 2026 13:45:13 -0400 Subject: [PATCH 1/3] Simplify trivial code patterns Signed-off-by: Zhiwei Liang --- sanic/cli/app.py | 2 +- sanic/compat.py | 2 +- sanic/server/websockets/impl.py | 2 +- sanic/worker/process.py | 6 ++---- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/sanic/cli/app.py b/sanic/cli/app.py index 929b5cce5b..f6feb5fbd4 100644 --- a/sanic/cli/app.py +++ b/sanic/cli/app.py @@ -282,7 +282,7 @@ def _inspector(self): key, value = arg.split("=") key = key.lstrip("-") except ValueError: - value = False if arg.startswith("--no-") else True + value = not arg.startswith("--no-") key = ( arg.replace("--no-", "") .lstrip("-") diff --git a/sanic/compat.py b/sanic/compat.py index f608909b64..ab7bb49419 100644 --- a/sanic/compat.py +++ b/sanic/compat.py @@ -139,7 +139,7 @@ def stat_async(path) -> Awaitable[os.stat_result]: return trio.Path(path).stat() open_async = trio.open_file - CancelledErrors = tuple([asyncio.CancelledError, trio.Cancelled]) + CancelledErrors = (asyncio.CancelledError, trio.Cancelled) else: if PYPY_IMPLEMENTATION: pypy_os_module_patch() diff --git a/sanic/server/websockets/impl.py b/sanic/server/websockets/impl.py index d68110773b..3d80be2d42 100644 --- a/sanic/server/websockets/impl.py +++ b/sanic/server/websockets/impl.py @@ -860,7 +860,7 @@ def connection_lost(self, exc): """ The WebSocket Connection is Closed. """ - if not self.ws_proto.state == CLOSED: + if self.ws_proto.state != CLOSED: # signal to the websocket connection handler # we've lost the connection self.ws_proto.fail(code=1006) diff --git a/sanic/worker/process.py b/sanic/worker/process.py index 6d52c92576..68c98ac948 100644 --- a/sanic/worker/process.py +++ b/sanic/worker/process.py @@ -231,12 +231,10 @@ def _wait_to_terminate(self): def _add_config(self) -> bool: sig = signature(self.target) - if "config" in sig.parameters or any( + return "config" in sig.parameters or any( param.kind == param.VAR_KEYWORD for param in sig.parameters.values() - ): - return True - return False + ) class Worker: From e773b014ac11164fbdbe86beea4a8264f535a393 Mon Sep 17 00:00:00 2001 From: Zhiwei Liang Date: Wed, 8 Apr 2026 13:00:45 -0400 Subject: [PATCH 2/3] Fix bandit security scan failure with bandit 1.9.4 Signed-off-by: Zhiwei Liang --- sanic/config.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sanic/config.py b/sanic/config.py index 270017b13b..8fb183f513 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -36,7 +36,7 @@ "EVENT_AUTOREGISTER": False, "DEPRECATION_FILTER": "once", "FORWARDED_FOR_HEADER": "X-Forwarded-For", - "FORWARDED_SECRET": None, + "FORWARDED_SECRET": None, # nosec B105 "GRACEFUL_SHUTDOWN_TIMEOUT": 15.0, "GRACEFUL_TCP_CLOSE_TIMEOUT": 5.0, "INSPECTOR": False, @@ -64,7 +64,7 @@ "REQUEST_MAX_SIZE": 100_000_000, "REQUEST_TIMEOUT": 60, "RESPONSE_TIMEOUT": 60, - "TLS_CERT_PASSWORD": "", + "TLS_CERT_PASSWORD": "", # nosec B105 "TOUCHUP": _default, "USE_UVLOOP": _default, "WEBSOCKET_MAX_SIZE": 2**20, # 1 MiB From 2505df019493a06955c9245876f95bf13978e98b Mon Sep 17 00:00:00 2001 From: Zhiwei Liang Date: Wed, 8 Apr 2026 13:21:19 -0400 Subject: [PATCH 3/3] Fix typing for CancelledErrors Signed-off-by: Zhiwei Liang --- sanic/compat.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sanic/compat.py b/sanic/compat.py index ab7bb49419..a63c3bcd0f 100644 --- a/sanic/compat.py +++ b/sanic/compat.py @@ -131,6 +131,7 @@ def get_all(self, key: str): use_trio = sys.argv[0].endswith("hypercorn") and "trio" in sys.argv +CancelledErrors: tuple[type[BaseException], ...] if use_trio: # pragma: no cover import trio # type: ignore @@ -153,7 +154,7 @@ def stat_async(path) -> Awaitable[os.stat_result]: async def open_async(file, mode="r", **kwargs): return aio_open(file, mode, **kwargs) - CancelledErrors = tuple([asyncio.CancelledError]) + CancelledErrors = (asyncio.CancelledError,) def ctrlc_workaround_for_windows(app):