Skip to content

Commit

Permalink
Root validator signature (#5294)
Browse files Browse the repository at this point in the history
* Fix type hints for root_validator

* Add mypy test

* Fix mypy test
  • Loading branch information
dmontagu committed Mar 28, 2023
1 parent 98427b3 commit d26d58d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
6 changes: 2 additions & 4 deletions pydantic/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def root_validator(
*,
# if you don't specify `pre` the default is `pre=False`
# which means you need to specify `skip_on_failure=True`
skip_on_failure: Literal[True] = ...,
skip_on_failure: Literal[True],
allow_reuse: bool = ...,
) -> Callable[[_V1RootValidatorFunctionType], _V1RootValidatorFunctionType,]:
...
Expand All @@ -230,11 +230,9 @@ def root_validator(
@overload
def root_validator(
*,
# for pre=False you don't need to specify `skip_on_failure`
# if you specify `pre=True` then you don't need to specify
# `skip_on_failure`, in fact it is not allowed as an argument!
pre: Literal[True],
skip_on_failure: Literal[True],
allow_reuse: bool = ...,
) -> Callable[[_V1RootValidatorFunctionType], _V1RootValidatorFunctionType,]:
...
Expand All @@ -246,7 +244,7 @@ def root_validator(
# if you explicitly specify `pre=False` then you
# MUST specify `skip_on_failure=True`
pre: Literal[False],
skip_on_failure: Literal[True] = ...,
skip_on_failure: Literal[True],
allow_reuse: bool = ...,
) -> Callable[[_V1RootValidatorFunctionType], _V1RootValidatorFunctionType,]:
...
Expand Down
22 changes: 21 additions & 1 deletion tests/mypy/modules/fail4.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from pydantic import validate_arguments
from typing import Any

from pydantic import BaseModel, root_validator, validate_arguments


@validate_arguments
Expand All @@ -24,3 +26,21 @@ def bar() -> str:

# return type should be a string
y: int = bar()


# Demonstrate type errors for root_validator signatures
class Model(BaseModel):
@root_validator()
@classmethod
def validate_1(cls, values: Any) -> Any:
return values

@root_validator(pre=True, skip_on_failure=True)
@classmethod
def validate_2(cls, values: Any) -> Any:
return values

@root_validator(pre=False)
@classmethod
def validate_3(cls, values: Any) -> Any:
return values
2 changes: 1 addition & 1 deletion tests/mypy/modules/success.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def check_age(cls, value: int) -> int:
def root_check(cls, values: Dict[str, Any]) -> Dict[str, Any]:
return values

@root_validator(pre=True, skip_on_failure=True, allow_reuse=False)
@root_validator(pre=True, allow_reuse=False)
def pre_root_check(cls, values: Dict[str, Any]) -> Dict[str, Any]:
return values

Expand Down
31 changes: 23 additions & 8 deletions tests/mypy/outputs/latest/fail4.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
5: note: "foo" defined here
12: error: Argument 1 to "foo" has incompatible type "str"; expected "int" [arg-type]
13: error: Argument "c" to "foo" has incompatible type "int"; expected "str" [arg-type]
14: error: Too many positional arguments for "foo" [misc]
14: error: Argument 2 to "foo" has incompatible type "int"; expected "str" [arg-type]
15: error: Unexpected keyword argument "d" for "foo" [call-arg]
17: error: "Callable[[int, DefaultNamedArg(str, 'c')], str]" has no attribute "raw_function" [attr-defined]
26: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
7: note: "foo" defined here
14: error: Argument 1 to "foo" has incompatible type "str"; expected "int" [arg-type]
15: error: Argument "c" to "foo" has incompatible type "int"; expected "str" [arg-type]
16: error: Too many positional arguments for "foo" [misc]
16: error: Argument 2 to "foo" has incompatible type "int"; expected "str" [arg-type]
17: error: Unexpected keyword argument "d" for "foo" [call-arg]
19: error: "Callable[[int, DefaultNamedArg(str, 'c')], str]" has no attribute "raw_function" [attr-defined]
28: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
33: error: All overload variants of "root_validator" require at least one argument [call-overload]
33: note: Possible overload variants:
33: note: def root_validator(*, skip_on_failure: Literal[True], allow_reuse: bool = ...) -> Callable[[_V1RootValidatorFunctionType], _V1RootValidatorFunctionType]
33: note: def root_validator(*, pre: Literal[True], allow_reuse: bool = ...) -> Callable[[_V1RootValidatorFunctionType], _V1RootValidatorFunctionType]
33: note: def root_validator(*, pre: Literal[False], skip_on_failure: Literal[True], allow_reuse: bool = ...) -> Callable[[_V1RootValidatorFunctionType], _V1RootValidatorFunctionType]
38: error: No overload variant of "root_validator" matches argument types "bool", "bool" [call-overload]
38: note: Possible overload variants:
38: note: def root_validator(*, skip_on_failure: Literal[True], allow_reuse: bool = ...) -> Callable[[_V1RootValidatorFunctionType], _V1RootValidatorFunctionType]
38: note: def root_validator(*, pre: Literal[True], allow_reuse: bool = ...) -> Callable[[_V1RootValidatorFunctionType], _V1RootValidatorFunctionType]
38: note: def root_validator(*, pre: Literal[False], skip_on_failure: Literal[True], allow_reuse: bool = ...) -> Callable[[_V1RootValidatorFunctionType], _V1RootValidatorFunctionType]
43: error: No overload variant of "root_validator" matches argument type "bool" [call-overload]
43: note: Possible overload variants:
43: note: def root_validator(*, skip_on_failure: Literal[True], allow_reuse: bool = ...) -> Callable[[_V1RootValidatorFunctionType], _V1RootValidatorFunctionType]
43: note: def root_validator(*, pre: Literal[True], allow_reuse: bool = ...) -> Callable[[_V1RootValidatorFunctionType], _V1RootValidatorFunctionType]
43: note: def root_validator(*, pre: Literal[False], skip_on_failure: Literal[True], allow_reuse: bool = ...) -> Callable[[_V1RootValidatorFunctionType], _V1RootValidatorFunctionType]

0 comments on commit d26d58d

Please sign in to comment.