Skip to content

BaseModel.copy(update=…) does not update __fields_set__ #2290

@yurikhan

Description

@yurikhan

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: /usr/local/lib/python3.6/dist-packages/pydantic
               python version: 3.6.9 (default, Oct  8 2020, 12:12:24)  [GCC 8.4.0]
                     platform: Linux-4.15.0-112-generic-x86_64-with-Ubuntu-18.04-bionic
     optional deps. installed: ['typing-extensions', 'email-validator']
from typing import Optional
import pydantic

class Foo(pydantic.BaseModel):
    foo: Optional[str]
    bar: Optional[str]

print(Foo(foo='hello')
      .copy(update={'bar': 'world'})
      .json(exclude_unset=True))
# Observed: {"foo": "hello"}
# Expected: {"foo": "hello", "bar": "world"}

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