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

print_obj() — anthropic-sdk-python Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  e1a5f6ee_8492_7930_e986_7929e31489a2["print_obj()"]
  fd640620_de49_331e_578e_1d363467c440["utils.py"]
  e1a5f6ee_8492_7930_e986_7929e31489a2 -->|defined in| fd640620_de49_331e_578e_1d363467c440
  ed917af8_5483_55a8_c5e6_be9eb94d1aca["rich_print_str()"]
  e1a5f6ee_8492_7930_e986_7929e31489a2 -->|calls| ed917af8_5483_55a8_c5e6_be9eb94d1aca
  c3357c12_fcce_22ef_51b2_90f3c4331d2d["clear_locals()"]
  e1a5f6ee_8492_7930_e986_7929e31489a2 -->|calls| c3357c12_fcce_22ef_51b2_90f3c4331d2d
  style e1a5f6ee_8492_7930_e986_7929e31489a2 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

tests/lib/utils.py lines 15–44

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)

Subdomains

Defined In

Frequently Asked Questions

What does print_obj() do?
print_obj() is a function in the anthropic-sdk-python codebase, defined in tests/lib/utils.py.
Where is print_obj() defined?
print_obj() is defined in tests/lib/utils.py at line 15.
What does print_obj() call?
print_obj() calls 2 function(s): clear_locals, rich_print_str.

Analyze Your Own Codebase

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

Try Supermodel Free