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],
)
| 633 | |
| 634 | |
| 635 | def 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 | |
| 681 | def is_valid_field_name(name: str) -> bool: |
no test coverage detected