Coverage for pydantic/annotated_handlers.py: 100.00%
15 statements
« prev ^ index » next coverage.py v7.5.4, created at 2024-07-03 19:29 +0000
« prev ^ index » next coverage.py v7.5.4, created at 2024-07-03 19:29 +0000
1"""Type annotations to use with `__get_pydantic_core_schema__` and `__get_pydantic_json_schema__`."""
3from __future__ import annotations as _annotations 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV
5from typing import TYPE_CHECKING, Any, Union 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV
7from pydantic_core import core_schema 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV
9if TYPE_CHECKING: 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV
10 from .json_schema import JsonSchemaMode, JsonSchemaValue
12 CoreSchemaOrField = Union[
13 core_schema.CoreSchema,
14 core_schema.ModelField,
15 core_schema.DataclassField,
16 core_schema.TypedDictField,
17 core_schema.ComputedField,
18 ]
20__all__ = 'GetJsonSchemaHandler', 'GetCoreSchemaHandler' 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV
23class GetJsonSchemaHandler: 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV
24 """Handler to call into the next JSON schema generation function.
26 Attributes:
27 mode: Json schema mode, can be `validation` or `serialization`.
28 """
30 mode: JsonSchemaMode 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV
32 def __call__(self, core_schema: CoreSchemaOrField, /) -> JsonSchemaValue: 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV
33 """Call the inner handler and get the JsonSchemaValue it returns.
34 This will call the next JSON schema modifying function up until it calls
35 into `pydantic.json_schema.GenerateJsonSchema`, which will raise a
36 `pydantic.errors.PydanticInvalidForJsonSchema` error if it cannot generate
37 a JSON schema.
39 Args:
40 core_schema: A `pydantic_core.core_schema.CoreSchema`.
42 Returns:
43 JsonSchemaValue: The JSON schema generated by the inner JSON schema modify
44 functions.
45 """
46 raise NotImplementedError
48 def resolve_ref_schema(self, maybe_ref_json_schema: JsonSchemaValue, /) -> JsonSchemaValue: 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV
49 """Get the real schema for a `{"$ref": ...}` schema.
50 If the schema given is not a `$ref` schema, it will be returned as is.
51 This means you don't have to check before calling this function.
53 Args:
54 maybe_ref_json_schema: A JsonSchemaValue which may be a `$ref` schema.
56 Raises:
57 LookupError: If the ref is not found.
59 Returns:
60 JsonSchemaValue: A JsonSchemaValue that has no `$ref`.
61 """
62 raise NotImplementedError
65class GetCoreSchemaHandler: 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV
66 """Handler to call into the next CoreSchema schema generation function."""
68 def __call__(self, source_type: Any, /) -> core_schema.CoreSchema: 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV
69 """Call the inner handler and get the CoreSchema it returns.
70 This will call the next CoreSchema modifying function up until it calls
71 into Pydantic's internal schema generation machinery, which will raise a
72 `pydantic.errors.PydanticSchemaGenerationError` error if it cannot generate
73 a CoreSchema for the given source type.
75 Args:
76 source_type: The input type.
78 Returns:
79 CoreSchema: The `pydantic-core` CoreSchema generated.
80 """
81 raise NotImplementedError
83 def generate_schema(self, source_type: Any, /) -> core_schema.CoreSchema: 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV
84 """Generate a schema unrelated to the current context.
85 Use this function if e.g. you are handling schema generation for a sequence
86 and want to generate a schema for its items.
87 Otherwise, you may end up doing something like applying a `min_length` constraint
88 that was intended for the sequence itself to its items!
90 Args:
91 source_type: The input type.
93 Returns:
94 CoreSchema: The `pydantic-core` CoreSchema generated.
95 """
96 raise NotImplementedError
98 def resolve_ref_schema(self, maybe_ref_schema: core_schema.CoreSchema, /) -> core_schema.CoreSchema: 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV
99 """Get the real schema for a `definition-ref` schema.
100 If the schema given is not a `definition-ref` schema, it will be returned as is.
101 This means you don't have to check before calling this function.
103 Args:
104 maybe_ref_schema: A `CoreSchema`, `ref`-based or not.
106 Raises:
107 LookupError: If the `ref` is not found.
109 Returns:
110 A concrete `CoreSchema`.
111 """
112 raise NotImplementedError
114 @property 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV
115 def field_name(self) -> str | None: 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV
116 """Get the name of the closest field to this validator."""
117 raise NotImplementedError
119 def _get_types_namespace(self) -> dict[str, Any] | None: 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV
120 """Internal method used during type resolution for serializer annotations."""
121 raise NotImplementedError