Skip to content
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
52 changes: 37 additions & 15 deletions python/pydantic_core/core_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,37 +122,57 @@ class CoreConfig(TypedDict, total=False):


class SerializationInfo(Protocol[ContextT]):
"""Extra data used during serialization."""

@property
def include(self) -> IncExCall: ...
def include(self) -> IncExCall:
"""The `include` argument set during serialization."""
...

@property
def exclude(self) -> IncExCall: ...
def exclude(self) -> IncExCall:
"""The `exclude` argument set during serialization."""
...

@property
def context(self) -> ContextT:
"""Current serialization context."""
"""The current serialization context."""
...

@property
def mode(self) -> str: ...
def mode(self) -> Literal['python', 'json']:
Copy link
Contributor

@l00ptr l00ptr Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Viicos,

In another place, Pydantic is less restrictive and allows the use of mode: Literal['json', 'python'] | str = 'python', so using only a Literal with two possibilities won't cause any problems?

On our end, MyPy throws errors because we use a custom mode for some models. Perhaps the other mode definitions (in pydantic) will evolve, and we'll have to find another way to use the "custom" mode. However, I currently don't know how to do that.

Best regards,
J.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @l00ptr, could you please provide more details on how you managed to integrate a custom mode in your Pydantic code? We currently support only two hardcoded modes, and I'd like to avoid "advertising" than custom modes may be supported while it isn't the case.

The existing Literal['json', 'python'] | str annotations were introduced a long time ago in pydantic/pydantic#4978, my best guess being that it wasn't clear if only two modes were to be supported.

"""The serialization mode set during serialization."""
...

@property
def by_alias(self) -> bool: ...
def by_alias(self) -> bool:
"""The `by_alias` argument set during serialization."""
...

@property
def exclude_unset(self) -> bool: ...
def exclude_unset(self) -> bool:
"""The `exclude_unset` argument set during serialization."""
...

@property
def exclude_defaults(self) -> bool: ...
def exclude_defaults(self) -> bool:
"""The `exclude_defaults` argument set during serialization."""
...

@property
def exclude_none(self) -> bool: ...
def exclude_none(self) -> bool:
"""The `exclude_none` argument set during serialization."""
...

@property
def serialize_as_any(self) -> bool: ...
def serialize_as_any(self) -> bool:
"""The `serialize_as_any` argument set during serialization."""
...

@property
def round_trip(self) -> bool: ...
def round_trip(self) -> bool:
"""The `round_trip` argument set during serialization."""
...

def mode_is_json(self) -> bool: ...

Expand All @@ -162,18 +182,20 @@ def __repr__(self) -> str: ...


class FieldSerializationInfo(SerializationInfo[ContextT], Protocol):
"""Extra data used during field serialization."""

@property
def field_name(self) -> str: ...
def field_name(self) -> str:
"""The name of the current field being serialized."""
...


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

@property
def context(self) -> ContextT:
"""Current validation context."""
"""The current validation context."""
...

@property
Expand Down
Loading