betty.factory module - Betty documentation

Hide navigation sidebar
Hide table of contents sidebar
Skip to content
Back to top
View this page
Toggle table of contents sidebar

# betty.factory module

Functionality for creating new class instances.

class betty.factory.Factory

  Bases: `Protocol`

  A callable to create a new instance.

  __init__(*args, **kwargs)

exception betty.factory.FactoryError

  Bases: `RuntimeError`

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

  classmethod new(new_cls: type) → Self

    Create a new instance.

class betty.factory.IndependentFactory

  Bases: `ABC`

  Provide a factory for classes that can instantiate themselves asynchronously.

  abstract async classmethod new() → Self

    Create a new instance.

exception betty.factory.InitFactoryError

  Bases: `FactoryError`

  Raised when a class could not be instantiated by calling it directly.

  classmethod new(new_cls: type) → Self

    Create a new instance.

class betty.factory.TargetFactory

  Bases: `ABC`

  Provide a factory for classes that depend on `self`.

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

    Create a new instance.

    Raises:

      FactoryError – raised when `cls` could not be instantiated.

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

  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.