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

__repr__ recursion #9424

Open
1 task done
sushart opened this issue May 10, 2024 · 3 comments
Open
1 task done

__repr__ recursion #9424

sushart opened this issue May 10, 2024 · 3 comments
Labels
bug V2 Bug related to Pydantic V2

Comments

@sushart
Copy link

sushart commented May 10, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Hi people,
great job so far, but imho there is a recursion issue caused by pydantics __ repr__ implementation, when I need to do some referencing between objects. The code is as example below.

I could fix this issue setting the repr=False, but the problem also appears in the eq when I compare objects.

Thank you for your work.

How to reproduce:

  1. Use Pycharm IDE
  2. Open file
  3. Insert code
  4. run

That implementation causes the PyCharm debugger to fail, even if the code works.

Example Code

from typing import Any

from pydantic import BaseModel
from typing_extensions import Optional

data = {'name': "Babo", "gf": "Babi",
        'friends': [{'name': "Gabi"}, {'name': "Babi"}]}


class Person(BaseModel):
    name: str = "Klause"
    gf: str
    gf_ref: Optional['Friend'] = None
    friends: list['Friend']

    def model_post_init(self, __context: Any) -> None:
        self.set_parent()
        self.find_gf()

    def set_parent(self):
        for friend in self.friends:
            friend.parent = self

    def find_gf(self):
        value = next((val for val in list(self.friends) if val.name == self.gf), None)
        if value:
            self.gf_ref = value


class Friend(BaseModel):
    name: str = "Mausi"
    parent: Optional[Person] = None


sut = Person(**data)

print(sut)

Python, Pydantic & OS Version

pydantic version: 2.7.0
        pydantic-core version: 2.18.1
          pydantic-core build: profile=release pgo=true
                 install path: C:\Users\schwer_a\Documents\Diplom\Repositories\xhw-sysflow-core\venv\Lib\site-packages\pydantic
               python version: 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)]
                     platform: Windows-10-10.0.22631-SP0
             related packages: typing_extensions-4.11.0
                       commit: unknown
@sushart sushart added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels May 10, 2024
@sydney-runkle
Copy link
Member

@sushart,

Hmm, nice find. Definitely a bug. Seems relatively low stakes, so will leave this as a good first issue for anyone interested!

@sydney-runkle sydney-runkle removed the pending Awaiting a response / confirmation label May 16, 2024
@eugenetriguba
Copy link

@sushart In this example, I'm curious what the output you're expecting is? The reason I ask is because the model has a cycle. The parent of gf_ref is set to the root object and so when gf_ref is attempted to be serialized, it attempts to serialize the root object again and then it attempts to serialize gf_ref again and so on.

@sydney-runkle Is an error here not the appropriate response? How were you thinking these cycles should be handled differently?

@sushart
Copy link
Author

sushart commented May 19, 2024

@eugenetriguba I would expect that I can chose which elements are involved in the compare process or if done automatically recursion is recognised and interrupted. Like repr=False compare=False wouldn't it be a nice and worthy feature?

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

No branches or pull requests

3 participants