Skip to content

Commit bf22dfc

Browse files
test_middleware: comment fix + nonlocal counter
Two Copilot suggestions on test_middleware.py: - Comment said "more than twice" but the docstring + spec §2 say "more than once" and the test pins at N=5 calls. Aligning the comment. - Replaced the ``inner_calls = [0]`` mutable-list closure trick with ``nonlocal inner_calls`` + a plain int. Cleaner; same behavior.
1 parent 85fc9d0 commit bf22dfc

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

tests/unit/test_middleware.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,15 +294,15 @@ async def innermost(_state: Any) -> Mapping[str, Any]:
294294
assert abs(rec.duration_ms - 10.0) < 0.01
295295

296296

297-
# ===== Reentrant next: middleware can call next more than twice =====
297+
# ===== Reentrant next: middleware can call next more than once =====
298298

299299

300300
async def test_middleware_can_call_next_repeatedly() -> None:
301301
"""Per spec §2: a middleware MAY call ``next`` more than once. Retry
302302
exercises this with N=2-3 attempts; this test pins the contract
303303
independently by calling ``next`` 5 times in a loop and asserting the
304304
inner runs exactly that many times."""
305-
inner_calls = [0]
305+
inner_calls = 0
306306

307307
async def call_five_times(state: Any, next_: NextCall) -> Mapping[str, Any]:
308308
last: Mapping[str, Any] = {}
@@ -311,13 +311,14 @@ async def call_five_times(state: Any, next_: NextCall) -> Mapping[str, Any]:
311311
return last
312312

313313
async def innermost(_state: Any) -> Mapping[str, Any]:
314-
inner_calls[0] += 1
315-
return {"trace": [f"call-{inner_calls[0]}"]}
314+
nonlocal inner_calls
315+
inner_calls += 1
316+
return {"trace": [f"call-{inner_calls}"]}
316317

317318
chain = compose_chain([call_five_times], innermost)
318319
result = await chain(TraceState())
319320

320-
assert inner_calls[0] == 5
321+
assert inner_calls == 5
321322
# Final result is the partial returned from the LAST call to next.
322323
assert result == {"trace": ["call-5"]}
323324

0 commit comments

Comments
 (0)