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

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

Closed
3 tasks done
leovp opened this issue Nov 23, 2020 · 3 comments · Fixed by #2143
Closed
3 tasks done

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

leovp opened this issue Nov 23, 2020 · 3 comments · Fixed by #2143
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@leovp
Copy link

leovp commented Nov 23, 2020

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.

@leovp leovp added the bug V1 Bug related to Pydantic V1.X label Nov 23, 2020
@leovp
Copy link
Author

leovp commented Nov 23, 2020

https://github.com/samuelcolvin/pydantic/blob/31bc2435d7968fdf8d1f0c5c67f0f851c1bef54e/pydantic/main.py#L789

Alright, I haven't looked really closely, but here's my guess: getattr here can never fallback to _missing, since ModelField.default is always there and has a value of None unless set explicitly:
https://github.com/samuelcolvin/pydantic/blob/31bc2435d7968fdf8d1f0c5c67f0f851c1bef54e/pydantic/fields.py#L255

UPD1: And here's where "bar" is created and gets the default value of None: https://github.com/samuelcolvin/pydantic/blob/31bc2435d7968fdf8d1f0c5c67f0f851c1bef54e/pydantic/fields.py#L308-L310

UPD2: Is it a good idea to check field.field_info.default instead of field.default? In this particular case it has a correct value of Ellipsis, and you could special-case it while serialising the model.

@PrettyWood
Copy link
Member

Hello @leovp and thanks for reporting :)
You're absolutely right! IMO the good fix would be to set default = Ellipsis when required
Would you like to make a PR for this? If you want I can do it as well tonight for a v1.7.3.
Please tell me.
Cheers

@leovp
Copy link
Author

leovp commented Nov 23, 2020

@PrettyWood
It's 11PM where I live, so please go ahead :)

PrettyWood added a commit to PrettyWood/pydantic that referenced this issue Nov 23, 2020
samuelcolvin added a commit that referenced this issue Jan 1, 2021
* fix: ignore `__doc__` as valid private attribute (#2091)

closes #2090

* Fixes a regression where Enum fields would not propagate keyword arguments to the schema (#2109)

fix #2108

* Fix schema extra not being included when field type is Enum

* Code format

* More code format

* Add changes file

Co-authored-by: Ben Martineau <b.martineau@iwoca.co.uk>

* fix: update all modified field values in `root_validator` when `validate_assignment` is on (#2119)

* fix: update all modified field values in `root_validator` when `validate_assignment` is on

closes #2116

* chore: update root_validator name

Co-authored-by: Arthur Pastel <arthur.pastel@gmail.com>

Co-authored-by: Arthur Pastel <arthur.pastel@gmail.com>

* fix: support `underscore_attrs_are_private` with generic models (#2139)

closes #2138

* fix: set right default value for required (optional) fields (#2143)

closes #2142

* Fix #2111: support pickle for built-in dataclasses (#2114)

* 2111: support pickle for built-in dataclasses

* 2111: add changes

* 2111: simplify test

* return original name + handle similar names

* add additional check

* fix a misspell

* remove useless f-string

* cleanup test

Co-authored-by: Samuel Colvin <s@muelcolvin.com>

* uprev and add history

* tempoarily pin pip

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
Co-authored-by: Ben Martineau <bm424@cam.ac.uk>
Co-authored-by: Ben Martineau <b.martineau@iwoca.co.uk>
Co-authored-by: Arthur Pastel <arthur.pastel@gmail.com>
Co-authored-by: aimestereo <aimestereo@gmail.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