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

.dict(skip_defaults=True) skips fields populated by alias #517

sommd opened this issue May 12, 2019 · 3 comments

.dict(skip_defaults=True) skips fields populated by alias #517

sommd opened this issue May 12, 2019 · 3 comments


Copy link

@sommd sommd commented May 12, 2019


For bugs/questions:

  • OS: Linux

  • Python version import sys; print(sys.version): 3.7.3 (default, Mar 26 2019, 21:43:19) [GCC 8.2.1 20181127]

  • Pydantic version import pydantic; print(pydantic.VERSION): 0.25

When using .dict(skip_defaults=True) with an aliased field, if that field was populated by the alias (i.e. Foo(alias_name=...) instead of Foo(field_name=...)) then it is always skipped. Happens with by_alias set to True and False as well.

from pydantic import BaseModel, Schema

class Foo(BaseModel):
    class Config:
        allow_population_by_alias = True
    a: int = Schema(1, alias="b")

# Correct
print(Foo())  # Foo a=1
print(Foo().dict(by_alias=False, skip_defaults=False))  # {'a': 1}
print(Foo().dict(by_alias=True, skip_defaults=False))  # {'b': 1}
print(Foo().dict(by_alias=False, skip_defaults=True))  # {}
print(Foo().dict(by_alias=True, skip_defaults=True))  # {}

# Correct
print(Foo(a=2))  # Foo a=2
print(Foo(a=2).dict(by_alias=False, skip_defaults=False))  # {'a': 2}
print(Foo(a=2).dict(by_alias=True, skip_defaults=False))  # {'b': 2}
print(Foo(a=2).dict(by_alias=False, skip_defaults=True))  # {'a': 2}
print(Foo(a=2).dict(by_alias=True, skip_defaults=True))  # {'b': 2}

# Correct
print(Foo(b=2))  # Foo a=2
print(Foo(b=2).dict(by_alias=False, skip_defaults=False))  # {'a': 2}
print(Foo(b=2).dict(by_alias=True, skip_defaults=False))  # {'b': 2}
# Incorrect
print(Foo(b=2).dict(by_alias=False, skip_defaults=True))  # {}
print(Foo(b=2).dict(by_alias=True, skip_defaults=True))  # {}
Copy link
Contributor Author

@sommd sommd commented May 12, 2019

After looking into it a bit, it looks like .dict() get's the values of the set fields from __fields_set__. __fields_set__ doesn't contain the name of the field if it was set by the alias. E.g:

print(Foo().__values__)  # {'a': 1}
print(Foo().__fields_set__)  # set()
print(Foo(a=2).__values__)  # {'a': 2}
print(Foo(a=2).__fields_set__)  # {'a'}
print(Foo(b=2).__values__)  # {'a': 2}
print(Foo(b=2).__fields_set__)  # set()

Is the behaviour for __fields_set__ wrong or should .dict() be using something else to get the fields?

Copy link

@haizaar haizaar commented May 16, 2019

Just hit this one as well.

samuelcolvin added a commit that referenced this issue May 18, 2019
* Fix __fields_set__ not using alias field names (#517)

fix #517

* Update HISTORY.rst

* Fix single quotes

* Don't need to check allow_population_by_alias

* Calculate fields_set in validate_model instead

* Fix formatting

* Add breaking change to history

* Minor fixed to validate_mode

* Minor fixes to tests

* Add comments for names_used and fields_set in validate_model
tiangolo added a commit to tiangolo/pydantic that referenced this issue May 18, 2019
Copy link

@haizaar haizaar commented May 18, 2019

gangefors added a commit to gangefors/pydantic that referenced this issue May 31, 2019
* upstream/master: (138 commits)
  add 'none-any.whl' to pypi upload (samuelcolvin#564)
  update benchmarks (samuelcolvin#563)
  cython (samuelcolvin#548)
  Fix issue with unspecified generic type (samuelcolvin#554)
  Run dataclass' original __post_init__ before validation (samuelcolvin#560)
  try to stop annoying warnings in azure pipeline (samuelcolvin#549)
  azure pipeline failOnStderr: false
  Azure Pipelines - tests for windows (samuelcolvin#538)
  Fix JSON Schema for list, tuple, and set, improving interoperability (samuelcolvin#540)
  Colors (samuelcolvin#516)
  Fix to schema generation for IPv{4,6}{Address,Interface,Network} (samuelcolvin#532)
  Fix __fields_set__ not using alias field names (samuelcolvin#517) (samuelcolvin#518)
  Change return type hint for create_model (samuelcolvin#526)
  Tuple ellipsis (samuelcolvin#512)
  Fix to schema generation for IPvAny{Address,Interface,Network} (samuelcolvin#498) (samuelcolvin#510)
  Scheduled monthly dependency update for May (samuelcolvin#499)
  Implement const keyword in Schema. (samuelcolvin#469)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants