Skip to content

Commit

Permalink
Standardize __get_pydantic_core_schema__ signature (#7415)
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed Sep 12, 2023
1 parent be88bca commit b46010b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
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,
) -> 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 @@ -1566,7 +1566,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 @@ -1655,7 +1655,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

0 comments on commit b46010b

Please sign in to comment.