MCPcopy
hub / github.com/pydantic/pydantic / rebuild_dataclass_fields

Function rebuild_dataclass_fields

pydantic/_internal/_fields.py:635–678  ·  view source on GitHub ↗

Rebuild the (already present) dataclass fields by trying to reevaluate annotations. This function should be called whenever a dataclass with incomplete fields is encountered. Raises: NameError: If one of the annotations failed to evaluate. Note: This function *doesn't*

(
    cls: type[PydanticDataclass],
    *,
    config_wrapper: ConfigWrapper,
    ns_resolver: NsResolver,
    typevars_map: Mapping[TypeVar, Any],
)

Source from the content-addressed store, hash-verified

633
634
635def rebuild_dataclass_fields(
636 cls: type[PydanticDataclass],
637 *,
638 config_wrapper: ConfigWrapper,
639 ns_resolver: NsResolver,
640 typevars_map: Mapping[TypeVar, Any],
641) -> dict[str, FieldInfo]:
642 """Rebuild the (already present) dataclass fields by trying to reevaluate annotations.
643
644 This function should be called whenever a dataclass with incomplete fields is encountered.
645
646 Raises:
647 NameError: If one of the annotations failed to evaluate.
648
649 Note:
650 This function *doesn't* mutate the dataclass fields in place, as it can be called during
651 schema generation, where you don't want to mutate other dataclass's fields.
652 """
653 FieldInfo_ = import_cached_field_info()
654
655 rebuilt_fields: dict[str, FieldInfo] = {}
656 with ns_resolver.push(cls):
657 for f_name, field_info in cls.__pydantic_fields__.items():
658 if field_info._complete:
659 rebuilt_fields[f_name] = field_info
660 else:
661 existing_desc = field_info.description
662 ann = _typing_extra.eval_type(
663 field_info._original_annotation,
664 *ns_resolver.types_namespace,
665 )
666 ann = _generics.replace_types(ann, typevars_map)
667 new_field = FieldInfo_.from_annotated_attribute(
668 ann,
669 field_info._original_assignment,
670 _source=AnnotationSource.DATACLASS,
671 )
672
673 # The description might come from the docstring if `use_attribute_docstrings` was `True`:
674 new_field.description = new_field.description if new_field.description is not None else existing_desc
675 update_field_from_config(config_wrapper, f_name, new_field)
676 rebuilt_fields[f_name] = new_field
677
678 return rebuilt_fields
679
680
681def is_valid_field_name(name: str) -> bool:

Callers 1

_dataclass_schemaMethod · 0.85

Calls 5

import_cached_field_infoFunction · 0.85
update_field_from_configFunction · 0.85
pushMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected