-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Closed
Labels
bug V1Bug related to Pydantic V1.XBug related to Pydantic V1.X
Description
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())":
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
pydantic version: 1.6.1
pydantic compiled: True
install path: /homes/<redacted>/.conda/envs/<redacted>/lib/python3.7/site-packages/pydantic
python version: 3.7.8 | packaged by conda-forge | (default, Jul 31 2020, 02:25:08) [GCC 7.5.0]
platform: Linux-4.9.0-0.bpo.6-amd64-x86_64-with-debian-8.11
optional deps. installed: ['typing-extensions']
Hi,
In a validator method, when adding the values parameter, I expect it to be a map from field names to validated types.
When specifying validate_assignment = True in model config, values gets a different type (just on assignment).
This is illustrated by the example below:
import pydantic
class ModelOne(pydantic.BaseModel):
a: int
class ModelTwo(pydantic.BaseModel):
m: ModelOne
b: int
@pydantic.validator('b')
def validate_b(cls, b, values):
print(values)
if 'm' in values:
return b + values['m'].a # this fails with AttributeError if values['m'] is a dict
else:
return b
class Config:
validate_assignment = True
model = ModelTwo(m=ModelOne(a=1), b=2)
#> {'m': ModelOne(a=1)}
model.b = 3
#> {'m': {'a': 1}}As far as I can tell, this behavior is not documented, and I'm pretty sure it's not intended.
edit: created a PR
Metadata
Metadata
Assignees
Labels
bug V1Bug related to Pydantic V1.XBug related to Pydantic V1.X