Coverage for pydantic / plugin / __init__.py: 100.00%
13 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-11-21 11:56 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-11-21 11:56 +0000
1"""Plugin interface for Pydantic plugins, and related types."""
3from __future__ import annotations 1habijklcdmnoepfgqrs
5from collections.abc import Callable 1habijklcdmnoepfgqrs
6from typing import Any, Literal, NamedTuple, TypeAlias 1habijklcdmnoepfgqrs
8from pydantic_core import CoreConfig, CoreSchema, ValidationError 1habijklcdmnoepfgqrs
9from typing_extensions import Protocol 1habijklcdmnoepfgqrs
11from pydantic.config import ExtraValues 1habijklcdmnoepfgqrs
13__all__ = ( 1habijklcdmnoepfgqrs
14 'PydanticPluginProtocol',
15 'BaseValidateHandlerProtocol',
16 'ValidatePythonHandlerProtocol',
17 'ValidateJsonHandlerProtocol',
18 'ValidateStringsHandlerProtocol',
19 'NewSchemaReturns',
20 'SchemaTypePath',
21 'SchemaKind',
22)
24NewSchemaReturns: TypeAlias = 'tuple[ValidatePythonHandlerProtocol | None, ValidateJsonHandlerProtocol | None, ValidateStringsHandlerProtocol | None]' 1habijklcdmnoepfgqrs
27class SchemaTypePath(NamedTuple): 1habijklcdmnoepfgqrs
28 """Path defining where `schema_type` was defined, or where `TypeAdapter` was called."""
30 module: str 1habijklcdmnoepfgqrs
31 name: str 1habijklcdmnoepfgqrs
34SchemaKind: TypeAlias = Literal['BaseModel', 'TypeAdapter', 'dataclass', 'create_model', 'validate_call'] 1habijklcdmnoepfgqrs
37class PydanticPluginProtocol(Protocol): 1habijklcdmnoepfgqrs
38 """Protocol defining the interface for Pydantic plugins.""" 1habijklcdmnoepfgqrs
40 def new_schema_validator( 1habijklcdmnoepfgqrs
41 self,
42 schema: CoreSchema, 1abcdefg
43 schema_type: Any, 1abcdefg
44 schema_type_path: SchemaTypePath, 1abcdefg
45 schema_kind: SchemaKind, 1abcdefg
46 config: CoreConfig | None, 1abcdefg
47 plugin_settings: dict[str, object], 1abcdefg
48 ) -> tuple[ 1abcdefg
49 ValidatePythonHandlerProtocol | None, ValidateJsonHandlerProtocol | None, ValidateStringsHandlerProtocol | None
50 ]:
51 """This method is called for each plugin every time a new [`SchemaValidator`][pydantic_core.SchemaValidator]
52 is created.
54 It should return an event handler for each of the three validation methods, or `None` if the plugin does not
55 implement that method.
57 Args:
58 schema: The schema to validate against.
59 schema_type: The original type which the schema was created from, e.g. the model class.
60 schema_type_path: Path defining where `schema_type` was defined, or where `TypeAdapter` was called.
61 schema_kind: The kind of schema to validate against.
62 config: The config to use for validation.
63 plugin_settings: Any plugin settings.
65 Returns:
66 A tuple of optional event handlers for each of the three validation methods -
67 `validate_python`, `validate_json`, `validate_strings`.
68 """
69 raise NotImplementedError('Pydantic plugins should implement `new_schema_validator`.')
72class BaseValidateHandlerProtocol(Protocol): 1habijklcdmnoepfgqrs
73 """Base class for plugin callbacks protocols. 1habijklcdmnoepfgqrs
75 You shouldn't implement this protocol directly, instead use one of the subclasses with adds the correctly
76 typed `on_error` method.
77 """
79 on_enter: Callable[..., None] 1habijklcdmnoepfgqrs
80 """`on_enter` is changed to be more specific on all subclasses""" 1habijklcdmnoepfgqrs
82 def on_success(self, result: Any) -> None: 1habijklcdmnoepfgqrs
83 """Callback to be notified of successful validation.
85 Args:
86 result: The result of the validation.
87 """
88 return
90 def on_error(self, error: ValidationError) -> None: 1habijklcdmnoepfgqrs
91 """Callback to be notified of validation errors.
93 Args:
94 error: The validation error.
95 """
96 return
98 def on_exception(self, exception: Exception) -> None: 1habijklcdmnoepfgqrs
99 """Callback to be notified of validation exceptions.
101 Args:
102 exception: The exception raised during validation.
103 """
104 return
107class ValidatePythonHandlerProtocol(BaseValidateHandlerProtocol, Protocol): 1habijklcdmnoepfgqrs
108 """Event handler for `SchemaValidator.validate_python`.""" 1habijklcdmnoepfgqrs
110 def on_enter( 1habijklcdmnoepfgqrs
111 self,
112 input: Any, 1abcdefg
113 *,
114 strict: bool | None = None, 1habijklcdmnoepfgqrs
115 extra: ExtraValues | None = None, 1habijklcdmnoepfgqrs
116 from_attributes: bool | None = None, 1habijklcdmnoepfgqrs
117 context: Any | None = None, 1habijklcdmnoepfgqrs
118 self_instance: Any | None = None, 1habijklcdmnoepfgqrs
119 by_alias: bool | None = None, 1habijklcdmnoepfgqrs
120 by_name: bool | None = None, 1habijklcdmnoepfgqrs
121 ) -> None: 1abcdefg
122 """Callback to be notified of validation start, and create an instance of the event handler.
124 Args:
125 input: The input to be validated.
126 strict: Whether to validate the object in strict mode.
127 extra: Whether to ignore, allow, or forbid extra data during model validation.
128 from_attributes: Whether to validate objects as inputs by extracting attributes.
129 context: The context to use for validation, this is passed to functional validators.
130 self_instance: An instance of a model to set attributes on from validation, this is used when running
131 validation from the `__init__` method of a model.
132 by_alias: Whether to use the field's alias to match the input data to an attribute.
133 by_name: Whether to use the field's name to match the input data to an attribute.
134 """
137class ValidateJsonHandlerProtocol(BaseValidateHandlerProtocol, Protocol): 1habijklcdmnoepfgqrs
138 """Event handler for `SchemaValidator.validate_json`.""" 1habijklcdmnoepfgqrs
140 def on_enter( 1habijklcdmnoepfgqrs
141 self,
142 input: str | bytes | bytearray, 1abcdefg
143 *,
144 strict: bool | None = None, 1habijklcdmnoepfgqrs
145 extra: ExtraValues | None = None, 1habijklcdmnoepfgqrs
146 context: Any | None = None, 1habijklcdmnoepfgqrs
147 self_instance: Any | None = None, 1habijklcdmnoepfgqrs
148 by_alias: bool | None = None, 1habijklcdmnoepfgqrs
149 by_name: bool | None = None, 1habijklcdmnoepfgqrs
150 ) -> None: 1abcdefg
151 """Callback to be notified of validation start, and create an instance of the event handler.
153 Args:
154 input: The JSON data to be validated.
155 strict: Whether to validate the object in strict mode.
156 extra: Whether to ignore, allow, or forbid extra data during model validation.
157 context: The context to use for validation, this is passed to functional validators.
158 self_instance: An instance of a model to set attributes on from validation, this is used when running
159 validation from the `__init__` method of a model.
160 by_alias: Whether to use the field's alias to match the input data to an attribute.
161 by_name: Whether to use the field's name to match the input data to an attribute.
162 """
165StringInput: TypeAlias = 'dict[str, StringInput]' 1habijklcdmnoepfgqrs
168class ValidateStringsHandlerProtocol(BaseValidateHandlerProtocol, Protocol): 1habijklcdmnoepfgqrs
169 """Event handler for `SchemaValidator.validate_strings`.""" 1habijklcdmnoepfgqrs
171 def on_enter( 1habijklcdmnoepfgqrs
172 self,
173 input: StringInput, 1abcdefg
174 *,
175 strict: bool | None = None, 1habijklcdmnoepfgqrs
176 extra: ExtraValues | None = None, 1habijklcdmnoepfgqrs
177 context: Any | None = None, 1habijklcdmnoepfgqrs
178 by_alias: bool | None = None, 1habijklcdmnoepfgqrs
179 by_name: bool | None = None, 1habijklcdmnoepfgqrs
180 ) -> None: 1abcdefg
181 """Callback to be notified of validation start, and create an instance of the event handler.
183 Args:
184 input: The string data to be validated.
185 strict: Whether to validate the object in strict mode.
186 extra: Whether to ignore, allow, or forbid extra data during model validation.
187 context: The context to use for validation, this is passed to functional validators.
188 by_alias: Whether to use the field's alias to match the input data to an attribute.
189 by_name: Whether to use the field's name to match the input data to an attribute.
190 """