Try to rebuild the pydantic-core schema for the model. This may be necessary when one of the annotations is a ForwardRef which could not be resolved during the initial attempt to build the schema, and automatic rebuilding fails. Args: force: Whether to force the
(
cls,
*,
force: bool = False,
raise_errors: bool = True,
_parent_namespace_depth: int = 2,
_types_namespace: MappingNamespace | None = None,
)
| 631 | |
| 632 | @classmethod |
| 633 | def model_rebuild( |
| 634 | cls, |
| 635 | *, |
| 636 | force: bool = False, |
| 637 | raise_errors: bool = True, |
| 638 | _parent_namespace_depth: int = 2, |
| 639 | _types_namespace: MappingNamespace | None = None, |
| 640 | ) -> bool | None: |
| 641 | """Try to rebuild the pydantic-core schema for the model. |
| 642 | |
| 643 | This may be necessary when one of the annotations is a ForwardRef which could not be resolved during |
| 644 | the initial attempt to build the schema, and automatic rebuilding fails. |
| 645 | |
| 646 | Args: |
| 647 | force: Whether to force the rebuilding of the model schema, defaults to `False`. |
| 648 | raise_errors: Whether to raise errors, defaults to `True`. |
| 649 | _parent_namespace_depth: The depth level of the parent namespace, defaults to 2. |
| 650 | _types_namespace: The types namespace, defaults to `None`. |
| 651 | |
| 652 | Returns: |
| 653 | Returns `None` if the schema is already "complete" and rebuilding was not required. |
| 654 | If rebuilding _was_ required, returns `True` if rebuilding was successful, otherwise `False`. |
| 655 | """ |
| 656 | already_complete = cls.__pydantic_complete__ |
| 657 | if already_complete and not force: |
| 658 | return None |
| 659 | |
| 660 | cls.__pydantic_complete__ = False |
| 661 | |
| 662 | for attr in ('__pydantic_core_schema__', '__pydantic_validator__', '__pydantic_serializer__'): |
| 663 | if attr in cls.__dict__ and not isinstance(getattr(cls, attr), _mock_val_ser.MockValSer): |
| 664 | # Deleting the validator/serializer is necessary as otherwise they can get reused in |
| 665 | # pydantic-core. We do so only if they aren't mock instances, otherwise — as `model_rebuild()` |
| 666 | # isn't thread-safe — concurrent model instantiations can lead to the parent validator being used. |
| 667 | # Same applies for the core schema that can be reused in schema generation. |
| 668 | delattr(cls, attr) |
| 669 | |
| 670 | if _types_namespace is not None: |
| 671 | rebuild_ns = _types_namespace |
| 672 | elif _parent_namespace_depth > 0: |
| 673 | rebuild_ns = _typing_extra.parent_frame_namespace(parent_depth=_parent_namespace_depth, force=True) or {} |
| 674 | else: |
| 675 | rebuild_ns = {} |
| 676 | |
| 677 | parent_ns = _model_construction.unpack_lenient_weakvaluedict(cls.__pydantic_parent_namespace__) or {} |
| 678 | |
| 679 | ns_resolver = _namespace_utils.NsResolver( |
| 680 | parent_namespace={**rebuild_ns, **parent_ns}, |
| 681 | ) |
| 682 | |
| 683 | return _model_construction.complete_model_class( |
| 684 | cls, |
| 685 | _config.ConfigWrapper(cls.model_config, check=False), |
| 686 | ns_resolver, |
| 687 | raise_errors=raise_errors, |
| 688 | # If the model was already complete, we don't need to call the hook again. |
| 689 | call_on_complete_hook=not already_complete, |
| 690 | is_force_rebuild=force, |
no outgoing calls