betty.factory module

Functionality for creating new class instances.

exception betty.factory.FactoryError[source]

Bases: RuntimeError

Raised when a class could not be instantiated by a factory API.

classmethod new(new_cls: type) Self[source]

Create a new instance for a class that failed to instantiate.

class betty.factory.IndependentFactory[source]

Bases: ABC

Provide a factory for classes that can instantiate themselves asynchronously.

abstract async classmethod new() Self[source]

Create a new instance.

class betty.factory.TargetFactory[source]

Bases: ABC, Generic[_T]

Provide a factory for classes that depend on self.

abstract async new_target(cls: type[_T]) _T[source]

Create a new instance.

Raises:

FactoryError – raised when cls could not be instantiated.

async betty.factory.new(cls: type[_T]) _T[source]

Create a new instance.

Returns:

  1. If cls extends betty.factory.IndependentFactory, this will call return cls’s

    new()’s return value.

  2. Otherwise cls() will be called without arguments, and the resulting instance will be returned.

Raises:

FactoryError – raised when cls could not be instantiated.