Skip to content

Make ValidationInfo and SerializationInfo generic for context #1686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions python/pydantic_core/core_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from re import Pattern
from typing import TYPE_CHECKING, Any, Callable, Literal, Union

from typing_extensions import deprecated
from typing_extensions import TypeVar, deprecated

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

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

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

class SerializationInfo(Protocol):

class SerializationInfo(Protocol[ContextT]):
@property
def include(self) -> IncExCall: ...

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

@property
def context(self) -> Any | None:
def context(self) -> ContextT:
"""Current serialization context."""
...

@property
def mode(self) -> str: ...
Expand Down Expand Up @@ -163,13 +166,13 @@ class FieldSerializationInfo(SerializationInfo, Protocol):
def field_name(self) -> str: ...


class ValidationInfo(Protocol):
class ValidationInfo(Protocol[ContextT]):
"""
Argument passed to validation functions.
"""

@property
def context(self) -> Any | None:
def context(self) -> ContextT:
"""Current validation context."""
...

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

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

@property
Expand Down
Loading