annotated_handlers.py 4.2 KB

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