fix: handle tuples in deepcopy_minimal to prevent in-place mutation#1228
Open
MaxwellCalkin wants to merge 1 commit intoanthropics:mainfrom
Open
fix: handle tuples in deepcopy_minimal to prevent in-place mutation#1228MaxwellCalkin wants to merge 1 commit intoanthropics:mainfrom
deepcopy_minimal to prevent in-place mutation#1228MaxwellCalkin wants to merge 1 commit intoanthropics:mainfrom
Conversation
The `deepcopy_minimal` function only recursed into dicts and lists, returning tuples as-is. This caused mutable objects inside tuples (such as header dicts in FileTypes values) to be shared by reference between the original and the "copy", leading to unexpected in-place mutation of the caller's data. Add tuple handling so that mutable contents within tuples are properly copied. This affects any code path that passes FileTypes tuples through `deepcopy_minimal`, including `files.beta.upload()`. Fixes anthropics#1202
52d29e4 to
e1f4121
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1202.
deepcopy_minimalonly recurses intodictandlist, returning all other types (includingtuple) as-is. This causes mutable objects nested inside tuples to be shared by reference between the original and the "copy", leading to unexpected in-place mutation of the caller's data.The most common case is
FileTypesvalues of the form(filename, content, content_type, headers_dict)passed tofiles.beta.upload(). The headers dict (4th tuple element) is not copied, so the SDK's internal processing mutates the caller's original dict.Changes
src/anthropic/_utils/_utils.py: Addtuplehandling todeepcopy_minimalusing the existingis_tuple()helper, so mutable contents within tuples are properly deep-copied.tests/test_deepcopy.py: Add three test cases covering:Before / After