Coverage for pydantic/plugin/__init__.py: 85.00%
20 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"""Usage docs: https://docs.pydantic.dev/2.8/concepts/plugins#build-a-plugin
3Plugin interface for Pydantic plugins, and related types.
4"""
6from __future__ import annotations 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
8from typing import Any, Callable, NamedTuple 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
10from pydantic_core import CoreConfig, CoreSchema, ValidationError 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
11from typing_extensions import Literal, Protocol, TypeAlias 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
13__all__ = ( 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
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]' 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
27class SchemaTypePath(NamedTuple): 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
28 """Path defining where `schema_type` was defined, or where `TypeAdapter` was called."""
30 module: str 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
31 name: str 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
34SchemaKind: TypeAlias = Literal['BaseModel', 'TypeAdapter', 'dataclass', 'create_model', 'validate_call'] 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
37class PydanticPluginProtocol(Protocol): 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
38 """Protocol defining the interface for Pydantic plugins.""" 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
40 def new_schema_validator( 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
41 self,
42 schema: CoreSchema, 1abcdpefghijklmno
43 schema_type: Any, 1abcdpefghijklmno
44 schema_type_path: SchemaTypePath, 1abcdpefghijklmno
45 schema_kind: SchemaKind, 1abcdpefghijklmno
46 config: CoreConfig | None, 1abcdpefghijklmno
47 plugin_settings: dict[str, object], 1abcdpefghijklmno
48 ) -> tuple[ 1abcdpefghijklmno
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): 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
73 """Base class for plugin callbacks protocols. 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
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] 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
80 """`on_enter` is changed to be more specific on all subclasses""" 1qrstabcdeuvwxfghiyzABCDjkEFGHlmno
82 def on_success(self, result: Any) -> None: 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
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: 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
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: 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
99 """Callback to be notified of validation exceptions.
101 Args:
102 exception: The exception raised during validation.
103 """
104 return
107class ValidatePythonHandlerProtocol(BaseValidateHandlerProtocol, Protocol): 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
108 """Event handler for `SchemaValidator.validate_python`."""
110 def on_enter( 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
111 self,
112 input: Any,
113 *,
114 strict: bool | None = None,
115 from_attributes: bool | None = None,
116 context: dict[str, Any] | None = None,
117 self_instance: Any | None = None,
118 ) -> None:
119 """Callback to be notified of validation start, and create an instance of the event handler.
121 Args:
122 input: The input to be validated.
123 strict: Whether to validate the object in strict mode.
124 from_attributes: Whether to validate objects as inputs by extracting attributes.
125 context: The context to use for validation, this is passed to functional validators.
126 self_instance: An instance of a model to set attributes on from validation, this is used when running
127 validation from the `__init__` method of a model.
128 """
129 pass
132class ValidateJsonHandlerProtocol(BaseValidateHandlerProtocol, Protocol): 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
133 """Event handler for `SchemaValidator.validate_json`."""
135 def on_enter( 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
136 self,
137 input: str | bytes | bytearray,
138 *,
139 strict: bool | None = None,
140 context: dict[str, Any] | None = None,
141 self_instance: Any | None = None,
142 ) -> None:
143 """Callback to be notified of validation start, and create an instance of the event handler.
145 Args:
146 input: The JSON data to be validated.
147 strict: Whether to validate the object in strict mode.
148 context: The context to use for validation, this is passed to functional validators.
149 self_instance: An instance of a model to set attributes on from validation, this is used when running
150 validation from the `__init__` method of a model.
151 """
152 pass
155StringInput: TypeAlias = 'dict[str, StringInput]' 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
158class ValidateStringsHandlerProtocol(BaseValidateHandlerProtocol, Protocol): 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
159 """Event handler for `SchemaValidator.validate_strings`."""
161 def on_enter( 1IJKLqrstabcdpeMNOPuvwxfghiQRyzABCDjkSTUVEFGHlmno
162 self, input: StringInput, *, strict: bool | None = None, context: dict[str, Any] | None = None
163 ) -> None:
164 """Callback to be notified of validation start, and create an instance of the event handler.
166 Args:
167 input: The string data to be validated.
168 strict: Whether to validate the object in strict mode.
169 context: The context to use for validation, this is passed to functional validators.
170 """
171 pass