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

RecursionError with recursive pydantic model and a default value #3525

Closed
apirogov opened this issue Dec 5, 2022 · 3 comments · Fixed by #3687
Closed

RecursionError with recursive pydantic model and a default value #3525

apirogov opened this issue Dec 5, 2022 · 3 comments · Fixed by #3687
Labels
bug something is clearly wrong here interop how to play nicely with other packages

Comments

@apirogov
Copy link

apirogov commented Dec 5, 2022

Minimal working example:

from pydantic import BaseModel
from hypothesis import given, strategies as st

class Dummy1(BaseModel):
    x: List[Dummy1] = []

Dummy1.update_forward_refs()


@given(st.from_type(Dummy1))
def test_modelgen(obj):
    print(obj.json(indent=2))

results in:

[...]
----
target = just([])

    def forced_value(target):
        try:
            return getattr(target, force_key)
        except AttributeError:
>           return getattr(target, cache_key)
E           RecursionError: maximum recursion depth exceeded while calling a Python object

/.../py3.8/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:105: RecursionError

but without the default value (empty list) it works fine.

Python 3.8
pydantic 1.10.2
pytest 7.0.1
hypothesis 6.58

@Zac-HD Zac-HD added bug something is clearly wrong here interop how to play nicely with other packages labels Dec 5, 2022
@Zac-HD
Copy link
Member

Zac-HD commented Jun 11, 2023

A very similar issue was fixed in #3656, released as version 6.75.9, so I suspect that this is now a problem in Pydantic's Hypothesis plugin.

@jobh
Copy link
Contributor

jobh commented Jun 27, 2023

A very similar issue was fixed in #3656, released as [version 6.75.9]

Actually it seems more related to #3671. When wrapping the recursive type in a list, the required recursion to validate the strategy is roughly doubled, and the default argument roughly doubles it again (due to the one_of(just(defaultval, ...)).

Effectively, this construction requires a recursionlimit of about 1100 to succeed on CPy. It's easy enough to bump it up to that, but where's the limit....?

@jobh
Copy link
Contributor

jobh commented Jun 28, 2023

I spoke too quickly; there's a real issue here, I know what it is and will throw up a PR soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something is clearly wrong here interop how to play nicely with other packages
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants