MCPcopy
hub / github.com/pydantic/pydantic / model_copy

Method model_copy

pydantic/main.py:393–425  ·  view source on GitHub ↗

!!! abstract "Usage Documentation" [`model_copy`](../concepts/models.md#model-copy) Returns a copy of the model. !!! note The underlying instance's [`__dict__`][object.__dict__] attribute is copied. This might have unexpected side effects if you

(self, *, update: Mapping[str, Any] | None = None, deep: bool = False)

Source from the content-addressed store, hash-verified

391 return m
392
393 def model_copy(self, *, update: Mapping[str, Any] | None = None, deep: bool = False) -> Self:
394 """!!! abstract "Usage Documentation"
395 [`model_copy`](../concepts/models.md#model-copy)
396
397 Returns a copy of the model.
398
399 !!! note
400 The underlying instance's [`__dict__`][object.__dict__] attribute is copied. This
401 might have unexpected side effects if you store anything in it, on top of the model
402 fields (e.g. the value of [cached properties][functools.cached_property]).
403
404 Args:
405 update: Values to change/add in the new model. Note: the data is not validated
406 before creating the new model. You should trust this data.
407 deep: Set to `True` to make a deep copy of the model.
408
409 Returns:
410 New model instance.
411 """
412 copied = self.__deepcopy__() if deep else self.__copy__()
413 if update:
414 if self.model_config.get('extra') == 'allow':
415 for k, v in update.items():
416 if k in self.__pydantic_fields__:
417 copied.__dict__[k] = v
418 else:
419 if copied.__pydantic_extra__ is None:
420 copied.__pydantic_extra__ = {}
421 copied.__pydantic_extra__[k] = v
422 else:
423 copied.__dict__.update(update)
424 copied.__pydantic_fields_set__.update(update.keys())
425 return copied
426
427 def model_dump(
428 self,

Calls 6

__deepcopy__Method · 0.95
__copy__Method · 0.95
keysMethod · 0.80
getMethod · 0.45
itemsMethod · 0.45
updateMethod · 0.45