-
-
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
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.3
pydantic compiled: True
install path: C:\Users\JonasTK\miniconda3\envs\testenv\Lib\site-packages\pydantic
python version: 3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)]
platform: Windows-10-10.0.19041-SP0
optional deps. installed: ['typing-extensions']
I know this is (sort of) showcased in the docs, but I still think it is a problem that .construct() does not give correct field order when there are fields with default values. Most of the time this is not a problem: comparing instances and .dict() outputs do not depend on the field order. But for JSON export, it does matter.
Shouldn't the JSON export from an instance generated via .construct() with pre-validated input be identical to the JSON export of a properly validated instance?
from pydantic import BaseModel
class Foo(BaseModel):
a: int
b: int = 42
c: float
instance = Foo(a=1, c=3.14) # Correct field order
instance_construct = Foo.construct(**instance.dict()) # Incorrect field order
print(instance) # Output: a=1 b=42 c=3.14
print(instance_construct) # Output: b=42 a=1 c=3.14
print(instance.json()) # Output: {"a": 1, "b": 42, "c": 3.14}
print(instance_construct.json()) # Output: {"b": 42, "a": 1, "c": 3.14}
assert instance == instance_construct # No Error
assert instance.dict() == instance_construct.dict() # No Error
assert instance.json() == instance_construct.json() # AssertionErrorMetadata
Metadata
Assignees
Labels
bug V1Bug related to Pydantic V1.XBug related to Pydantic V1.X