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] | 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.

        Returns:
            Configured instance of the requested class.

        Raises:
            SweetTeaError: When the key is not found or filters don't match.
        """
        # Find all entries that have the specified key value and match the generic type
        entries = []
        key_variations = cls._generate_key_variations(key)
        typed_entries = cls._registry.typed_entries(lookup_type=cls._get_generic_type())

        for variation in key_variations:
            entries.extend([entry for entry in typed_entries if entry.key == variation])
            if entries:  # Found entries with this variation
                break

        return cls._create_from_entries(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] | None

Configuration parameters as keyword arguments.

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] | 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.

    Returns:
        Configured instance of the requested class.

    Raises:
        SweetTeaError: When the key is not found or filters don't match.
    """
    # Find all entries that have the specified key value and match the generic type
    entries = []
    key_variations = cls._generate_key_variations(key)
    typed_entries = cls._registry.typed_entries(lookup_type=cls._get_generic_type())

    for variation in key_variations:
        entries.extend([entry for entry in typed_entries if entry.key == variation])
        if entries:  # Found entries with this variation
            break

    return cls._create_from_entries(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] | None

Configuration parameters as keyword arguments.

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] | 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.

    Returns:
        Configured instance of the requested class.

    Raises:
        SweetTeaError: When the key is not found or filters don't match.
    """
    # Find all entries that have the specified key value and match the generic type
    entries = []
    key_variations = cls._generate_key_variations(key)
    typed_entries = cls._registry.typed_entries(lookup_type=cls._get_generic_type())

    for variation in key_variations:
        entries.extend([entry for entry in typed_entries if entry.key == variation])
        if entries:  # Found entries with this variation
            break

    return cls._create_from_entries(entries, key, library, label, configuration)