Home / Function/ test_copy_build_request() — anthropic-sdk-python Function Reference

test_copy_build_request() — anthropic-sdk-python Function Reference

Architecture documentation for the test_copy_build_request() function in test_client.py from the anthropic-sdk-python codebase.

Function python AnthropicClient AsyncAPI calls 1 called by 1

Entity Profile

Dependency Diagram

graph TD
  e22034d2_309c_9ab1_a7a5_3ad92238df9e["test_copy_build_request()"]
  d53b6c34_1743_a045_4be1_3554ab153964["TestAnthropic"]
  e22034d2_309c_9ab1_a7a5_3ad92238df9e -->|defined in| d53b6c34_1743_a045_4be1_3554ab153964
  702b2cf9_feca_3e68_addc_69dd90b45e8f["test_copy_build_request()"]
  702b2cf9_feca_3e68_addc_69dd90b45e8f -->|calls| e22034d2_309c_9ab1_a7a5_3ad92238df9e
  702b2cf9_feca_3e68_addc_69dd90b45e8f["test_copy_build_request()"]
  e22034d2_309c_9ab1_a7a5_3ad92238df9e -->|calls| 702b2cf9_feca_3e68_addc_69dd90b45e8f
  style e22034d2_309c_9ab1_a7a5_3ad92238df9e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

tests/test_client.py lines 250–310

    def test_copy_build_request(self, client: Anthropic) -> None:
        options = FinalRequestOptions(method="get", url="/foo")

        def build_request(options: FinalRequestOptions) -> None:
            client_copy = client.copy()
            client_copy._build_request(options)

        # ensure that the machinery is warmed up before tracing starts.
        build_request(options)
        gc.collect()

        tracemalloc.start(1000)

        snapshot_before = tracemalloc.take_snapshot()

        ITERATIONS = 10
        for _ in range(ITERATIONS):
            build_request(options)

        gc.collect()
        snapshot_after = tracemalloc.take_snapshot()

        tracemalloc.stop()

        def add_leak(leaks: list[tracemalloc.StatisticDiff], diff: tracemalloc.StatisticDiff) -> None:
            if diff.count == 0:
                # Avoid false positives by considering only leaks (i.e. allocations that persist).
                return

            if diff.count % ITERATIONS != 0:
                # Avoid false positives by considering only leaks that appear per iteration.
                return

            for frame in diff.traceback:
                if any(
                    frame.filename.endswith(fragment)
                    for fragment in [
                        # to_raw_response_wrapper leaks through the @functools.wraps() decorator.
                        #
                        # removing the decorator fixes the leak for reasons we don't understand.
                        "anthropic/_legacy_response.py",
                        "anthropic/_response.py",
                        # pydantic.BaseModel.model_dump || pydantic.BaseModel.dict leak memory for some reason.
                        "anthropic/_compat.py",
                        # Standard library leaks we don't care about.
                        "/logging/__init__.py",
                    ]
                ):
                    return

            leaks.append(diff)

        leaks: list[tracemalloc.StatisticDiff] = []
        for diff in snapshot_after.compare_to(snapshot_before, "traceback"):
            add_leak(leaks, diff)
        if leaks:
            for leak in leaks:
                print("MEMORY LEAK:", leak)
                for frame in leak.traceback:
                    print(frame)
            raise AssertionError()

Subdomains

Frequently Asked Questions

What does test_copy_build_request() do?
test_copy_build_request() is a function in the anthropic-sdk-python codebase, defined in tests/test_client.py.
Where is test_copy_build_request() defined?
test_copy_build_request() is defined in tests/test_client.py at line 250.
What does test_copy_build_request() call?
test_copy_build_request() calls 1 function(s): test_copy_build_request.
What calls test_copy_build_request()?
test_copy_build_request() is called by 1 function(s): test_copy_build_request.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free