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