MCPcopy
hub / github.com/pydantic/pydantic / PydanticModelTransformer

Class PydanticModelTransformer

pydantic/mypy.py:457–1191  ·  view source on GitHub ↗

Transform the BaseModel subclass according to the plugin settings. Attributes: tracked_config_fields: A set of field configs that the plugin has to track their value.

Source from the content-addressed store, hash-verified

455
456
457class PydanticModelTransformer:
458 """Transform the BaseModel subclass according to the plugin settings.
459
460 Attributes:
461 tracked_config_fields: A set of field configs that the plugin has to track their value.
462 """
463
464 tracked_config_fields: set[str] = {
465 'extra',
466 'frozen',
467 'from_attributes',
468 'populate_by_name',
469 'validate_by_alias',
470 'validate_by_name',
471 'alias_generator',
472 'strict',
473 }
474
475 def __init__(
476 self,
477 cls: ClassDef,
478 reason: Expression | Statement,
479 api: SemanticAnalyzerPluginInterface,
480 plugin_config: PydanticPluginConfig,
481 ) -> None:
482 self._cls = cls
483 self._reason = reason
484 self._api = api
485
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()

Calls

no outgoing calls

Tested by

no test coverage detected