Skip to content

validate_assignment changes the field order #2073

Closed

Description

@coldino

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

             pydantic version: 1.7.1
            pydantic compiled: True
                 install path: /home/user/work/bugs/python/pydantic-ordering/.venv/lib/python3.8/site-packages/pydantic
               python version: 3.8.5 (default, Jul 28 2020, 12:59:40)  [GCC 9.3.0]
                     platform: Linux-4.19.128-microsoft-standard-x86_64-with-glibc2.29
     optional deps. installed: []

Including validate_assignment = True in a model's config seems to cause assignment to change the field order when using dict(). This change was discovered when upgrading to 1.7.0 and also applies to 1.7.1, but was not present in prior versions.

This behaviour does not seem to agree with https://pydantic-docs.helpmanual.io/usage/models/#field-ordering, and is something that is important for my uses (where it generates massive diffs in the output) and surely others.

Running this snippet with and without the validate_assignment line produces different output:

from pydantic import BaseModel

class Model(BaseModel):
    title: str = ''
    name: str = ''

    class Config:
        validate_assignment = True
        ...

m = Model(name='name')
m.title = 'title'
print(m.dict())

Activity

  1. added a commit that references this issue on Oct 30, 2020
    24878ee
  2. PrettyWood commented on Oct 30, 2020

    @PrettyWood
    Contributor

    Hi @coldino
    Thanks for reporting!
    I just made a fix for this, which may well be added in v1.7.2 coming out in the next few days.

  3. coldino commented on Oct 30, 2020

    @coldino
    Author

    Excellent. Thank you very much for all your hard work.

  4. added a commit that references this issue on Oct 31, 2020
    4680940
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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