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

Infinite recursion error if a field has the same name as its type #8240

Closed
1 task done
guillaume-alliander opened this issue Nov 28, 2023 · 3 comments
Closed
1 task done
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation

Comments

@guillaume-alliander
Copy link

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

If a field has the same name as its type, and is defined with Field(), then a RecursionError happens,

In the example below:

  • this is true with pydantic dataclass or BaseModel
  • using the default python dataclass, this does not happen
  • if =Field() is not given, this does not happen
  • it seems to only happen if the type of the field is another Pydantic object (int: int or Path:Path with from pathlib import Path are both fine)

The given snippet is enough to blow python up.

My expectation would be to see "42" if I add:

o=Other(Thing=Thing(i=42))
print(o.Thing.i)

Which is the case if I use python's dataclasses.

Example Code

# from dataclasses import dataclass # works
from pydantic import Field
from pydantic.dataclasses import dataclass  # blows up


@dataclass
class Thing:
    i:int

@dataclass
class Other:
    Thing: Thing=Field() # Works if Field is no present

Python, Pydantic & OS Version

python -c "import pydantic.version; print(pydantic.version.version_info())"                                                             0s - 11:48:19
             pydantic version: 2.5.0
        pydantic-core version: 2.14.1
          pydantic-core build: profile=release pgo=true
                 install path: /home/gui/.conda/envs/pycgmes10/lib/python3.10/site-packages/pydantic
               python version: 3.10.11 (main, Apr 20 2023, 19:02:41) [GCC 11.2.0]
                     platform: Linux-5.10.102.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
             related packages: pyright-1.1.335 typing_extensions-4.8.0 mypy-1.5.1
@guillaume-alliander guillaume-alliander added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Nov 28, 2023
@Viicos
Copy link
Contributor

Viicos commented Nov 28, 2023

Duplicate of: #7327 (you will find explanation and related issues here).
I'll take a look to see if it's possible to have a better error message here as this issue comes up pretty often.

@sydney-runkle
Copy link
Member

I'm wondering if it makes sense to have a "common mistakes" section of the docs that highlights issues like this one that we see pop up more than once a month.

@sydney-runkle
Copy link
Member

Marking as fixed by #8243

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

No branches or pull requests

3 participants