fix: handle tuples in deepcopy_minimal to prevent dict mutation#1226
Open
giulio-leone wants to merge 1 commit intoanthropics:mainfrom
Open
fix: handle tuples in deepcopy_minimal to prevent dict mutation#1226giulio-leone wants to merge 1 commit intoanthropics:mainfrom
giulio-leone wants to merge 1 commit intoanthropics:mainfrom
Conversation
`deepcopy_minimal` only recursed into dicts and lists, ignoring tuples. When a `FileTypes` tuple like `(name, content, mime, headers_dict)` was passed to `files.beta.upload`, the headers dict inside the tuple was shared between the original and the copy, causing in-place mutation of the caller's data. Add tuple handling so dicts nested inside tuples are properly copied. Fixes anthropics#1202 Signed-off-by: Giulio Leone <6887247+giulio-leone@users.noreply.github.com>
Author
|
Friendly ping — CI is green, tests pass, rebased on latest. Ready for review whenever convenient. Happy to address any feedback. 🙏 |
Author
|
Added a real runtime validation pass for this fix using the public Results:
|
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_minimaldoes not recurse into tuples, causingfiles.beta.uploadto mutate the caller's headers dict in-place.Root Cause
deepcopy_minimalonly handlesdictandlist, returning everything else as-is. When aFileTypestuple like(name, content, mime, headers_dict)is passed, the tuple is shared by reference, so any downstream mutation of the headers dict (element [3]) leaks back to the caller.Fix
Add tuple handling to
deepcopy_minimal:This ensures dicts (and lists) nested inside tuples are recursively copied.
Tests
obj3 is obj4identity, now checks proper copy)test_tuple_with_nested_dict— verifies the exactFileTypesscenario from Use ofdeepcopy_minimalinfiles.beta.uploadmutates dict in place #1202: mutating the copy's headers dict does not affect the original