MCPcopy
hub / github.com/pydantic/pydantic / __deepcopy__

Method __deepcopy__

pydantic/main.py:990–1009  ·  view source on GitHub ↗

Returns a deep copy of the model.

(self, memo: dict[int, Any] | None = None)

Source from the content-addressed store, hash-verified

988 return m
989
990 def __deepcopy__(self, memo: dict[int, Any] | None = None) -> Self:
991 """Returns a deep copy of the model."""
992 cls = type(self)
993 m = cls.__new__(cls)
994 _object_setattr(m, '__dict__', deepcopy(self.__dict__, memo=memo))
995 _object_setattr(m, '__pydantic_extra__', deepcopy(self.__pydantic_extra__, memo=memo))
996 # This next line doesn't need a deepcopy because __pydantic_fields_set__ is a set[str],
997 # and attempting a deepcopy would be marginally slower.
998 _object_setattr(m, '__pydantic_fields_set__', copy(self.__pydantic_fields_set__))
999
1000 if not hasattr(self, '__pydantic_private__') or self.__pydantic_private__ is None:
1001 _object_setattr(m, '__pydantic_private__', None)
1002 else:
1003 _object_setattr(
1004 m,
1005 '__pydantic_private__',
1006 deepcopy({k: v for k, v in self.__pydantic_private__.items() if v is not PydanticUndefined}, memo=memo),
1007 )
1008
1009 return m
1010
1011 if not TYPE_CHECKING:
1012 # We put `__getattr__` in a non-TYPE_CHECKING block because otherwise, mypy allows arbitrary attribute access

Callers 1

model_copyMethod · 0.95

Calls 2

__new__Method · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected