Skip to content

BaseModel.construct returns instances with None assigned to fields with a default_factory #1732

@zoopp

Description

@zoopp

Bug

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

             pydantic version: 1.6.1
            pydantic compiled: True
                 install path: /home/mihai/.virtualenvs/pydantic/lib/python3.8/site-packages/pydantic
               python version: 3.8.3 (default, May 17 2020, 18:15:42)  [GCC 10.1.0]
                     platform: Linux-5.7.7-1-ck-x86_64-with-glibc2.2.5
     optional deps. installed: ['devtools']
from pydantic import BaseModel, Field
from typing import List

class Model(BaseModel):
    foo: List[int] = Field(default_factory=list)
    bar: str = 'Baz'

print(Model.construct()) 

# in pydantic   1.5.1 the result is: foo=[] bar='Baz'
# in pydantic  ^1.6.0 the result is: foo=None bar='Baz'

Hello, I'm using pydantic in an application and after updating to v1.6.1 from v1.5.1 I came across this situation which results in a couple of failures.

From the changelog and documentation it's not clear if this change was intended or not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions