fields

class polyfactory.fields.WrappedCallable[source]

Bases: TypedDict

A ref storing a callable. This class is a utility meant to prevent binding of methods.

class polyfactory.fields.Require[source]

Bases: object

A factory field that marks an attribute as a required build-time kwarg.

class polyfactory.fields.Ignore[source]

Bases: object

A factory field that marks an attribute as ignored.

class polyfactory.fields.Use[source]

Bases: Generic[P, T]

Factory field used to wrap a callable.

The callable will be invoked whenever building the given factory attribute.

__init__(fn: ~typing.Callable[[~P], ~polyfactory.fields.T], *args: ~typing.~P, **kwargs: ~typing.~P) None[source]

Wrap a callable.

Parameters:
  • fn – A callable to wrap.

  • args – Any args to pass to the callable.

  • kwargs – Any kwargs to pass to the callable.

to_value() T[source]

Invoke the callable.

Returns:

The output of the callable.

class polyfactory.fields.PostGenerated[source]

Bases: object

Factory field that allows generating values after other fields are generated by the factory.

__init__(fn: Callable, *args: Any, **kwargs: Any) None[source]

Designate field as post-generated.

Parameters:
  • fn – A callable.

  • args – Args for the callable.

  • kwargs – Kwargs for the callable.

to_value(name: str, values: dict[str, Any]) Any[source]

Invoke the post-generation callback passing to it the build results.

Parameters:
  • name – Field name.

  • values – Generated values.

Returns:

An arbitrary value.

class polyfactory.fields.Fixture[source]

Bases: object

Factory field to create a pytest fixture from a factory.

__init__(fixture: Callable, size: int | None = None, **kwargs: Any) None[source]

Create a fixture from a factory.

Parameters:
  • fixture – A factory that was registered as a fixture.

  • size – Optional batch size.

  • kwargs – Any build kwargs.

to_value() Any[source]

Call the factory’s build or batch method.

Raises:

ParameterException

Returns:

The build result.

class polyfactory.fields.Param[source]

Bases: Generic[T]

A constant parameter that can be used by other fields but will not be passed to the final object.

If a value for the parameter is not passed in the field’s definition, it must be passed at build time. Otherwise, a MissingParamException will be raised.

__init__(param: ~polyfactory.fields.T | ~typing.Callable[[...], ~polyfactory.fields.T] | type[polyfactory.field_meta.Null] = <class 'polyfactory.field_meta.Null'>, is_callable: bool = False, **kwargs: ~typing.Any) None[source]

Designate a parameter.

Parameters:

param – A constant or an unpassed value that can be referenced later

to_value(from_build: ~polyfactory.fields.T | ~typing.Callable[[...], ~polyfactory.fields.T] | type[polyfactory.field_meta.Null] = <class 'polyfactory.field_meta.Null'>, **kwargs: ~typing.Any) T[source]

Determines the value to use at build time

If a value was passed to the constructor, it will be used. Otherwise, the value passed at build time will be used. If no value was passed at build time, a MissingParamException will be raised.

Parameters:

args – from_build: The value passed at build time (if any).

Returns:

The value

Raises:

MissingParamException