MCPcopy
hub / github.com/pydantic/pydantic / model_construct

Method model_construct

pydantic/main.py:317–391  ·  view source on GitHub ↗

Creates a new instance of the `Model` class with validated data. Creates a new model setting `__dict__` and `__pydantic_fields_set__` from trusted or pre-validated data. Default values are respected, but no other validation is performed. !!! note `model_construc

(cls, _fields_set: set[str] | None = None, **values: Any)

Source from the content-addressed store, hash-verified

315
316 @classmethod
317 def model_construct(cls, _fields_set: set[str] | None = None, **values: Any) -> Self: # noqa: C901
318 """Creates a new instance of the `Model` class with validated data.
319
320 Creates a new model setting `__dict__` and `__pydantic_fields_set__` from trusted or pre-validated data.
321 Default values are respected, but no other validation is performed.
322
323 !!! note
324 `model_construct()` generally respects the `model_config.extra` setting on the provided model.
325 That is, if `model_config.extra == 'allow'`, then all extra passed values are added to the model instance's `__dict__`
326 and `__pydantic_extra__` fields. If `model_config.extra == 'ignore'` (the default), then all extra passed values are ignored.
327 Because no validation is performed with a call to `model_construct()`, having `model_config.extra == 'forbid'` does not result in
328 an error if extra values are passed, but they will be ignored.
329
330 Args:
331 _fields_set: A set of field names that were originally explicitly set during instantiation. If provided,
332 this is directly used for the [`model_fields_set`][pydantic.BaseModel.model_fields_set] attribute.
333 Otherwise, the field names from the `values` argument will be used.
334 values: Trusted or pre-validated data dictionary.
335
336 Returns:
337 A new instance of the `Model` class with validated data.
338 """
339 m = cls.__new__(cls)
340 fields_values: dict[str, Any] = {}
341 fields_set = set()
342
343 for name, field in cls.__pydantic_fields__.items():
344 if field.alias is not None and field.alias in values:
345 fields_values[name] = values.pop(field.alias)
346 fields_set.add(name)
347
348 if (name not in fields_set) and (field.validation_alias is not None):
349 validation_aliases: list[str | AliasPath] = (
350 field.validation_alias.choices
351 if isinstance(field.validation_alias, AliasChoices)
352 else [field.validation_alias]
353 )
354
355 for alias in validation_aliases:
356 if isinstance(alias, str) and alias in values:
357 fields_values[name] = values.pop(alias)
358 fields_set.add(name)
359 break
360 elif isinstance(alias, AliasPath):
361 value = alias.search_dict_for_path(values)
362 if value is not PydanticUndefined:
363 fields_values[name] = value
364 fields_set.add(name)
365 break
366
367 if name not in fields_set:
368 if name in values:
369 fields_values[name] = values.pop(name)
370 fields_set.add(name)
371 elif not field.is_required():
372 fields_values[name] = field.get_default(call_default_factory=True, validated_data=fields_values)
373 if _fields_set is None:
374 _fields_set = fields_set

Calls 7

search_dict_for_pathMethod · 0.80
is_requiredMethod · 0.80
__new__Method · 0.45
itemsMethod · 0.45
get_defaultMethod · 0.45
getMethod · 0.45
model_post_initMethod · 0.45