Mock data generation factories for Python types with support for dataclasses, Pydantic, SQLAlchemy, and more
—
Core factory methods that provide the essential functionality for building instances, generating batches, and managing the data generation lifecycle. These methods are available on all factory classes and form the foundation of polyfactory's API.
Build single instances of the target model with optional field overrides and configuration.
@classmethod
def build(**kwargs) -> T:
"""
Build a single instance of the factory's model.
Parameters:
- **kwargs: Field values to override in the generated instance
Returns:
Instance of the model type with generated or overridden values
"""Usage Example:
from dataclasses import dataclass
from polyfactory.factories import DataclassFactory
@dataclass
class User:
name: str
age: int
email: str
class UserFactory(DataclassFactory[User]):
__model__ = User
# Build with all generated values
user = UserFactory.build()
# Build with specific overrides
user = UserFactory.build(name="Alice", age=30)Generate multiple instances efficiently with optional size specification and field overrides applied to all instances.
@classmethod
def batch(size: int, **kwargs) -> list[T]:
"""
Build multiple instances of the factory's model.
Parameters:
- size: Number of instances to generate
- **kwargs: Field values to override in all generated instances
Returns:
List of model instances
"""Usage Example:
# Generate 10 users
users = UserFactory.batch(10)
# Generate 5 users all with age=25
users = UserFactory.batch(5, age=25)Generate instances that ensure full type coverage, creating variations that exercise all possible code paths and type combinations.
@classmethod
def coverage(cls, **kwargs) -> Iterator[T]:
"""
Build instances that provide full coverage of the model's type variations.
Parameters:
- **kwargs: Field values to override in generated instances
Returns:
Iterator of model instances covering all type variations
"""Build and persist instances using synchronous persistence handlers, integrating directly with databases and storage systems.
@classmethod
def create_sync(**kwargs) -> T:
"""
Build and persist a single instance synchronously.
Parameters:
- **kwargs: Field values to override in the generated instance
Returns:
Persisted instance of the model type
Raises:
ConfigurationException: If no sync persistence handler is configured
"""
@classmethod
def create_batch_sync(size: int, **kwargs) -> list[T]:
"""
Build and persist multiple instances synchronously.
Parameters:
- size: Number of instances to generate and persist
- **kwargs: Field values to override in all generated instances
Returns:
List of persisted model instances
"""Build and persist instances using asynchronous persistence handlers for non-blocking database operations.
@classmethod
async def create_async(**kwargs) -> T:
"""
Build and persist a single instance asynchronously.
Parameters:
- **kwargs: Field values to override in the generated instance
Returns:
Persisted instance of the model type
Raises:
ConfigurationException: If no async persistence handler is configured
"""
@classmethod
async def create_batch_async(size: int, **kwargs) -> list[T]:
"""
Build and persist multiple instances asynchronously.
Parameters:
- size: Number of instances to generate and persist
- **kwargs: Field values to override in all generated instances
Returns:
List of persisted model instances
"""Dynamically create factory classes for types without explicitly defining factory subclasses.
@classmethod
def create_factory(model: type, **kwargs) -> type[BaseFactory]:
"""
Generate a factory class for a given model type.
Parameters:
- model: The model type to create a factory for
- **kwargs: Configuration attributes for the factory class
Returns:
Factory class for the specified model type
"""Usage Example:
from typing import TypedDict
class PersonDict(TypedDict):
name: str
age: int
# Create factory dynamically
PersonFactory = BaseFactory.create_factory(PersonDict)
person = PersonFactory.build()Verify whether a given type is supported by polyfactory's generation system.
@classmethod
def is_supported_type(value: Any) -> bool:
"""
Check if a type is supported for automatic generation.
Parameters:
- value: Type or value to check for support
Returns:
True if the type can be automatically generated
"""Install with Tessl CLI
npx tessl i tessl/pypi-polyfactory