-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Closed
Labels
bug V1Bug related to Pydantic V1.XBug related to Pydantic V1.X
Description
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)) # {}Metadata
Metadata
Assignees
Labels
bug V1Bug related to Pydantic V1.XBug related to Pydantic V1.X