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