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

Bug: Strange behavior with self-reference model #583

Closed
1 of 4 tasks
FredrikBakken opened this issue Sep 13, 2024 · 1 comment · Fixed by #584
Closed
1 of 4 tasks

Bug: Strange behavior with self-reference model #583

FredrikBakken opened this issue Sep 13, 2024 · 1 comment · Fixed by #584
Labels
bug Something isn't working

Comments

@FredrikBakken
Copy link

FredrikBakken commented Sep 13, 2024

Description

Encountering some strange errors with self-referencing models.

In the first case, there seems to be a 50/50 chance for the pytest to work:

from __future__ import annotations
from typing import Optional, Dict

import pydantic as pyd

from polyfactory.factories.pydantic_factory import ModelFactory


class Bar(pyd.BaseModel):
    fields: Dict[str, Foo] = pyd.Field(default_factory=dict)

class Foo(pyd.BaseModel):
    sometimes: Optional[Bar] = pyd.Field(default=None, exclude=True)
    # never: Dict[str, Bar] = pyd.Field(default_factory=dict)

class FooFactory(ModelFactory[Foo]):
    __model__ = Foo

def test_foo():
    foo_instance = FooFactory.build()
    assert isinstance(foo_instance, Foo)

In the second case, the pytest seems to never work:

from __future__ import annotations
from typing import Optional, Dict

import pydantic as pyd

from polyfactory.factories.pydantic_factory import ModelFactory


class Bar(pyd.BaseModel):
    fields: Dict[str, Foo] = pyd.Field(default_factory=dict)

class Foo(pyd.BaseModel):
    # sometimes: Optional[Bar] = pyd.Field(default=None, exclude=True)
    never: Dict[str, Bar] = pyd.Field(default_factory=dict)


class FooFactory(ModelFactory[Foo]):
    __model__ = Foo

def test_foo():
    foo_instance = FooFactory.build()
    assert isinstance(foo_instance, Foo)

Release Version

2.16.2

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Note

While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@FredrikBakken FredrikBakken added the bug Something isn't working label Sep 13, 2024
@FredrikBakken FredrikBakken changed the title Bug: Strange behavior with self-reference Bug: Strange behavior with self-reference model Sep 13, 2024
@adhtruong
Copy link
Collaborator

Thanks for reporting @FredrikBakken .

  • The first case for optional is definitely a bug and PR is in
  • Second case is a little trickier. For a workaround in the meantime setting never = {} should work and would be the behaviour. Right now polyfactory doesn't support nested recursive models other than in trivial cases where there is an obvious out e.g. choosing None in the case of the sometimes in the example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants