Skip to content

Commit

Permalink
Assume builtins don't accept an info argument (pydantic#6754)
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb authored Jul 19, 2023
1 parent c588b5d commit 98a8d56
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pydantic/_internal/_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ def inspect_validator(validator: Callable[..., Any], mode: FieldValidatorModes)
Returns:
Whether the validator takes an info argument.
"""
if getattr(validator, '__module__', None) == 'builtins':
# int, str, etc.
return False
sig = signature(validator)
n_positional = count_positional_params(sig)
if mode == 'wrap':
Expand Down
10 changes: 10 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ class Model(BaseModel):
assert Model(x='1.0').x == 1.0


def test_annotated_validator_builtin() -> None:
"""https://github.com/pydantic/pydantic/issues/6752"""
TruncatedFloat = Annotated[float, BeforeValidator(int)]

class Model(BaseModel):
x: TruncatedFloat

assert Model(x=1.234).x == 1


def test_annotated_validator_plain() -> None:
MyInt = Annotated[int, PlainValidator(lambda x, _info: x if x != -1 else 0)]

Expand Down

0 comments on commit 98a8d56

Please sign in to comment.