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.