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) None [source]¶
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 [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 = <random.Random object>) 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.
- 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.
- 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 build(factory_use_construct: bool = False, **kwargs: Any) T [source]¶
Build an instance of the factory’s __model__
- 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.