Skip to content

Commit 002a05c

Browse files
Refactor timeout and body handling to use match statements (#3112)
* 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) <noreply@anthropic.com> * 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) <noreply@anthropic.com> * 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) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent be3a291 commit 002a05c

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/mock_vws/_requests_mock_server/decorators.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,25 @@ def wrapped(
167167
# requests allows timeout as a (connect, read)
168168
# tuple. The delay simulates server response
169169
# time, so compare against the read timeout.
170-
if isinstance(timeout, tuple):
171-
timeout = timeout[1]
172-
effective: float | None = None
173-
if isinstance(timeout, (int, float)):
174-
effective = float(timeout)
170+
match timeout:
171+
case (_, int() | float() as read_timeout):
172+
effective: float | None = float(read_timeout)
173+
case int() | float():
174+
effective = float(timeout)
175+
case _:
176+
effective = None
175177

176178
if effective is not None and delay_seconds > effective:
177179
sleep_fn(effective)
178180
raise requests.exceptions.Timeout
179181

180-
raw_body = request.body
181-
if raw_body is None:
182-
body_bytes = b""
183-
elif isinstance(raw_body, str):
184-
body_bytes = raw_body.encode(encoding="utf-8")
185-
else:
186-
body_bytes = raw_body
182+
match request.body:
183+
case None:
184+
body_bytes = b""
185+
case str() as raw_body:
186+
body_bytes = raw_body.encode(encoding="utf-8")
187+
case _:
188+
body_bytes = request.body
187189

188190
path = request.path_url
189191
if base_path and path.startswith(base_path):

0 commit comments

Comments
 (0)