Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/anthropic/_utils/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,16 @@ def deepcopy_minimal(item: _T) -> _T:

- mappings, e.g. `dict`
- list
- tuple

This is done for performance reasons.
"""
if is_mapping(item):
return cast(_T, {k: deepcopy_minimal(v) for k, v in item.items()})
if is_list(item):
return cast(_T, [deepcopy_minimal(entry) for entry in item])
if is_tuple(item):
return cast(_T, tuple(deepcopy_minimal(entry) for entry in item))
return item


Expand Down
35 changes: 31 additions & 4 deletions tests/test_deepcopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ def test_nested_list() -> None:
assert_different_identities(obj1[1], obj2[1])


def test_simple_tuple() -> None:
obj1 = ("a", "b")
obj2 = deepcopy_minimal(obj1)
assert_different_identities(obj1, obj2)
assert obj1[0] is obj2[0] # immutable strings are same object
assert obj1[1] is obj2[1]


def test_nested_tuple() -> None:
obj1 = ("a", {"bar": True})
obj2 = deepcopy_minimal(obj1)
assert_different_identities(obj1, obj2)
assert_different_identities(obj1[1], obj2[1])
assert obj1[0] is obj2[0] # immutable string preserved


class MyObject: ...


Expand All @@ -52,7 +68,18 @@ def test_ignores_other_types() -> None:
assert_different_identities(obj1, obj2)
assert obj1["foo"] is my_obj

# tuples
obj3 = ("a", "b")
obj4 = deepcopy_minimal(obj3)
assert obj3 is obj4
# tuples with lists inside - list should be copied
inner_list = [1, {"x": "y"}]
obj7 = ("z", inner_list)
obj8 = deepcopy_minimal(obj7)
assert obj7 is not obj8 # new tuple created
assert obj7[1] is not obj8[1] # list inside is copied
assert obj7[1][1] is not obj8[1][1] # dict inside that list is also copied

# deeply nested: tuple -> dict -> list -> dict
obj9 = ({"items": [{"name": "a"}]},)
obj10 = deepcopy_minimal(obj9)
assert obj9 is not obj10
assert obj9[0] is not obj10[0] # dict in tuple copied
assert obj9[0]["items"] is not obj10[0]["items"] # list in dict copied
assert obj9[0]["items"][0] is not obj10[0]["items"][0] # dict in list copied