MCPcopy Create free account
hub / github.com/anthropics/anthropic-sdk-python / test_deep_nesting

Function test_deep_nesting

tests/test_prepare.py:726–751  ·  view source on GitHub ↗
(send: Send, client: Anthropic | AsyncAnthropic, monkeypatch: pytest.MonkeyPatch)

Source from the content-addressed store, hash-verified

724
725
726async def test_deep_nesting(send: Send, client: Anthropic | AsyncAnthropic, monkeypatch: pytest.MonkeyPatch) -> None:
727 monkeypatch.setattr(MARKERS_ATTR, MARKERS)
728
729 # untyped JSON fields (`input_schema`, ...) can nest past the recursion limit
730 def nested(depth: int) -> object:
731 data: object = {"type": "base64", "dropped": omit, "data": SAMPLE_FILE_PATH}
732 for i in range(depth):
733 data = {"k": data} if i % 2 else (data,)
734 return data
735
736 depth = 5 * sys.getrecursionlimit()
737 if isinstance(client, Anthropic):
738 prepared = prepare_request_data(nested(depth), location="body")
739 else:
740 prepared = await async_prepare_request_data(nested(depth), location="body")
741 for i in reversed(range(depth)):
742 prepared = prepared["k"] if i % 2 else prepared[0]
743 assert prepared == {"type": "base64", "data": SAMPLE_FILE_BASE64}
744
745 # sent end to end from less deep only because `json` itself recurses once per level before Python 3.12
746 depth = sys.getrecursionlimit() - 100
747 await send({"metadata": nested(depth)})
748
749 assert send.request.content.endswith(
750 b'{"type":"base64","data":"SGVsbG8sIHdvcmxkIQo="}' + b"]}" * (depth // 2) + b"}"
751 )
752
753
754async def test_circular_reference(send: Send) -> None:

Callers

nothing calls this directly

Calls 4

prepare_request_dataFunction · 0.90
nestedFunction · 0.85
sendFunction · 0.85

Tested by

no test coverage detected