From 371555ad7b6f9a4fc3719e0a57907dcdcc02dfe6 Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Wed, 15 Apr 2026 06:28:46 +0100 Subject: [PATCH 1/3] Refactor timeout and body handling to use match statements Replace isinstance chains with match/case for timeout normalization and request body conversion in _wrap_callback. Closes #3110 Co-Authored-By: Claude Opus 4.6 (1M context) --- .../_requests_mock_server/decorators.py | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/mock_vws/_requests_mock_server/decorators.py b/src/mock_vws/_requests_mock_server/decorators.py index 26a0b7967..2af559d7d 100644 --- a/src/mock_vws/_requests_mock_server/decorators.py +++ b/src/mock_vws/_requests_mock_server/decorators.py @@ -167,23 +167,25 @@ def wrapped( # requests allows timeout as a (connect, read) # tuple. The delay simulates server response # time, so compare against the read timeout. - if isinstance(timeout, tuple): - timeout = timeout[1] - effective: float | None = None - if isinstance(timeout, (int, float)): - effective = float(timeout) + match timeout: + case tuple(): + effective: float | None = float(timeout[1]) + case int() | float(): + effective = float(timeout) + case _: + effective = None if effective is not None and delay_seconds > effective: sleep_fn(effective) raise requests.exceptions.Timeout - raw_body = request.body - if raw_body is None: - body_bytes = b"" - elif isinstance(raw_body, str): - body_bytes = raw_body.encode(encoding="utf-8") - else: - body_bytes = raw_body + match request.body: + case None: + body_bytes = b"" + case str() as raw_body: + body_bytes = raw_body.encode(encoding="utf-8") + case bytes() as raw_body: + body_bytes = raw_body path = request.path_url if base_path and path.startswith(base_path): From b4cfed03e2398b43b1393ab99ff181e9b9c42ebc Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Wed, 15 Apr 2026 06:44:48 +0100 Subject: [PATCH 2/3] Use wildcard case for body to preserve original fallthrough behavior The original else branch handled any type, not just bytes(). Using case _ preserves that catch-all semantics. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/mock_vws/_requests_mock_server/decorators.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mock_vws/_requests_mock_server/decorators.py b/src/mock_vws/_requests_mock_server/decorators.py index 2af559d7d..9e6f4f9ed 100644 --- a/src/mock_vws/_requests_mock_server/decorators.py +++ b/src/mock_vws/_requests_mock_server/decorators.py @@ -184,8 +184,8 @@ def wrapped( body_bytes = b"" case str() as raw_body: body_bytes = raw_body.encode(encoding="utf-8") - case bytes() as raw_body: - body_bytes = raw_body + case _: + body_bytes = request.body path = request.path_url if base_path and path.startswith(base_path): From 3d57724cd9bb86e2dcba56154c2c00b31c8f69ab Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Wed, 15 Apr 2026 07:02:51 +0100 Subject: [PATCH 3/3] Handle (connect, None) timeout tuples correctly Match only int/float read values in the tuple case, so (5, None) falls through to effective = None instead of raising TypeError. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/mock_vws/_requests_mock_server/decorators.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mock_vws/_requests_mock_server/decorators.py b/src/mock_vws/_requests_mock_server/decorators.py index 9e6f4f9ed..ae699a3a1 100644 --- a/src/mock_vws/_requests_mock_server/decorators.py +++ b/src/mock_vws/_requests_mock_server/decorators.py @@ -168,8 +168,8 @@ def wrapped( # tuple. The delay simulates server response # time, so compare against the read timeout. match timeout: - case tuple(): - effective: float | None = float(timeout[1]) + case (_, int() | float() as read_timeout): + effective: float | None = float(read_timeout) case int() | float(): effective = float(timeout) case _: