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
RecursionError in combination with Dataclasses and create_model #4949
Comments
You can resolve this by removing the extra Might have similar origin to #4907. |
I applied both decorators to shorten the example. In my actual code I can't remove the extra
P.S. Your response time is impressive! |
That was luck, often it's not so good. :-)
As mentioned on the linked issue, the current dataclass support is very
involved, most of our bug fixes have been related to trying to patch
dataclass edge cases.
I'd strongly prefer not to change dataclass support again in v1, but
hopefully we can fix this properly in v2.
Unless we can find a trivial solution.
…On Fri, 13 Jan 2023, 16:34 mbillingr, ***@***.***> wrote:
I applied both decorators to shorten the example. In my actual code I
can't remove the extra dataclass because I have the following situation:
# can't change this definition
@dataclasses.dataclass
class ValueObj:
name: str
# IO module
ValueObj = pydantic.dataclasses.dataclass(domain.ValueObj)
P.S. Your response time is impressive!
—
Reply to this email directly, view it on GitHub
<#4949 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA62GGN3EVBLS5NK3MWUXSDWSF7XPANCNFSM6AAAAAAT2SPHYU>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
I can understand that you don't feel the urge to touch the dataclass support in v1. For me it's already a win to know that this is really a bug and not just me misusing your API. If it can't be fixed upstream I'll find a workaround. (Even though I'm not particularly fond of the idea, it should be easy to just duplicate the Starting Monday, I could free some time to look into this in more detail. Would you accept a pull request if I managed to find a simple fix? (I don't expect to, but you never know... :)) |
Possibly, it's really up to @PrettyWood who is the "master of dataclasses". |
I dug deeper and found that the recursion occurs while deep-copying a I think this problem could be solved by adding a class DataclassProxy:
# ...
def __deepcopy__(self, memo: Any) -> "DataclassProxy":
return DataclassProxy(deepcopy(self.__dataclass__, memo)) This seems to fix my example above (I did not check if it breaks anything else, though). |
Yup @mbillingr ! Seems like the right fix 👍 |
I guess it's up to you then to decide what to do next... |
I'm pretty sure this will become obsolete in V2, as the dataclass implementation will be radically simplified. But if you'd like to create a PR again the |
* add test to reproduce #4949 * fix infinitely recursive deepcopy * add test for shallow copy of wrapped dataclass * fix infinitely recursive copy of wrapped dataclass --------- Co-authored-by: Martin Billinger-Finke <2392396+mbillingr@users.noreply.github.com>
fixed by #4963 |
Initial Checks
Description
I'm getting a
RecursionError
when callingpydantic.create_model
with a__base__
class that contains fields which are both pydantic and vanilla dataclasses.Most of the recursion related problems I could find were related to recursive data types (such as #1370). There is no (obvious) recursion in the data structures I use.
There is an easy workaround: Don't use both decorators.
Unfortunately, I need both because the class is defined in another module and I need to wrap it with pydantic for I/O validation.
Originally, I ran into this problem using FastAPI, but managed to reproduce it using pydantic alone. So I hope this is the correct place to ask.
Example Code
Python, Pydantic & OS Version
Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: