Home / File/ utils.py — anthropic-sdk-python Source File

utils.py — anthropic-sdk-python Source File

Architecture documentation for utils.py, a python file in the anthropic-sdk-python codebase. 7 imports, 0 dependents.

File python AnthropicClient SyncAPI 7 imports 4 functions

Entity Profile

Dependency Diagram

graph LR
  fd640620_de49_331e_578e_1d363467c440["utils.py"]
  3ee9df07_3d82_3549_bc64_a6db4e026506["io"]
  fd640620_de49_331e_578e_1d363467c440 --> 3ee9df07_3d82_3549_bc64_a6db4e026506
  506d0594_2a0d_4f14_1041_ed428dcfcac8["inspect"]
  fd640620_de49_331e_578e_1d363467c440 --> 506d0594_2a0d_4f14_1041_ed428dcfcac8
  89ddcdd7_3ae1_4c7b_41bb_9f1e25f16875["typing"]
  fd640620_de49_331e_578e_1d363467c440 --> 89ddcdd7_3ae1_4c7b_41bb_9f1e25f16875
  37c05070_ca59_d596_7250_de9d1939227f["typing_extensions"]
  fd640620_de49_331e_578e_1d363467c440 --> 37c05070_ca59_d596_7250_de9d1939227f
  a0b28eb3_d4e0_bb21_6a83_ba005c9793f4["rich"]
  fd640620_de49_331e_578e_1d363467c440 --> a0b28eb3_d4e0_bb21_6a83_ba005c9793f4
  cde8421b_93c7_41e4_d69d_2a3f1bade2f2["pytest"]
  fd640620_de49_331e_578e_1d363467c440 --> cde8421b_93c7_41e4_d69d_2a3f1bade2f2
  21de8837_7dae_989e_fdbb_1415c0770d90["pydantic"]
  fd640620_de49_331e_578e_1d363467c440 --> 21de8837_7dae_989e_fdbb_1415c0770d90
  style fd640620_de49_331e_578e_1d363467c440 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from __future__ import annotations

import io
import inspect
from typing import Any, Iterable
from typing_extensions import TypeAlias

import rich
import pytest
import pydantic

ReprArgs: TypeAlias = "Iterable[tuple[str | None, Any]]"


def print_obj(obj: object, monkeypatch: pytest.MonkeyPatch) -> str:
    """Pretty print an object to a string"""

    # monkeypatch pydantic model printing so that model fields
    # are always printed in the same order so we can reliably
    # use this for snapshot tests
    original_repr = pydantic.BaseModel.__repr_args__

    def __repr_args__(self: pydantic.BaseModel) -> ReprArgs:
        return sorted(original_repr(self), key=lambda arg: arg[0] or arg)

    def __repr_name__(self: pydantic.BaseModel) -> str:
        # Drop generic parameters from the name
        # e.g. `GenericModel[Location]` -> `GenericModel`
        return self.__class__.__name__.split("[", maxsplit=1)[0]

    with monkeypatch.context() as m:
        m.setattr(pydantic.BaseModel, "__repr_args__", __repr_args__)
        m.setattr(pydantic.BaseModel, "__repr_name__", __repr_name__)

        string = rich_print_str(obj)

        # we remove all `fn_name.<locals>.` occurrences
        # so that we can share the same snapshots between
        # pydantic v1 and pydantic v2 as their output for
        # generic models differs, e.g.
        #
        # v2: `GenericModel[test_generic_model.<locals>.Location]`
        # v1: `GenericModel[Location]`
        return clear_locals(string, stacklevel=2)


def get_caller_name(*, stacklevel: int = 1) -> str:
    frame = inspect.currentframe()
    assert frame is not None

    for i in range(stacklevel):
        frame = frame.f_back
        assert frame is not None, f"no {i}th frame"

    return frame.f_code.co_name


def clear_locals(string: str, *, stacklevel: int) -> str:
    caller = get_caller_name(stacklevel=stacklevel + 1)
    return string.replace(f"{caller}.<locals>.", "")


def rich_print_str(obj: object) -> str:
    """Like `rich.print()` but returns the string instead"""
    buf = io.StringIO()

    console = rich.console.Console(file=buf, width=120)
    console.out(obj)

    return buf.getvalue()

Subdomains

Dependencies

  • inspect
  • io
  • pydantic
  • pytest
  • rich
  • typing
  • typing_extensions

Frequently Asked Questions

What does utils.py do?
utils.py is a source file in the anthropic-sdk-python codebase, written in python. It belongs to the AnthropicClient domain, SyncAPI subdomain.
What functions are defined in utils.py?
utils.py defines 4 function(s): clear_locals, get_caller_name, print_obj, rich_print_str.
What does utils.py depend on?
utils.py imports 7 module(s): inspect, io, pydantic, pytest, rich, typing, typing_extensions.
Where is utils.py in the architecture?
utils.py is located at tests/lib/utils.py (domain: AnthropicClient, subdomain: SyncAPI, directory: tests/lib).

Analyze Your Own Codebase

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

Try Supermodel Free