fix: handle tuples in deepcopy_minimal to prevent dict mutation#1245
Open
passionworkeer wants to merge 2 commits intoanthropics:mainfrom
Open
fix: handle tuples in deepcopy_minimal to prevent dict mutation#1245passionworkeer wants to merge 2 commits intoanthropics:mainfrom
passionworkeer wants to merge 2 commits intoanthropics:mainfrom
Conversation
The deepcopy_minimal function only handled dict and list types, but not tuples. When a tuple containing a dict was passed, the dict inside the tuple could be mutated in-place during processing. This fix adds tuple handling to recursively copy tuple elements, preventing in-place mutations of dicts or lists nested inside tuples. Closes anthropics#1202
There was a problem hiding this comment.
Pull request overview
Fixes deepcopy_minimal so it also deep-copies tuples (recursively), preventing in-place mutation of dicts/lists nested inside tuples (e.g., in files.beta.upload file tuples).
Changes:
- Documented tuple support in
deepcopy_minimal’s docstring. - Added recursive tuple handling to
deepcopy_minimal.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/anthropic/_utils/_utils.py
Outdated
Comment on lines
+192
to
+193
| if isinstance(item, tuple): | ||
| return cast(_T, tuple(deepcopy_minimal(entry) for entry in item)) |
src/anthropic/_utils/_utils.py
Outdated
| 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 isinstance(item, tuple): |
- Use is_tuple() TypeGuard helper instead of isinstance check - Update test_ignores_other_types to reflect new tuple behavior - Add assertion that dict nested inside tuple is copied
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 an issue where deepcopy_minimal was mutating dicts nested inside tuples.
Problem
The deepcopy_minimal function in _utils.py only handled dict and list types. When a tuple containing a dict was passed to functions like iles.beta.upload, the dict inside the tuple could be mutated in-place during processing.
Solution
Added tuple handling to deepcopy_minimal:
python if isinstance(item, tuple): return cast(_T, tuple(deepcopy_minimal(entry) for entry in item))This ensures that any dicts or lists nested inside tuples are properly deep-copied, preventing in-place mutations.
Changes
Closes #1202