Skip to content

Required optional field has a default value of None after v1.7.0 #2142

@leovp

Description

@leovp

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.7.2
            pydantic compiled: False
                 install path: /home/leovp/Projects/pydantic/pydantic
               python version: 3.6.12 (default, Sep  5 2020, 11:22:16)  [GCC 7.5.0]
                     platform: Linux-4.15.0-54-generic-x86_64-with-debian-buster-sid
     optional deps. installed: []
from typing import Optional

from pydantic import BaseModel


class Model(BaseModel):
    foo: int
    bar: Optional[int] = ...


m = Model(foo=1, bar=None)  # or Model(foo=1)
Model.parse_obj(m.dict(exclude_defaults=True))  # fails on pydantic v1.7.0+

It seems that pydantic thinks bar (a "required optional" field) has a default value of None and serializes the model without this key, which makes it non-deserializable. This was not the case on versions 1.5.x and 1.6.x, so I think it's a regression. (Or maybe it was never a guarantee).

According to git bisect the potential regression was introduced in commit 4bc4230.

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