MCPcopy
hub / github.com/pydantic/pydantic / set_model_fields

Function set_model_fields

pydantic/_internal/_model_construction.py:566–597  ·  view source on GitHub ↗

Collect and set `cls.__pydantic_fields__` and `cls.__class_vars__`. Args: cls: BaseModel or dataclass. config_wrapper: The config wrapper instance. ns_resolver: Namespace resolver to use when getting model annotations.

(
    cls: type[BaseModel],
    config_wrapper: ConfigWrapper,
    ns_resolver: NsResolver,
)

Source from the content-addressed store, hash-verified

564
565
566def set_model_fields(
567 cls: type[BaseModel],
568 config_wrapper: ConfigWrapper,
569 ns_resolver: NsResolver,
570) -> None:
571 """Collect and set `cls.__pydantic_fields__` and `cls.__class_vars__`.
572
573 Args:
574 cls: BaseModel or dataclass.
575 config_wrapper: The config wrapper instance.
576 ns_resolver: Namespace resolver to use when getting model annotations.
577 """
578 typevars_map = get_model_typevars_map(cls)
579 fields, pydantic_extra_info, class_vars = collect_model_fields(
580 cls, config_wrapper, ns_resolver, typevars_map=typevars_map
581 )
582
583 cls.__pydantic_fields__ = fields
584 cls.__pydantic_extra_info__ = pydantic_extra_info
585 cls.__class_vars__.update(class_vars)
586
587 for k in class_vars:
588 # Class vars should not be private attributes
589 # We remove them _here_ and not earlier because we rely on inspecting the class to determine its classvars,
590 # but private attributes are determined by inspecting the namespace _prior_ to class creation.
591 # In the case that a classvar with a leading-'_' is defined via a ForwardRef (e.g., when using
592 # `__future__.annotations`), we want to remove the private attribute which was detected _before_ we knew it
593 # evaluated to a classvar
594
595 value = cls.__private_attributes__.pop(k, None)
596 if value is not None and value.default is not PydanticUndefined:
597 setattr(cls, k, value.default)
598
599
600def complete_model_class(

Callers 1

__new__Method · 0.85

Calls 3

get_model_typevars_mapFunction · 0.85
collect_model_fieldsFunction · 0.85
updateMethod · 0.45

Tested by

no test coverage detected