Validator of multiple fields does not behave consistently standalone and in another list
JakkuSakura opened this issue · comments
Checks
- I added a descriptive title to this issue
- I have searched (google, github) for similar issues and couldn't find anything
- I have read and followed the docs and still think this is a bug
Bug
Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:
pydantic version: 1.8.2
pydantic compiled: True
install path: /usr/local/lib/python3.9/site-packages/pydantic
python version: 3.9.1 (default, Jan 8 2021, 17:17:43) [Clang 12.0.0 (clang-1200.0.32.28)]
platform: macOS-12.1-x86_64-i386-64bit
optional deps. installed: ['typing-extensions']
...
from pydantic import *
from typing import List
class Foo(BaseModel):
a: str
b: str
@validator("a", "b")
@classmethod
def validate(cls, x) -> str:
assert isinstance(x, str)
return x
class Bar(BaseModel):
foos: List[Foo]
Foo(a="a", b="b")
Foo.parse_obj({
'a': 'a',
'b': 'b'
})
Bar.parse_obj({
'foos': [{
'a': 'a',
'b': 'b'
}]
})
python3 test.py
Traceback (most recent call last):
File "/Users/jack/Dev/test_python/test.py", line 22, in <module>
Bar.parse_obj({
File "pydantic/main.py", line 578, in pydantic.main.BaseModel.parse_obj
File "pydantic/main.py", line 406, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for Bar
foos -> 0
(type=assertion_error)
I get the following on both v1.10 and v2:
from typing import List
from pydantic import BaseModel, validator
class Foo(BaseModel):
a: str
b: str
@validator("a", "b")
@classmethod
def val_a_b(cls, x: str) -> str:
assert isinstance(x, str)
return x
class Bar(BaseModel):
foos: List[Foo]
print(Foo(a="a", b="b"))
"""
a='a' b='b'
"""
print(Foo.parse_obj({'a': 'a', 'b': 'b'}))
"""
a='a' b='b'
"""
print(Bar.parse_obj({'foos': [{'a': 'a', 'b': 'b'}]}))
"""
foos=[Foo(a='a', b='b')]
"""
So it seems like this is fixed.