MCPcopy
hub / github.com/pydantic/pydantic / transform

Method transform

pydantic/mypy.py:488–522  ·  view source on GitHub ↗

Configures the BaseModel subclass according to the plugin settings. In particular: * determines the model config and fields, * adds a fields-aware signature for the initializer and construct methods * freezes the class if frozen = True * stores the fields, c

(self)

Source from the content-addressed store, hash-verified

486 self.plugin_config = plugin_config
487
488 def transform(self) -> bool:
489 """Configures the BaseModel subclass according to the plugin settings.
490
491 In particular:
492
493 * determines the model config and fields,
494 * adds a fields-aware signature for the initializer and construct methods
495 * freezes the class if frozen = True
496 * stores the fields, config, and if the class is settings in the mypy metadata for access by subclasses
497 """
498 info = self._cls.info
499 is_a_root_model = is_root_model(info)
500 config = self.collect_config()
501 fields, class_vars = self.collect_fields_and_class_vars(config, is_a_root_model)
502 if fields is None or class_vars is None:
503 # Some definitions are not ready. We need another pass.
504 return False
505 for field in fields:
506 if field.type is None:
507 return False
508
509 is_settings = info.has_base(BASESETTINGS_FULLNAME)
510 self.add_initializer(fields, config, is_settings, is_a_root_model)
511 self.add_model_construct_method(fields, config, is_settings, is_a_root_model)
512 self.set_frozen(fields, self._api, frozen=config.frozen is True)
513
514 self.adjust_decorator_signatures()
515
516 info.metadata[METADATA_KEY] = {
517 'fields': {field.name: field.serialize() for field in fields},
518 'class_vars': {class_var.name: class_var.serialize() for class_var in class_vars},
519 'config': config.get_values_dict(),
520 }
521
522 return True
523
524 def adjust_decorator_signatures(self) -> None:
525 """When we decorate a function `f` with `pydantic.validator(...)`, `pydantic.field_validator`

Calls 9

collect_configMethod · 0.95
add_initializerMethod · 0.95
set_frozenMethod · 0.95
is_root_modelFunction · 0.85
get_values_dictMethod · 0.80
serializeMethod · 0.45