Skip to content

Commit 477b9b3

Browse files
authored
Make ValidationInfo and SerializationInfo generic for context (#1686)
1 parent a7bd9f8 commit 477b9b3

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

python/pydantic_core/core_schema.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from re import Pattern
1414
from typing import TYPE_CHECKING, Any, Callable, Literal, Union
1515

16-
from typing_extensions import deprecated
16+
from typing_extensions import TypeVar, deprecated
1717

1818
if sys.version_info < (3, 12):
1919
from typing_extensions import TypedDict
@@ -118,17 +118,20 @@ class CoreConfig(TypedDict, total=False):
118118

119119
IncExCall: TypeAlias = 'set[int | str] | dict[int | str, IncExCall] | None'
120120

121+
ContextT = TypeVar('ContextT', covariant=True, default='Any | None')
121122

122-
class SerializationInfo(Protocol):
123+
124+
class SerializationInfo(Protocol[ContextT]):
123125
@property
124126
def include(self) -> IncExCall: ...
125127

126128
@property
127129
def exclude(self) -> IncExCall: ...
128130

129131
@property
130-
def context(self) -> Any | None:
132+
def context(self) -> ContextT:
131133
"""Current serialization context."""
134+
...
132135

133136
@property
134137
def mode(self) -> str: ...
@@ -163,13 +166,13 @@ class FieldSerializationInfo(SerializationInfo, Protocol):
163166
def field_name(self) -> str: ...
164167

165168

166-
class ValidationInfo(Protocol):
169+
class ValidationInfo(Protocol[ContextT]):
167170
"""
168171
Argument passed to validation functions.
169172
"""
170173

171174
@property
172-
def context(self) -> Any | None:
175+
def context(self) -> ContextT:
173176
"""Current validation context."""
174177
...
175178

@@ -180,7 +183,7 @@ def config(self) -> CoreConfig | None:
180183

181184
@property
182185
def mode(self) -> Literal['python', 'json']:
183-
"""The type of input data we are currently validating"""
186+
"""The type of input data we are currently validating."""
184187
...
185188

186189
@property

0 commit comments

Comments
 (0)