-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
__init__.py
156 lines (121 loc) · 5.45 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""Usage docs: https://docs.pydantic.dev/2.4/concepts/plugins#build-a-plugin
Plugin interface for Pydantic plugins, and related types.
"""
from __future__ import annotations
from typing import Any, Callable
from pydantic_core import CoreConfig, CoreSchema, ValidationError
from typing_extensions import Protocol, TypeAlias
__all__ = (
'PydanticPluginProtocol',
'BaseValidateHandlerProtocol',
'ValidatePythonHandlerProtocol',
'ValidateJsonHandlerProtocol',
'ValidateStringsHandlerProtocol',
'NewSchemaReturns',
)
NewSchemaReturns: TypeAlias = 'tuple[ValidatePythonHandlerProtocol | None, ValidateJsonHandlerProtocol | None, ValidateStringsHandlerProtocol | None]'
class PydanticPluginProtocol(Protocol):
"""Protocol defining the interface for Pydantic plugins."""
def new_schema_validator(
self,
schema: CoreSchema,
type_path: str,
item_type: str,
config: CoreConfig | None,
plugin_settings: dict[str, object],
) -> tuple[
ValidatePythonHandlerProtocol | None, ValidateJsonHandlerProtocol | None, ValidateStringsHandlerProtocol | None
]:
"""This method is called for each plugin every time a new [`SchemaValidator`][pydantic_core.SchemaValidator]
is created.
It should return an event handler for each of the three validation methods, or `None` if the plugin does not
implement that method.
Args:
schema: The schema to validate against.
type_path: The path of item to validate against.
item_type: The type of item to validate against.
config: The config to use for validation.
plugin_settings: Any plugin settings.
Returns:
A tuple of optional event handlers for each of the three validation methods -
`validate_python`, `validate_json`, `validate_strings`.
"""
raise NotImplementedError('Pydantic plugins should implement `new_schema_validator`.')
class BaseValidateHandlerProtocol(Protocol):
"""Base class for plugin callbacks protocols.
You shouldn't implement this protocol directly, instead use one of the subclasses with adds the correctly
typed `on_error` method.
"""
on_enter: Callable[..., None]
"""`on_enter` is changed to be more specific on all subclasses"""
def on_success(self, result: Any) -> None:
"""Callback to be notified of successful validation.
Args:
result: The result of the validation.
"""
return
def on_error(self, error: ValidationError) -> None:
"""Callback to be notified of validation errors.
Args:
error: The validation error.
"""
return
def on_exception(self, exception: Exception) -> None:
"""Callback to be notified of validation exceptions.
Args:
exception: The exception raised during validation.
"""
return
class ValidatePythonHandlerProtocol(BaseValidateHandlerProtocol, Protocol):
"""Event handler for `SchemaValidator.validate_python`."""
def on_enter(
self,
input: Any,
*,
strict: bool | None = None,
from_attributes: bool | None = None,
context: dict[str, Any] | None = None,
self_instance: Any | None = None,
) -> None:
"""Callback to be notified of validation start, and create an instance of the event handler.
Args:
input: The input to be validated.
strict: Whether to validate the object in strict mode.
from_attributes: Whether to validate objects as inputs by extracting attributes.
context: The context to use for validation, this is passed to functional validators.
self_instance: An instance of a model to set attributes on from validation, this is used when running
validation from the `__init__` method of a model.
"""
pass
class ValidateJsonHandlerProtocol(BaseValidateHandlerProtocol, Protocol):
"""Event handler for `SchemaValidator.validate_json`."""
def on_enter(
self,
input: str | bytes | bytearray,
*,
strict: bool | None = None,
context: dict[str, Any] | None = None,
self_instance: Any | None = None,
) -> None:
"""Callback to be notified of validation start, and create an instance of the event handler.
Args:
input: The JSON data to be validated.
strict: Whether to validate the object in strict mode.
context: The context to use for validation, this is passed to functional validators.
self_instance: An instance of a model to set attributes on from validation, this is used when running
validation from the `__init__` method of a model.
"""
pass
StringInput: TypeAlias = 'dict[str, StringInput]'
class ValidateStringsHandlerProtocol(BaseValidateHandlerProtocol, Protocol):
"""Event handler for `SchemaValidator.validate_strings`."""
def on_enter(
self, input: StringInput, *, strict: bool | None = None, context: dict[str, Any] | None = None
) -> None:
"""Callback to be notified of validation start, and create an instance of the event handler.
Args:
input: The string data to be validated.
strict: Whether to validate the object in strict mode.
context: The context to use for validation, this is passed to functional validators.
"""
pass