Skip to content

Commit f209dc1

Browse files
moonbox3Copilot
andcommitted
fix(microsoft#4455): use class-level sets for deepcopy field exclusion
- SerializationMixin.__deepcopy__: check type(self).DEFAULT_EXCLUDE instead of hardcoding 'raw_representation' - Content.__deepcopy__: add _SHALLOW_COPY_FIELDS class variable and check against it instead of hardcoding - Fix tautological assertion in test (was always True) - Add second excluded field to test to verify DEFAULT_EXCLUDE is respected generically Co-authored-by: Copilot <[email protected]>
1 parent 91bede1 commit f209dc1

3 files changed

Lines changed: 25 additions & 15 deletions

File tree

‎python/packages/core/agent_framework/_serialization.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,19 @@ def __init__(self, **kwargs):
266266
INJECTABLE: ClassVar[set[str]] = set()
267267

268268
def __deepcopy__(self, memo: dict[int, Any]) -> SerializationMixin:
269-
"""Create a deep copy, preserving ``raw_representation`` by reference.
269+
"""Create a deep copy, preserving ``DEFAULT_EXCLUDE`` fields by reference.
270270
271-
``raw_representation`` may contain LLM SDK objects (e.g., proto/gRPC
272-
responses) that are not safe to deep-copy. It is kept as a shallow
273-
reference in the copy; all other attributes are deep-copied normally.
271+
Fields listed in ``DEFAULT_EXCLUDE`` may contain LLM SDK objects
272+
(e.g., proto/gRPC responses) that are not safe to deep-copy. They are
273+
kept as shallow references in the copy; all other attributes are
274+
deep-copied normally.
274275
"""
275276
cls = type(self)
276277
result = cls.__new__(cls)
277278
memo[id(self)] = result
279+
exclude = cls.DEFAULT_EXCLUDE
278280
for k, v in self.__dict__.items():
279-
if k == "raw_representation":
281+
if k in exclude:
280282
object.__setattr__(result, k, v)
281283
else:
282284
object.__setattr__(result, k, copy.deepcopy(v, memo))

‎python/packages/core/agent_framework/_types.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ class Content:
446446
`Content.from_uri()`, etc. to create instances.
447447
"""
448448

449+
_SHALLOW_COPY_FIELDS: ClassVar[set[str]] = {"raw_representation"}
450+
449451
def __init__(
450452
self,
451453
type: ContentType,
@@ -527,16 +529,17 @@ def __init__(
527529
self.approved = approved
528530

529531
def __deepcopy__(self, memo: dict[int, Any]) -> Content:
530-
"""Create a deep copy, preserving ``raw_representation`` by reference.
532+
"""Create a deep copy, preserving ``_SHALLOW_COPY_FIELDS`` by reference.
531533
532-
``raw_representation`` may contain LLM SDK objects (e.g., proto/gRPC
533-
responses) that are not safe to deep-copy.
534+
Fields listed in ``_SHALLOW_COPY_FIELDS`` may contain LLM SDK objects
535+
(e.g., proto/gRPC responses) that are not safe to deep-copy.
534536
"""
535537
cls = type(self)
536538
result = cls.__new__(cls)
537539
memo[id(self)] = result
540+
shallow = cls._SHALLOW_COPY_FIELDS
538541
for k, v in self.__dict__.items():
539-
if k == "raw_representation":
542+
if k in shallow:
540543
object.__setattr__(result, k, v)
541544
else:
542545
object.__setattr__(result, k, deepcopy(v, memo))

‎python/packages/core/tests/core/test_serializable_mixin.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -437,20 +437,25 @@ def __deepcopy__(self, memo):
437437
raise TypeError("cannot deepcopy")
438438

439439
class TestClass(SerializationMixin):
440-
DEFAULT_EXCLUDE = {"raw_representation"}
440+
DEFAULT_EXCLUDE = {"raw_representation", "other_opaque"}
441441

442-
def __init__(self, value: str, raw_representation: Any = None):
443-
self.value = value
442+
def __init__(self, items: list, raw_representation: Any = None, other_opaque: Any = None):
443+
self.items = items
444444
self.raw_representation = raw_representation
445+
self.other_opaque = other_opaque
445446

446447
raw = NonCopyable()
447-
obj = TestClass(value="hello", raw_representation=raw)
448+
opaque = NonCopyable()
449+
original_items = ["a", "b"]
450+
obj = TestClass(items=original_items, raw_representation=raw, other_opaque=opaque)
448451
cloned = copy.deepcopy(obj)
449452

450-
assert cloned.value == "hello"
453+
# Excluded fields should be the same object (shallow copy)
451454
assert cloned.raw_representation is raw
455+
assert cloned.other_opaque is opaque
452456
# Normal attributes should be independent copies
453-
assert cloned.value is not obj.value or cloned.value == obj.value
457+
assert cloned.items is not original_items
458+
assert cloned.items == ["a", "b"]
454459

455460
def test_deepcopy_deep_copies_non_excluded_fields(self):
456461
"""Test that deepcopy fully copies fields not in DEFAULT_EXCLUDE."""

0 commit comments

Comments
 (0)