litestar-org/polyfactory

Enhancement: Expose the faker instance

Closed this issue ยท 3 comments

Summary

Hi, currently switching to PolyFactory from pydantic-factories. ๐ŸŽ‰

Having access to the faker instance for generating data is useful, as was requested in #79 and implemented in #81

Thank you!

Basic Example

class MyFactory(ModelFactory[MyClass]):
    """Factory."""

    __model__ = MyClass

    optional_override1 = ModelFactory.get_faker().pystr

    complex_override = Use(lambda: f"{ModelFactory.get_faker().pystr()}/{ModelFactory.get_faker().pystr()}")

Drawbacks and Impact

No response

Unresolved questions

No response


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

You can already access the faker instance: MyFactory.__faker__. All factories are guaranteed to have a Faker instance as well. You can change the Faker instance by giving a different value for __faker__ as well.

Oh I feel like dunderscore properties would be considered "private" and maybe change unexpectedly. But maybe it's just me. I can work with MyFactory.__faker__.

Thank you

@lindycoder the dunderscore properties are not private and are part of the public API. The reason they're named as such is so that we can easily differentiate between them and the classmethods you may define to generate the value as documented here.