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

Pydantic 1.6: fields with default_factory cause parse_obj() to set fields to unparsed values #1717

Closed
johnanthonyowens opened this issue Jul 13, 2020 · 1 comment · Fixed by #1712
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@johnanthonyowens
Copy link

johnanthonyowens commented Jul 13, 2020

Bug

This is likely related to #1710 but does not explicitly involve validators. This would be a good candidate for a unit test to prevent future regressions.

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

pydantic version: 1.6
pydantic compiled: True
install path: /usr/local/lib/python3.8/site-packages/pydantic
python version: 3.8.3 (default, May 16 2020, 07:08:28)  [GCC 8.3.0]
platform: Linux-4.15.0-109-generic-x86_64-with-glibc2.2.5
optional deps. installed: []

Here is a self-contained example illustrating the problem:

from pydantic import BaseModel, Field

class Inner(BaseModel):
    val: int = Field(0)

class Outer(BaseModel):
    inner_1: Inner = Field(default_factory=Inner)
    inner_2: Inner = Field(Inner())

default = Outer().dict() # default == {'inner_1': {'val': 0}, 'inner_2': {'val': 0}}
parsed = Outer.parse_obj(default)

print(parsed)

In 1.6, this prints:
inner_1={'value': 'default'} inner_2=Inner(value='default')
Note that inner_1 is a dict, not an Inner object!

In 1.5.1 this prints:
inner_1=Inner(val=0) inner_2=Inner(val=0)

@johnanthonyowens johnanthonyowens added the bug V1 Bug related to Pydantic V1.X label Jul 13, 2020
PrettyWood added a commit to PrettyWood/pydantic that referenced this issue Jul 13, 2020
@PrettyWood
Copy link
Member

PrettyWood commented Jul 13, 2020

Hello @johnanthonyowens you are right !
I added your example in the unit tests of the fix 16e489b and updated the changelog.
Thanks for the suggestion as the regression did not only impact the validation of nested models but also the parsing!

PrettyWood added a commit to PrettyWood/pydantic that referenced this issue Jul 13, 2020
samuelcolvin added a commit that referenced this issue Jul 15, 2020
…#1712)

* fix: validate nested models with `default_factory`

PR #1504 introduced a regression by bypassing `populate_validators()`,
which would skip the validation of children in nested models
with `default_factory`

closes #1710

* test: add example of nested models parsing with `default_factory`

closes #1717

* add testcase from #1722

* bodge for benchmarks

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants