Rebuild the (already present) model fields by trying to reevaluate annotations. This function should be called whenever a model with incomplete fields is encountered. Returns: A two-tuple, the first element being the rebuilt fields, the second element being the rebuild `Pyd
(
cls: type[BaseModel],
*,
config_wrapper: ConfigWrapper,
ns_resolver: NsResolver,
typevars_map: Mapping[TypeVar, Any],
)
| 434 | |
| 435 | |
| 436 | def rebuild_model_fields( |
| 437 | cls: type[BaseModel], |
| 438 | *, |
| 439 | config_wrapper: ConfigWrapper, |
| 440 | ns_resolver: NsResolver, |
| 441 | typevars_map: Mapping[TypeVar, Any], |
| 442 | ) -> tuple[dict[str, FieldInfo], PydanticExtraInfo | None]: |
| 443 | """Rebuild the (already present) model fields by trying to reevaluate annotations. |
| 444 | |
| 445 | This function should be called whenever a model with incomplete fields is encountered. |
| 446 | |
| 447 | Returns: |
| 448 | A two-tuple, the first element being the rebuilt fields, the second element being |
| 449 | the rebuild `PydanticExtraInfo` instance, if available. |
| 450 | |
| 451 | Raises: |
| 452 | NameError: If one of the annotations failed to evaluate. |
| 453 | |
| 454 | Note: |
| 455 | This function *doesn't* mutate the model fields in place, as it can be called during |
| 456 | schema generation, where you don't want to mutate other model's fields. |
| 457 | """ |
| 458 | rebuilt_fields: dict[str, FieldInfo] = {} |
| 459 | with ns_resolver.push(cls): |
| 460 | for f_name, field_info in cls.__pydantic_fields__.items(): |
| 461 | if field_info._complete: |
| 462 | rebuilt_fields[f_name] = field_info |
| 463 | else: |
| 464 | new_field = _recreate_field_info( |
| 465 | field_info, ns_resolver=ns_resolver, typevars_map=typevars_map, lenient=False |
| 466 | ) |
| 467 | update_field_from_config(config_wrapper, f_name, new_field) |
| 468 | rebuilt_fields[f_name] = new_field |
| 469 | |
| 470 | if cls.__pydantic_extra_info__ is not None and not cls.__pydantic_extra_info__.complete: |
| 471 | rebuilt_extra_info = PydanticExtraInfo( |
| 472 | annotation=_typing_extra.eval_type( |
| 473 | cls.__pydantic_extra_info__.annotation, *ns_resolver.types_namespace |
| 474 | ), |
| 475 | complete=True, |
| 476 | ) |
| 477 | else: |
| 478 | rebuilt_extra_info = cls.__pydantic_extra_info__ |
| 479 | |
| 480 | return rebuilt_fields, rebuilt_extra_info |
| 481 | |
| 482 | |
| 483 | def _recreate_field_info( |
no test coverage detected