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

Closed
sommd opened this issue May 12, 2019 · 3 comments

Comments

Projects
None yet
3 participants
@sommd
Copy link
Contributor

commented May 12, 2019

Bug

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))  # {}
@sommd

This comment has been minimized.

Copy link
Contributor Author

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?

tiangolo added a commit to tiangolo/pydantic that referenced this issue May 14, 2019

@tiangolo tiangolo referenced this issue May 14, 2019

Closed

Add support for arbitrary class instances #520

4 of 4 tasks complete

@samuelcolvin samuelcolvin added the bug label May 15, 2019

@haizaar

This comment has been minimized.

Copy link

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) (#518)
* 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

@haizaar

This comment has been minimized.

Copy link

commented May 18, 2019

gangefors added a commit to gangefors/pydantic that referenced this issue May 31, 2019

Merge remote-tracking branch 'upstream/master'
* upstream/master: (138 commits)
  add 'none-any.whl' to pypi upload (samuelcolvin#564)
  uprev
  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)
  uprev.
  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)
  uprev
  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
You can’t perform that action at this time.