From 54568aeb967187cd5ef910881cf0594f3a170179 Mon Sep 17 00:00:00 2001 From: dchhh Date: Thu, 21 May 2020 21:47:41 +0800 Subject: [PATCH 1/3] fix issure #1155 --- pydantic/fields.py | 8 ++++---- tests/test_validators.py | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/pydantic/fields.py b/pydantic/fields.py index c49ad7f047..37f8c1ea8b 100644 --- a/pydantic/fields.py +++ b/pydantic/fields.py @@ -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 @@ -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) diff --git a/tests/test_validators.py b/tests/test_validators.py index 386fbbeb77..28d05c62e3 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -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 From 627948591cae54330b3ba812d8c5cf56b15a3641 Mon Sep 17 00:00:00 2001 From: dchhh Date: Sun, 28 Jun 2020 23:35:53 +0800 Subject: [PATCH 2/3] add changes.1545-dcHHH.md --- changes/1545-dcHHH.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/1545-dcHHH.md diff --git a/changes/1545-dcHHH.md b/changes/1545-dcHHH.md new file mode 100644 index 0000000000..748509f171 --- /dev/null +++ b/changes/1545-dcHHH.md @@ -0,0 +1 @@ +Move the assignment of field.validate_always in fields.py to make 'always' parameter of validator works on inheritance. \ No newline at end of file From 0d5caf2f8afc5e0b2f7e56be8889eaa9128da09d Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Mon, 29 Jun 2020 09:26:13 +0100 Subject: [PATCH 3/3] improve change description --- changes/1545-dcHHH.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changes/1545-dcHHH.md b/changes/1545-dcHHH.md index 748509f171..a5ccad5f78 100644 --- a/changes/1545-dcHHH.md +++ b/changes/1545-dcHHH.md @@ -1 +1 @@ -Move the assignment of field.validate_always in fields.py to make 'always' parameter of validator works on inheritance. \ No newline at end of file +Move the assignment of `field.validate_always` in `fields.py` so the `always` parameter of validators work on inheritance.