Skip to content

Abstract Factory API

sweet_tea.abstract_factory.AbstractFactory

Bases: TypeParameterizedFactory, Generic[T], Factory

A generic factory that constrains instantiation to subclasses of type T.

Usage

factory = AbstractFactory[MyBaseClass] instance = factory.create('my_key')

Only classes inheriting from MyBaseClass will be available

Each parameterization is an independent subclass, so several may be held and used in any order without interfering with one another.

Source code in sweet_tea/abstract_factory.py
class AbstractFactory(TypeParameterizedFactory, Generic[T], Factory):
    """
    A generic factory that constrains instantiation to subclasses of type T.

    Usage:
        factory = AbstractFactory[MyBaseClass]
        instance = factory.create('my_key')
        # Only classes inheriting from MyBaseClass will be available

    Each parameterization is an independent subclass, so several may be held and
    used in any order without interfering with one another.
    """

    @classmethod
    def create(
        cls,
        key: str,
        library: str = "",
        label: str = "",
        configuration: dict[str, Any] | BaseModel | None = None,
    ) -> T:
        """
        Create an instance of a registered class that is a subclass of the generic type.

        Args:
            key: Name to reference the class from the registry.
            library: Optional library filter for the class.
            label: Optional label filter for the class.
            configuration: Configuration parameters as keyword arguments, given as a
                dict or a pydantic model whose fields are spread into the constructor.

        Returns:
            Configured instance of the requested class.

        Raises:
            SweetTeaError: When the key is not found or filters don't match.
        """
        # Resolve before filtering by type: a lazily registered entry has no class
        # object, so it cannot answer issubclass until its module is imported. Doing
        # this first means the type filter sees real classes while still importing
        # only the modules that hold a candidate for this key (see SWE-10).
        cls._resolve_for(key)
        typed_entries = cls._registry.typed_entries(lookup_type=cls._get_generic_type())

        return cls._create_from_entries(
            cls._find_entries(key, candidates=typed_entries),
            key,
            library,
            label,
            configuration,
        )

create(key, library='', label='', configuration=None) classmethod

Create an instance of a registered class that is a subclass of the generic type.

Parameters:

Name Type Description Default
key str

Name to reference the class from the registry.

required
library str

Optional library filter for the class.

''
label str

Optional label filter for the class.

''
configuration dict[str, Any] | BaseModel | None

Configuration parameters as keyword arguments, given as a dict or a pydantic model whose fields are spread into the constructor.

None

Returns:

Type Description
T

Configured instance of the requested class.

Raises:

Type Description
SweetTeaError

When the key is not found or filters don't match.

Source code in sweet_tea/abstract_factory.py
@classmethod
def create(
    cls,
    key: str,
    library: str = "",
    label: str = "",
    configuration: dict[str, Any] | BaseModel | None = None,
) -> T:
    """
    Create an instance of a registered class that is a subclass of the generic type.

    Args:
        key: Name to reference the class from the registry.
        library: Optional library filter for the class.
        label: Optional label filter for the class.
        configuration: Configuration parameters as keyword arguments, given as a
            dict or a pydantic model whose fields are spread into the constructor.

    Returns:
        Configured instance of the requested class.

    Raises:
        SweetTeaError: When the key is not found or filters don't match.
    """
    # Resolve before filtering by type: a lazily registered entry has no class
    # object, so it cannot answer issubclass until its module is imported. Doing
    # this first means the type filter sees real classes while still importing
    # only the modules that hold a candidate for this key (see SWE-10).
    cls._resolve_for(key)
    typed_entries = cls._registry.typed_entries(lookup_type=cls._get_generic_type())

    return cls._create_from_entries(
        cls._find_entries(key, candidates=typed_entries),
        key,
        library,
        label,
        configuration,
    )

Methods

create()

sweet_tea.abstract_factory.AbstractFactory.create(key, library='', label='', configuration=None) classmethod

Create an instance of a registered class that is a subclass of the generic type.

Parameters:

Name Type Description Default
key str

Name to reference the class from the registry.

required
library str

Optional library filter for the class.

''
label str

Optional label filter for the class.

''
configuration dict[str, Any] | BaseModel | None

Configuration parameters as keyword arguments, given as a dict or a pydantic model whose fields are spread into the constructor.

None

Returns:

Type Description
T

Configured instance of the requested class.

Raises:

Type Description
SweetTeaError

When the key is not found or filters don't match.

Source code in sweet_tea/abstract_factory.py
@classmethod
def create(
    cls,
    key: str,
    library: str = "",
    label: str = "",
    configuration: dict[str, Any] | BaseModel | None = None,
) -> T:
    """
    Create an instance of a registered class that is a subclass of the generic type.

    Args:
        key: Name to reference the class from the registry.
        library: Optional library filter for the class.
        label: Optional label filter for the class.
        configuration: Configuration parameters as keyword arguments, given as a
            dict or a pydantic model whose fields are spread into the constructor.

    Returns:
        Configured instance of the requested class.

    Raises:
        SweetTeaError: When the key is not found or filters don't match.
    """
    # Resolve before filtering by type: a lazily registered entry has no class
    # object, so it cannot answer issubclass until its module is imported. Doing
    # this first means the type filter sees real classes while still importing
    # only the modules that hold a candidate for this key (see SWE-10).
    cls._resolve_for(key)
    typed_entries = cls._registry.typed_entries(lookup_type=cls._get_generic_type())

    return cls._create_from_entries(
        cls._find_entries(key, candidates=typed_entries),
        key,
        library,
        label,
        configuration,
    )