Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

123 řádky
4.3 KiB

  1. """Type annotations to use with `__get_pydantic_core_schema__` and `__get_pydantic_json_schema__`."""
  2. from __future__ import annotations as _annotations
  3. from typing import TYPE_CHECKING, Any, Union
  4. from pydantic_core import core_schema
  5. if TYPE_CHECKING:
  6. from ._internal._namespace_utils import NamespacesTuple
  7. from .json_schema import JsonSchemaMode, JsonSchemaValue
  8. CoreSchemaOrField = Union[
  9. core_schema.CoreSchema,
  10. core_schema.ModelField,
  11. core_schema.DataclassField,
  12. core_schema.TypedDictField,
  13. core_schema.ComputedField,
  14. ]
  15. __all__ = 'GetJsonSchemaHandler', 'GetCoreSchemaHandler'
  16. class GetJsonSchemaHandler:
  17. """Handler to call into the next JSON schema generation function.
  18. Attributes:
  19. mode: Json schema mode, can be `validation` or `serialization`.
  20. """
  21. mode: JsonSchemaMode
  22. def __call__(self, core_schema: CoreSchemaOrField, /) -> JsonSchemaValue:
  23. """Call the inner handler and get the JsonSchemaValue it returns.
  24. This will call the next JSON schema modifying function up until it calls
  25. into `pydantic.json_schema.GenerateJsonSchema`, which will raise a
  26. `pydantic.errors.PydanticInvalidForJsonSchema` error if it cannot generate
  27. a JSON schema.
  28. Args:
  29. core_schema: A `pydantic_core.core_schema.CoreSchema`.
  30. Returns:
  31. JsonSchemaValue: The JSON schema generated by the inner JSON schema modify
  32. functions.
  33. """
  34. raise NotImplementedError
  35. def resolve_ref_schema(self, maybe_ref_json_schema: JsonSchemaValue, /) -> JsonSchemaValue:
  36. """Get the real schema for a `{"$ref": ...}` schema.
  37. If the schema given is not a `$ref` schema, it will be returned as is.
  38. This means you don't have to check before calling this function.
  39. Args:
  40. maybe_ref_json_schema: A JsonSchemaValue which may be a `$ref` schema.
  41. Raises:
  42. LookupError: If the ref is not found.
  43. Returns:
  44. JsonSchemaValue: A JsonSchemaValue that has no `$ref`.
  45. """
  46. raise NotImplementedError
  47. class GetCoreSchemaHandler:
  48. """Handler to call into the next CoreSchema schema generation function."""
  49. def __call__(self, source_type: Any, /) -> core_schema.CoreSchema:
  50. """Call the inner handler and get the CoreSchema it returns.
  51. This will call the next CoreSchema modifying function up until it calls
  52. into Pydantic's internal schema generation machinery, which will raise a
  53. `pydantic.errors.PydanticSchemaGenerationError` error if it cannot generate
  54. a CoreSchema for the given source type.
  55. Args:
  56. source_type: The input type.
  57. Returns:
  58. CoreSchema: The `pydantic-core` CoreSchema generated.
  59. """
  60. raise NotImplementedError
  61. def generate_schema(self, source_type: Any, /) -> core_schema.CoreSchema:
  62. """Generate a schema unrelated to the current context.
  63. Use this function if e.g. you are handling schema generation for a sequence
  64. and want to generate a schema for its items.
  65. Otherwise, you may end up doing something like applying a `min_length` constraint
  66. that was intended for the sequence itself to its items!
  67. Args:
  68. source_type: The input type.
  69. Returns:
  70. CoreSchema: The `pydantic-core` CoreSchema generated.
  71. """
  72. raise NotImplementedError
  73. def resolve_ref_schema(self, maybe_ref_schema: core_schema.CoreSchema, /) -> core_schema.CoreSchema:
  74. """Get the real schema for a `definition-ref` schema.
  75. If the schema given is not a `definition-ref` schema, it will be returned as is.
  76. This means you don't have to check before calling this function.
  77. Args:
  78. maybe_ref_schema: A `CoreSchema`, `ref`-based or not.
  79. Raises:
  80. LookupError: If the `ref` is not found.
  81. Returns:
  82. A concrete `CoreSchema`.
  83. """
  84. raise NotImplementedError
  85. @property
  86. def field_name(self) -> str | None:
  87. """Get the name of the closest field to this validator."""
  88. raise NotImplementedError
  89. def _get_types_namespace(self) -> NamespacesTuple:
  90. """Internal method used during type resolution for serializer annotations."""
  91. raise NotImplementedError