fields#

class polyfactory.fields.WrappedCallable#

Bases: TypedDict

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

class polyfactory.fields.Require#

Bases: object

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

class polyfactory.fields.Ignore#

Bases: object

A factory field that marks an attribute as ignored.

class polyfactory.fields.Use#

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#

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#

Invoke the callable.

Returns:

The output of the callable.

class polyfactory.fields.PostGenerated#

Bases: object

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

__init__(fn: Callable, *args: Any, **kwargs: Any) None#

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#

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#

Bases: object

Factory field to create a pytest fixture from a factory.

__init__(fixture: Callable, size: int | None = None, **kwargs: Any) None#

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#

Call the factory’s build or batch method.

Raises:

ParameterException

Returns:

The build result.