-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Closed
Labels
Milestone
Description
Bug
Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":
pydantic version: 1.5.1
pydantic
78A0
compiled: True
install path: /Users/samuel/Desktop/venv/lib/python3.8/site-packages/pydantic
python version: 3.8.3 (default, May 16 2020, 22:17:30) [Clang 11.0.3 (clang-1103.0.32.59)]
platform: macOS-10.15.4-x86_64-i386-64bit
optional deps. installed: []
I use a validator on a subclass and reference a List attribute on the parent class. However, the validator does not get called.
from typing import List
from pydantic import BaseModel, validator
class Thing1(BaseModel):
item: str
item2: List[str]
class Thing2(Thing1):
@validator("item2", for_each=True)
def check_item(cls, v, values):
assert v == values.get("item")
# This will NOT raise a ValidationError like expected; it won't even call the validator, so this is a silent error
hat = Thing2(item="hi", item2=["hi", "h"])A workaround is to just treat v as a list (leave each_item=False):
from typing import List
from pydantic import BaseModel, validator
class Thing1(BaseModel):
item: str
item2: List[str]
class Thing2(Thing1):
@validator("item2")
def check_item(cls, v, values):
for string in v:
assert v == values.get("item")
return v
# This will not raise a ValidationError as expected
cat = Thing2(item="hi", item2["hi", "hi"])
# This will raise a ValidationError as expected
hat = Thing2(item="hi", item2=["hi", "h"])