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(model) errors when field name keys exists #10575

Closed
1 task done
zzstoatzz opened this issue Oct 8, 2024 · 4 comments
Closed
1 task done

dict(model) errors when field name keys exists #10575

zzstoatzz opened this issue Oct 8, 2024 · 4 comments
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation

Comments

@zzstoatzz
Copy link
Contributor

zzstoatzz commented Oct 8, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

A user of ours opened this issue, which appeared to show that dumping a model with dict(...) breaks if you have field on the model whose name is literally keys - which I was able to reproduce.

This error was certainly unexpected to me - I couldn't find an open or closed related issue, but apologies if I'm missing something. I couldn't immediately find a fix for this looking at BaseModel.__iter__ and a bit elsewhere, but will come back to this to try and add a fix later.

Example Code

from pydantic import BaseModel

errored = False


class A(BaseModel):
    keys: str


a = A(keys="hello")

assert a.model_dump() == {"keys": "hello"}

try:
    dict(a)
except TypeError as exc:
    errored = True
    assert "'str' object is not callable" in str(exc)
    print(f"`dict(A(keys='hello'))` errored with {exc}")

assert errored

Python, Pydantic & OS Version

             pydantic version: 2.9.2
        pydantic-core version: 2.23.4
          pydantic-core build: profile=release pgo=false
                 install path: /Users/nate/github.com/prefecthq/prefect/.venv/lib/python3.12/site-packages/pydantic
               python version: 3.12.5 (main, Aug  6 2024, 19:08:49) [Clang 15.0.0 (clang-1500.3.9.4)]
                     platform: macOS-14.6.1-arm64-arm-64bit
             related packages: fastapi-0.115.0 mypy-1.11.2 pydantic-extra-types-2.9.0 pydantic-settings-2.5.2 typing_extensions-4.12.2
                       commit: unknown

@zzstoatzz zzstoatzz added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Oct 8, 2024
@zzstoatzz zzstoatzz changed the title dict(model) errors when keys field name exists dict(model) errors when field name keys exists Oct 8, 2024
@Viicos
Copy link
Member

Viicos commented Oct 9, 2024

Hello @zzstoatzz,

you are hitting an annoying Python behavior reported here. I'm going to try to update the Python documentation because the behavior is probably going to stay the same.

As a workaround (which I'm going to document in the Pydantic documentation), you can call dict(iter(model)).

@sydney-runkle
Copy link
Member

Closing for now, I think this issue represents good enough documentation of this problem + workaround. Thanks @Viicos!

@zzstoatzz
Copy link
Contributor Author

thanks for the info @Viicos - sounds good! 👍

@Viicos
Copy link
Member

Viicos commented Oct 9, 2024

Docs PR, hopefully this will be clarified: python/cpython#125213

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 pending Awaiting a response / confirmation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants