From 94162e16493ee1136f17de45d79abdfb587a3ac7 Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Wed, 15 Apr 2026 06:12:51 +0100 Subject: [PATCH 1/4] Deduplicate httpx Timeout construction with match helper Extract _httpx_timeout() helper using match/case to build httpx.Timeout, replacing duplicated isinstance branches in HTTPXTransport and AsyncHTTPXTransport. Closes #2960 Co-Authored-By: Claude Opus 4.6 (1M context) --- src/vws/transports.py | 54 +++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/src/vws/transports.py b/src/vws/transports.py index 80aa989f..33dd39f2 100644 --- a/src/vws/transports.py +++ b/src/vws/transports.py @@ -10,6 +10,28 @@ from vws.response import Response +@beartype(conf=BeartypeConf(is_pep484_tower=True)) +def _httpx_timeout( + request_timeout: float | tuple[float, float], +) -> httpx.Timeout: + """Build an ``httpx.Timeout`` from a request timeout value.""" + match request_timeout: + case (connect, read): + return httpx.Timeout( + connect=connect, + read=read, + write=None, + pool=None, + ) + case float() | int() as timeout: + return httpx.Timeout( + connect=timeout, + read=timeout, + write=None, + pool=None, + ) + + @runtime_checkable class Transport(Protocol): """Protocol for HTTP transports used by VWS clients. @@ -149,21 +171,7 @@ def __call__( Returns: A Response populated from the httpx response. """ - if isinstance(request_timeout, tuple): - connect_timeout, read_timeout = request_timeout - httpx_timeout = httpx.Timeout( - connect=connect_timeout, - read=read_timeout, - write=None, - pool=None, - ) - else: - httpx_timeout = httpx.Timeout( - connect=request_timeout, - read=request_timeout, - write=None, - pool=None, - ) + httpx_timeout = _httpx_timeout(request_timeout=request_timeout) httpx_response = self._client.request( method=method, @@ -272,21 +280,7 @@ async def __call__( Returns: A Response populated from the httpx response. """ - if isinstance(request_timeout, tuple): - connect_timeout, read_timeout = request_timeout - httpx_timeout = httpx.Timeout( - connect=connect_timeout, - read=read_timeout, - write=None, - pool=None, - ) - else: - httpx_timeout = httpx.Timeout( - connect=request_timeout, - read=request_timeout, - write=None, - pool=None, - ) + httpx_timeout = _httpx_timeout(request_timeout=request_timeout) httpx_response = await self._client.request( method=method, From 61bc5e79462d0ac8526e370b9d6d91651c479236 Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Wed, 15 Apr 2026 06:13:29 +0100 Subject: [PATCH 2/4] Add exhaustive match default to satisfy ty type checker Co-Authored-By: Claude Opus 4.6 (1M context) --- src/vws/transports.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/vws/transports.py b/src/vws/transports.py index 33dd39f2..4c06f5ad 100644 --- a/src/vws/transports.py +++ b/src/vws/transports.py @@ -30,6 +30,9 @@ def _httpx_timeout( write=None, pool=None, ) + case _: # pragma: no cover + msg = f"Unexpected timeout type: {type(request_timeout)}" + raise TypeError(msg) @runtime_checkable From 141fd5759b493bab69966a5189b45193c30c3565 Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Wed, 15 Apr 2026 06:18:19 +0100 Subject: [PATCH 3/4] Use assert_never instead of pragma: no cover for exhaustive match Co-Authored-By: Claude Opus 4.6 (1M context) --- src/vws/transports.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/vws/transports.py b/src/vws/transports.py index 4c06f5ad..a907f924 100644 --- a/src/vws/transports.py +++ b/src/vws/transports.py @@ -1,7 +1,7 @@ """HTTP transport implementations for VWS clients.""" from collections.abc import Awaitable -from typing import Protocol, Self, runtime_checkable +from typing import Protocol, Self, assert_never, runtime_checkable import httpx import requests @@ -30,9 +30,8 @@ def _httpx_timeout( write=None, pool=None, ) - case _: # pragma: no cover - msg = f"Unexpected timeout type: {type(request_timeout)}" - raise TypeError(msg) + case _ as unreachable: + assert_never(unreachable) @runtime_checkable From 4940fcba38bb2d2512d3f04b7e1b94bface6a87e Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Wed, 15 Apr 2026 06:19:35 +0100 Subject: [PATCH 4/4] Use assert_never for exhaustive match, suppress ty/pyrefly @Todo issues Co-Authored-By: Claude Opus 4.6 (1M context) --- src/vws/transports.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vws/transports.py b/src/vws/transports.py index a907f924..5dd9db23 100644 --- a/src/vws/transports.py +++ b/src/vws/transports.py @@ -31,7 +31,7 @@ def _httpx_timeout( pool=None, ) case _ as unreachable: - assert_never(unreachable) + assert_never(unreachable) # pyrefly: ignore[bad-argument-type] # ty: ignore[type-assertion-failure] @runtime_checkable