You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 # worksfrompydanticimportFieldfrompydantic.dataclassesimportdataclass# blows up@dataclassclassThing:
i:int@dataclassclassOther:
Thing: Thing=Field() # Works if Field is no present
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.
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.
Initial Checks
Description
If a field has the same name as its type, and is defined with
Field()
, then aRecursionError
happens,In the example below:
dataclass
orBaseModel
=Field()
is not given, this does not happenint: int
orPath:Path
withfrom 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:
Which is the case if I use python's dataclasses.
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: