# pydantic_factory

class polyfactory.factories.pydantic_factory.PydanticBuildContext

  Bases: `BuildContext`

class polyfactory.factories.pydantic_factory.PydanticConstraints

  Bases: `Constraints`

  Metadata regarding a Pydantic type constraints, if any

class polyfactory.factories.pydantic_factory.PydanticFieldMeta

  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) → None

    Create a factory field metadata instance.

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

    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 = <random.Random object>) → PydanticFieldMeta

    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]

    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

  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.

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

    Determine whether the given value is supported by the factory.

    Parameters:

      value – An arbitrary value.

    Returns:

      A typeguard

  classmethod get_model_fields() → list[FieldMeta]

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

    Returns:

      A list of field MetaData instances.

  classmethod build(factory_use_construct: bool = False, **kwargs: Any) → T

    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]

    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

    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

    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]]

    Map types to callables.

    Notes:

      - This method is distinct to allow overriding.

    Returns:

      a dictionary mapping types to callables.