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

computed_fields have inconsistent behaviour between model_dump and model_dump_json (part 2) #8691

Closed
1 task done
Tracked by #8671
Samreay opened this issue Feb 1, 2024 · 2 comments · Fixed by pydantic/pydantic-core#1187
Closed
1 task done
Tracked by #8671
Assignees
Labels
bug V2 Bug related to Pydantic V2

Comments

@Samreay
Copy link

Samreay commented Feb 1, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Was validating the changes made from #8250 and encountered a new issue.

The code submitted as a reproduction in the prior issue now works, so I've increased the complexity by one notch to reproduce a different failure. @sydney-runkle tagging you simply because you were so responsive on the prior issue and Im assuming this will have a similar fix :)

Importantly, the print output agrees between the two methods if you remove the computed function, or move it after y_is_none

I believe the issue is here: https://github.com/pydantic/pydantic-core/pull/1098/files#diff-9d4853412aaf93703485a5ed7c50c373184ec151d1d480a109e52430fedf9ca8R78

Where the code returns after the first None computed field, instead of simply moving onto the next field to check if it is None.

Example Code

from pydantic import BaseModel, computed_field


class Example(BaseModel):
    x: int
    y: int | None

    @computed_field
    def computed(self) -> None:
        return None

    @computed_field
    def y_is_none(self) -> bool:
        return self.y is None


if __name__ == "__main__":
    e = Example(x=1, y=None)

    print(e.model_dump(exclude_none=True))
    # {'x': 1, 'y_is_none': True}

    print(e.model_dump_json(exclude_none=True))
    # {"x":1}

Python, Pydantic & OS Version

pydantic version: 2.6.0
        pydantic-core version: 2.16.1
          pydantic-core build: profile=release pgo=true
                 install path: /home/sam/arenko/trade-selector/.venv/lib/python3.11/site-packages/pydantic
               python version: 3.11.4 (main, Jul  4 2023, 13:46:25) [GCC 11.3.0]
                     platform: Linux-5.15.133.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
             related packages: typing_extensions-4.9.0 fastapi-0.109.0 mypy-1.8.0 pydantic-settings-2.1.0
                       commit: unknown
@Samreay Samreay added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Feb 1, 2024
@sydney-runkle
Copy link
Member

@Samreay,

Thanks for bringing this to our attention! Indeed, looks like this should be a pretty easy fix in pydantic-core.

Would you like to open a PR there with a fix, and we can get that change into our upcoming patch release of pydantic?

@sydney-runkle
Copy link
Member

@Samreay,

I went ahead and opened that PR in core. I've verified that with the changes, here's the behavior in pydantic:

from pydantic import BaseModel, computed_field


class Example(BaseModel):
    x: int
    y: int | None

    @computed_field
    def computed(self) -> None:
        return None

    @computed_field
    def y_is_none(self) -> bool:
        return self.y is None


if __name__ == "__main__":
    e = Example(x=1, y=None)

    print(e.model_dump(exclude_none=True))
    # {'x': 1, 'y_is_none': True}

    print(e.model_dump_json(exclude_none=True))
    # {"x":1,"y_is_none":true}

See the core PR here: pydantic/pydantic-core#1187

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants