{"id":1631,"library":"polyfactory","title":"Polyfactory","description":"Polyfactory is a versatile Python library for generating mock data, primarily used for testing and development. It supports various data models, including Pydantic, SQLAlchemy, and Dataclasses, offering a flexible API for customization. The current version is 3.3.0, and it maintains an active release cadence with regular updates and feature enhancements.","status":"active","version":"3.3.0","language":"en","source_language":"en","source_url":"https://github.com/litestar-org/polyfactory","tags":["mocking","testing","data generation","factories","pydantic","sqlalchemy","dataclasses"],"install":[{"cmd":"pip install polyfactory","lang":"bash","label":"Core library"},{"cmd":"pip install 'polyfactory[pydantic]'","lang":"bash","label":"With Pydantic support (common)"}],"dependencies":[{"reason":"Commonly used for model definition, enabling ModelFactory functionality.","package":"pydantic","optional":true}],"imports":[{"symbol":"ModelFactory","correct":"from polyfactory.factories import ModelFactory"},{"note":"The generic 'Factory' class was renamed to 'PolyFactory' in v3.0.0.","wrong":"from polyfactory.factories.base import Factory","symbol":"PolyFactory","correct":"from polyfactory.factories import PolyFactory"},{"symbol":"SyncPersistenceStrategy","correct":"from polyfactory.persistence import SyncPersistenceStrategy"},{"symbol":"AsyncPersistenceStrategy","correct":"from polyfactory.persistence import AsyncPersistenceStrategy"}],"quickstart":{"code":"from polyfactory.factories import ModelFactory\nfrom pydantic import BaseModel\nfrom datetime import date\n\nclass User(BaseModel):\n    name: str\n    email: str\n    age: int\n    is_active: bool = True\n    joined_date: date\n\n# Build a single instance (in-memory, no persistence)\nuser_instance = ModelFactory.build(User)\nprint(f\"Generated User: {user_instance.model_dump_json(indent=2)}\")\n\n# Build multiple instances (in-memory list)\nusers_batch = ModelFactory.batch(User, size=3)\nprint(f\"\\nGenerated Users Batch (first item): {users_batch[0].model_dump_json(indent=2)}\")","lang":"python","description":"This example demonstrates how to use `ModelFactory` to generate mock Pydantic model instances. `ModelFactory.build()` creates a single instance in memory, while `ModelFactory.batch()` creates a list of instances. For persistence, a `PersistenceStrategy` would be required with `ModelFactory.create()`."},"warnings":[{"fix":"Update imports from `from polyfactory.factories.base import Factory` to `from polyfactory.factories import PolyFactory`.","message":"The generic `Factory` class was renamed to `PolyFactory`.","severity":"breaking","affected_versions":"<3.0.0"},{"fix":"When using `create()`, you must now pass a persistence strategy (e.g., `SyncPersistenceStrategy`) as an argument. The `use_api_client` parameter was also removed in v3.0.0.","message":"The `create()` method now explicitly requires a persistence strategy.","severity":"breaking","affected_versions":"<3.0.0"},{"fix":"`build()` generates an in-memory instance without any persistence, useful for simple object creation. `create()` is designed to persist the generated data and requires a `PersistenceStrategy` to do so (e.g., saving to a database or calling an API).","message":"Confusion between `build()` and `create()` methods.","severity":"gotcha","affected_versions":"All"},{"fix":"For generic factories, the `__model__` attribute should be defined as a class variable within the `PolyFactory` subclass itself, rather than being passed as an argument to `build()` or `create()` methods (which is now deprecated for this purpose).","message":"Defining the target model for generic `PolyFactory` instances.","severity":"gotcha","affected_versions":"All"},{"fix":"For Pydantic models, use `ModelFactory`. For Dataclasses, use `DataclassFactory`. These specific factories offer tailored functionality and better integration than configuring `PolyFactory` with `__model__`.","message":"Using the generic `PolyFactory` when a specific factory type is available.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}