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

Fix typehint for JSON schema extra callable #6659

Merged
merged 1 commit into from
Jul 13, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions pydantic/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Configuration for Pydantic models."""
from __future__ import annotations as _annotations

from typing import TYPE_CHECKING, Any, Callable, overload
from typing import TYPE_CHECKING, Any, Callable, Dict, Type, Union
from warnings import warn

from typing_extensions import Literal, Protocol, TypedDict
from typing_extensions import Literal, TypeAlias, TypedDict

from ._migration import getattr_migration
from .deprecated.config import BaseConfig
Expand All @@ -18,14 +18,10 @@
__all__ = 'BaseConfig', 'ConfigDict', 'Extra'


class JsonSchemaExtraCallable(Protocol):
@overload
def __call__(self, schema: dict[str, Any]) -> None:
pass

@overload
def __call__(self, schema: dict[str, Any], model_class: type[Any]) -> None:
pass
JsonSchemaExtraCallable: TypeAlias = Union[
Callable[[Dict[str, Any]], None],
Callable[[Dict[str, Any], Type[Any]], None],
]


class _Extra:
Expand Down
4 changes: 2 additions & 2 deletions pydantic/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1300,9 +1300,9 @@ def _update_class_schema(
schema_to_update.update(json_schema_extra)
elif callable(json_schema_extra):
if len(inspect.signature(json_schema_extra).parameters) > 1:
json_schema_extra(schema_to_update, cls)
json_schema_extra(schema_to_update, cls) # type: ignore
else:
json_schema_extra(schema_to_update)
json_schema_extra(schema_to_update) # type: ignore
elif json_schema_extra is not None:
raise ValueError(
f"model_config['json_schema_extra']={json_schema_extra} should be a dict, callable, or None"
Expand Down