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

Make ModelWrapValidator protocols generic #7154

Merged
merged 1 commit into from Aug 22, 2023
Merged
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
6 changes: 3 additions & 3 deletions pydantic/functional_validators.py
Expand Up @@ -351,7 +351,7 @@ def __call__( # noqa: D102
...


class ModelWrapValidatorWithoutInfo(Protocol):
class ModelWrapValidatorWithoutInfo(Protocol[_ModelType]):
"""A @model_validator decorated function signature.
This is used when `mode='wrap'` and the function does not have info argument.
"""
Expand All @@ -368,7 +368,7 @@ def __call__( # noqa: D102
...


class ModelWrapValidator(Protocol):
class ModelWrapValidator(Protocol[_ModelType]):
"""A @model_validator decorated function signature. This is used when `mode='wrap'`."""

def __call__( # noqa: D102
Expand Down Expand Up @@ -423,7 +423,7 @@ def __call__( # noqa: D102
ModelAfterValidator = Callable[[_ModelType, _core_schema.ValidationInfo], _ModelType]
"""A `@model_validator` decorated function signature. This is used when `mode='after'`."""

_AnyModelWrapValidator = Union[ModelWrapValidator, ModelWrapValidatorWithoutInfo]
_AnyModelWrapValidator = Union[ModelWrapValidator[_ModelType], ModelWrapValidatorWithoutInfo[_ModelType]]
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this need to be parametrized later?

Choose a reason for hiding this comment

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

From my testing, adding the _ModelType parameter to the overload return type on line 435 seems to give the right results for a correct and incorrect validator (and the after overload does the same thing already):

) -> Callable[[_AnyModelWrapValidator[_ModelType]], _decorators.PydanticDescriptorProxy[_decorators.ModelValidatorDecoratorInfo]]:

That appears to be the only place where these types are used.

_AnyModeBeforeValidator = Union[ModelBeforeValidator, ModelBeforeValidatorWithoutInfo]
_AnyModelAfterValidator = Union[ModelAfterValidator[_ModelType], ModelAfterValidatorWithoutInfo[_ModelType]]

Expand Down