Skip to content

Abstract Factory API

sweet_tea.abstract_factory.AbstractFactory

Bases: 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

Source code in sweet_tea/abstract_factory.py
class AbstractFactory(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
    """

    _type = T  # type: ignore[misc]

    @classmethod
    def __class_getitem__(cls, item: Type[T]) -> Type["AbstractFactory[T]"]:
        """Create a parameterized generic subclass with the specified type."""
        result = super().__class_getitem__(item)  # type: ignore[attr-defined,misc]
        result._type = item  # type: ignore[misc]
        return result

    @classmethod
    def _get_generic_type(cls) -> Type[T]:
        """Get the generic type parameter."""
        return cls._type

    @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)

__class_getitem__(item) classmethod

Create a parameterized generic subclass with the specified type.

Source code in sweet_tea/abstract_factory.py
@classmethod
def __class_getitem__(cls, item: Type[T]) -> Type["AbstractFactory[T]"]:
    """Create a parameterized generic subclass with the specified type."""
    result = super().__class_getitem__(item)  # type: ignore[attr-defined,misc]
    result._type = item  # type: ignore[misc]
    return result

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)