Generate schema for a Pydantic model.
(self, cls: type[BaseModel])
| 754 | return schema |
| 755 | |
| 756 | def _model_schema(self, cls: type[BaseModel]) -> core_schema.CoreSchema: |
| 757 | """Generate schema for a Pydantic model.""" |
| 758 | BaseModel_ = import_cached_base_model() |
| 759 | |
| 760 | with self.defs.get_schema_or_ref(cls) as (model_ref, maybe_schema): |
| 761 | if maybe_schema is not None: |
| 762 | return maybe_schema |
| 763 | |
| 764 | schema = cls.__dict__.get('__pydantic_core_schema__') |
| 765 | if schema is not None and not isinstance(schema, MockCoreSchema): |
| 766 | if schema['type'] == 'definitions': |
| 767 | schema = self.defs.unpack_definitions(schema) |
| 768 | ref = get_ref(schema) |
| 769 | if ref: |
| 770 | return self.defs.create_definition_reference_schema(schema) |
| 771 | else: |
| 772 | return schema |
| 773 | |
| 774 | config_wrapper = ConfigWrapper(cls.model_config, check=False) |
| 775 | |
| 776 | with self._config_wrapper_stack.push(config_wrapper), self._ns_resolver.push(cls): |
| 777 | core_config = self._config_wrapper.core_config(title=cls.__name__) |
| 778 | |
| 779 | if cls.__pydantic_fields_complete__ or cls is BaseModel_: |
| 780 | fields = getattr(cls, '__pydantic_fields__', {}) |
| 781 | extra_info = getattr(cls, '__pydantic_extra_info__', None) |
| 782 | else: |
| 783 | if '__pydantic_fields__' not in cls.__dict__: |
| 784 | # This happens when we have a loop in the schema generation: |
| 785 | # class Base[T](BaseModel): |
| 786 | # t: T |
| 787 | # |
| 788 | # class Other(BaseModel): |
| 789 | # b: 'Base[Other]' |
| 790 | # When we build fields for `Other`, we evaluate the forward annotation. |
| 791 | # At this point, `Other` doesn't have the model fields set. We create |
| 792 | # `Base[Other]`; model fields are successfully built, and we try to generate |
| 793 | # a schema for `t: Other`. As `Other.__pydantic_fields__` aren't set, we abort. |
| 794 | raise PydanticUndefinedAnnotation( |
| 795 | name=cls.__name__, |
| 796 | message=f'Class {cls.__name__!r} is not defined', |
| 797 | ) |
| 798 | try: |
| 799 | fields, extra_info = rebuild_model_fields( |
| 800 | cls, |
| 801 | config_wrapper=self._config_wrapper, |
| 802 | ns_resolver=self._ns_resolver, |
| 803 | typevars_map=self._typevars_map or {}, |
| 804 | ) |
| 805 | except NameError as e: |
| 806 | raise PydanticUndefinedAnnotation.from_name_error(e) from e |
| 807 | |
| 808 | decorators = cls.__pydantic_decorators__ |
| 809 | computed_fields = decorators.computed_fields |
| 810 | check_decorator_fields_exist( |
| 811 | chain( |
| 812 | decorators.field_validators.values(), |
| 813 | decorators.field_serializers.values(), |
no test coverage detected