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

check ModelField().validate_always when inheriting #1545

Merged
merged 3 commits into from Jun 29, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/1545-dcHHH.md
@@ -0,0 +1 @@
Move the assignment of `field.validate_always` in `fields.py` so the `always` parameter of validators work on inheritance.
8 changes: 4 additions & 4 deletions pydantic/fields.py
Expand Up @@ -354,10 +354,6 @@ def prepare(self) -> None:
# user will need to call model.update_forward_refs()
return

self.validate_always = getattr(self.type_, 'validate_always', False) or any(
v.always for v in self.class_validators.values()
)

if self.required is False and default_value is None:
self.allow_none = True

Expand Down Expand Up @@ -500,6 +496,10 @@ def populate_validators(self) -> None:
and class validators. This method should be idempotent, e.g. it should be safe to call multiple times
without mis-configuring the field.
"""
self.validate_always = getattr(self.type_, 'validate_always', False) or any(
v.always for v in self.class_validators.values()
)

class_validators_ = self.class_validators.values()
if not self.sub_fields or self.shape == SHAPE_GENERIC:
get_validators = getattr(self.type_, '__get_validators__', None)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_validators.py
Expand Up @@ -286,6 +286,25 @@ def check_a(cls, v):
assert check_calls == 2


def test_validate_always_on_inheritance():
check_calls = 0

class ParentModel(BaseModel):
a: str = None

class Model(ParentModel):
@validator('a', pre=True, always=True)
def check_a(cls, v):
nonlocal check_calls
check_calls += 1
return v or 'xxx'

assert Model().a == 'xxx'
assert check_calls == 1
assert Model(a='y').a == 'y'
assert check_calls == 2


def test_validate_not_always():
check_calls = 0

Expand Down