Skip to content
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

Standardize __get_pydantic_core_schema__ signature #7415

Merged
merged 1 commit into from Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 6 additions & 8 deletions docs/usage/types/custom.md
Expand Up @@ -408,16 +408,14 @@ Notice that type checkers will not complain about assigning `'abc'` to `Username
Another use case for the pattern in the previous section is to handle third party types.

```py
from typing import (
Any,
Callable,
)
from typing import Any

from pydantic_core import core_schema
from typing_extensions import Annotated

from pydantic import (
BaseModel,
GetCoreSchemaHandler,
GetJsonSchemaHandler,
ValidationError,
)
Expand All @@ -441,7 +439,7 @@ class _ThirdPartyTypePydanticAnnotation:
def __get_pydantic_core_schema__(
cls,
_source_type: Any,
_handler: Callable[[Any], core_schema.CoreSchema],
_handler: GetCoreSchemaHandler,
) -> core_schema.CoreSchema:
"""
We return a pydantic_core.CoreSchema that behaves in the following ways:
Expand Down Expand Up @@ -743,12 +741,12 @@ except ValidationError as e:
The same idea can be applied to create generic container types, like a custom `Sequence` type:

```python
from typing import Any, Callable, Sequence, TypeVar
from typing import Any, Sequence, TypeVar

from pydantic_core import ValidationError, core_schema
from typing_extensions import get_args

from pydantic import BaseModel
from pydantic import BaseModel, GetCoreSchemaHandler

T = TypeVar('T')

Expand All @@ -765,7 +763,7 @@ class MySequence(Sequence[T]):

@classmethod
def __get_pydantic_core_schema__(
cls, source: Any, handler: Callable[[Any], core_schema.CoreSchema]
cls, source: Any, handler: GetCoreSchemaHandler
) -> core_schema.CoreSchema:
instance_schema = core_schema.is_instance_schema(cls)

Expand Down
7 changes: 6 additions & 1 deletion pydantic/networks.py
Expand Up @@ -9,7 +9,7 @@
from pydantic_core import MultiHostUrl, PydanticCustomError, Url, core_schema
from typing_extensions import Annotated, TypeAlias

from ._internal import _fields, _repr, _schema_generation_shared
from ._internal import _annotated_handlers, _fields, _repr, _schema_generation_shared
from ._migration import getattr_migration
from .json_schema import JsonSchemaValue

Expand Down Expand Up @@ -194,6 +194,7 @@ class Model(BaseModel):
def __get_pydantic_core_schema__(
cls,
source: type[Any],
handler: _annotated_handlers.GetCoreSchemaHandler,
) -> core_schema.CoreSchema:
import_email_validator()
return core_schema.general_after_validator_function(cls._validate, core_schema.str_schema())
Expand Down Expand Up @@ -268,6 +269,7 @@ def __get_pydantic_json_schema__(
def __get_pydantic_core_schema__(
cls,
source: type[Any],
handler: _annotated_handlers.GetCoreSchemaHandler,
) -> core_schema.CoreSchema:
import_email_validator()
return core_schema.general_after_validator_function(
Expand Down Expand Up @@ -321,6 +323,7 @@ def __get_pydantic_json_schema__(
def __get_pydantic_core_schema__(
cls,
source: type[Any],
handler: _annotated_handlers.GetCoreSchemaHandler,
) -> core_schema.CoreSchema:
return core_schema.general_plain_validator_function(
cls._validate, serialization=core_schema.to_string_ser_schema()
Expand Down Expand Up @@ -360,6 +363,7 @@ def __get_pydantic_json_schema__(
def __get_pydantic_core_schema__(
cls,
source: type[Any],
handler: _annotated_handlers.GetCoreSchemaHandler,
Copy link
Member

@Kludex Kludex Sep 12, 2023

Choose a reason for hiding this comment

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

Are those needed in this file?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, not needed because they are working without them. This is just to make all the __get_pydantic_core_schema__ signature similar.

Copy link
Member

Choose a reason for hiding this comment

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

I see why you changed the other files, but since this is optional here, I would leave without it.

Anyway, not a blocker. Do as you think is more appropriate. 🙏

) -> core_schema.CoreSchema:
return core_schema.general_plain_validator_function(
cls._validate, serialization=core_schema.to_string_ser_schema()
Expand Down Expand Up @@ -401,6 +405,7 @@ def __get_pydantic_json_schema__(
def __get_pydantic_core_schema__(
cls,
source: type[Any],
handler: _annotated_handlers.GetCoreSchemaHandler,
) -> core_schema.CoreSchema:
return core_schema.general_plain_validator_function(
cls._validate, serialization=core_schema.to_string_ser_schema()
Expand Down
4 changes: 2 additions & 2 deletions pydantic/types.py
Expand Up @@ -1534,7 +1534,7 @@ def __get_pydantic_json_schema__(
return field_schema

def __get_pydantic_core_schema__(
self, source: type[Any], handler: Callable[[Any], core_schema.CoreSchema]
self, source: type[Any], handler: _annotated_handlers.GetCoreSchemaHandler
) -> core_schema.CoreSchema:
return core_schema.general_after_validator_function(
function=self.decode,
Expand Down Expand Up @@ -1623,7 +1623,7 @@ class Model(BaseModel):
"""

def __get_pydantic_core_schema__(
self, source: type[Any], handler: Callable[[Any], core_schema.CoreSchema]
self, source: type[Any], handler: _annotated_handlers.GetCoreSchemaHandler
) -> core_schema.CoreSchema:
return core_schema.general_after_validator_function(
function=self.decode_str,
Expand Down
6 changes: 2 additions & 4 deletions tests/test_edge_cases.py
Expand Up @@ -29,6 +29,7 @@
from pydantic import (
BaseModel,
ConfigDict,
GetCoreSchemaHandler,
PydanticDeprecatedSince20,
PydanticInvalidForJsonSchema,
PydanticSchemaGenerationError,
Expand Down Expand Up @@ -1890,10 +1891,7 @@ def __init__(self, t1: T1, t2: T2):
self.t2 = t2

@classmethod
def __get_pydantic_core_schema__(
cls,
source: Any,
):
def __get_pydantic_core_schema__(cls, source: Any, handler: GetCoreSchemaHandler):
schema = core_schema.is_instance_schema(cls)

args = get_args(source)
Expand Down