pydantic_factory

class polyfactory.factories.pydantic_factory.PydanticBuildContext[source]

Bases: BuildContext

class polyfactory.factories.pydantic_factory.PydanticConstraints[source]

Bases: Constraints

Metadata regarding a Pydantic type constraints, if any

class polyfactory.factories.pydantic_factory.PydanticFieldMeta[source]

Bases: FieldMeta

Field meta subclass capable of handling pydantic ModelFields

__init__(*, name: str, annotation: type, random: Random | None = None, default: Any = Ellipsis, children: list[FieldMeta] | None = None, constraints: PydanticConstraints | None = None, examples: list[Any] | None = None) None[source]

Create a factory field metadata instance.

classmethod from_field_info(field_name: str, field_info: FieldInfo, use_alias: bool, random: Random | None = None, randomize_collection_length: bool | None = None, min_collection_length: int | None = None, max_collection_length: int | None = None) PydanticFieldMeta[source]

Create an instance from a pydantic field info.

Parameters:
  • field_name – The name of the field.

  • field_info – A pydantic FieldInfo instance.

  • use_alias – Whether to use the field alias.

  • random – A random.Random instance.

  • randomize_collection_length – Whether to randomize collection length.

  • min_collection_length – Minimum collection length.

  • max_collection_length – Maximum collection length.

Returns:

A PydanticFieldMeta instance.

classmethod from_model_field(model_field: ModelField, use_alias: bool, randomize_collection_length: bool | None = None, min_collection_length: int | None = None, max_collection_length: int | None = None, random: Random | None = None) PydanticFieldMeta[source]

Create an instance from a pydantic model field. :param _sphinx_paramlinks_polyfactory.factories.pydantic_factory.PydanticFieldMeta.from_model_field.model_field: A pydantic ModelField. :param _sphinx_paramlinks_polyfactory.factories.pydantic_factory.PydanticFieldMeta.from_model_field.use_alias: Whether to use the field alias. :param _sphinx_paramlinks_polyfactory.factories.pydantic_factory.PydanticFieldMeta.from_model_field.randomize_collection_length: A boolean flag whether to randomize collections lengths :param _sphinx_paramlinks_polyfactory.factories.pydantic_factory.PydanticFieldMeta.from_model_field.min_collection_length: Minimum number of elements in randomized collection :param _sphinx_paramlinks_polyfactory.factories.pydantic_factory.PydanticFieldMeta.from_model_field.max_collection_length: Maximum number of elements in randomized collection :param _sphinx_paramlinks_polyfactory.factories.pydantic_factory.PydanticFieldMeta.from_model_field.random: An instance of random.Random.

Returns:

A PydanticFieldMeta instance.

classmethod get_constraints_metadata(annotation: Any) Sequence[Any][source]

Get the metadatas of the constraints from the given annotation.

Parameters:
  • annotation – A type annotation.

  • random – An instance of random.Random.

Returns:

A list of the metadata in the annotation.

class polyfactory.factories.pydantic_factory.ModelFactory[source]

Bases: Generic[T], BaseFactory[T]

Base factory for pydantic models

__is_base_factory__: bool = True

Flag dictating whether the factory is a ‘base’ factory. Base factories are registered globally as handlers for types. For example, the ‘DataclassFactory’, ‘TypedDictFactory’ and ‘ModelFactory’ are all base factories.

__use_examples__: ClassVar[bool] = False

Flag indicating whether to use a random example, if provided (Pydantic >=V2)

Example code:

class Payment(BaseModel):
    amount: int = Field(0)
    currency: str = Field(examples=['USD', 'EUR', 'INR'])

class PaymentFactory(ModelFactory[Payment]):
    __use_examples__ = True
>>> payment = PaymentFactory.build()
>>> payment
Payment(amount=120, currency="EUR")
__config_keys__: tuple[str, ...] = ('__check_model__', '__allow_none_optionals__', '__set_as_default_factory_for_type__', '__faker__', '__random__', '__randomize_collection_length__', '__min_collection_length__', '__max_collection_length__', '__use_defaults__', '__use_examples__')

Keys to be considered as config values to pass on to dynamically created factories.

classmethod is_supported_type(value: Any) TypeGuard[type[T]][source]

Determine whether the given value is supported by the factory.

Parameters:

value – An arbitrary value.

Returns:

A typeguard

classmethod get_model_fields() list[polyfactory.field_meta.FieldMeta][source]

Retrieve a list of fields from the factory’s model.

Returns:

A list of field MetaData instances.

classmethod get_field_value(field_meta: FieldMeta, field_build_parameters: Any | None = None, build_context: BuildContext | None = None) Any[source]

Return a value from examples if exists, else random value.

Parameters:
  • field_meta – FieldMeta instance.

  • field_build_parameters – Any build parameters passed to the factory as kwarg values.

  • build_context – BuildContext data for current build.

Returns:

An arbitrary value.

classmethod build(factory_use_construct: bool = False, **kwargs: Any) T[source]

Build an instance of the factory’s __model__

Parameters:
  • factory_use_construct – A boolean that determines whether validations will be made when instantiating the model. This is supported only for pydantic models.

  • kwargs – Any kwargs. If field_meta names are set in kwargs, their values will be used.

Returns:

An instance of type T.

classmethod coverage(factory_use_construct: bool = False, **kwargs: Any) abc.Iterator[T][source]

Build a batch of the factory’s Meta.model will full coverage of the sub-types of the model.

Parameters:

kwargs – Any kwargs. If field_meta names are set in kwargs, their values will be used.

Returns:

A iterator of instances of type T.

classmethod is_custom_root_field(field_meta: FieldMeta) bool[source]

Determine whether the field is a custom root field.

Parameters:

field_meta – FieldMeta instance.

Returns:

A boolean determining whether the field is a custom root.

classmethod should_set_field_value(field_meta: FieldMeta, **kwargs: Any) bool[source]

Determine whether to set a value for a given field_name. This is an override of BaseFactory.should_set_field_value.

Parameters:
  • field_meta – FieldMeta instance.

  • kwargs – Any kwargs passed to the factory.

Returns:

A boolean determining whether a value should be set for the given field_meta.

classmethod get_provider_map() dict[Any, Callable[[], Any]][source]

Map types to callables which accept no arguments and return a random value of the given type.

Notes:
  • This method is distinct to allow overriding.

Returns:

a dictionary mapping types to callables.