Skip to content

Types

Stores low-level types used through the library.

SortedSequence: TypeAlias
module-attribute
#

A sequence that is sorted. Only useful for typing

SortedIterable: TypeAlias
module-attribute
#

An iterable that is sorted. Only useful for typing

Item
module-attribute
#

The type associated with components, splits and choices

Config: TypeAlias
module-attribute
#

An object representing a configuration of a pipeline.

Space
module-attribute
#

Generic for objects that are aware of a space but not the specific kind

Seed: TypeAlias
module-attribute
#

Type alias for kinds of Seeded objects.

FidT: TypeAlias
module-attribute
#

Type alias for a fidelity bound.

class Comparable #

Bases: Protocol

Protocol for annotating comparable types.

class Requeue(generator) #

Bases: Iterator[T]

A queue that can have items requeued.

Requeue
import random
from amltk.types import Requeue

name_generator = iter(["Alice", "Bob", "Charlie"])
queue: Requeue[str] = Requeue(name_generator)

rng = random.Random(1)

def process_name(name: str) -> bool:
    return rng.choice([True, False])

for name in queue:
    print(f"Processing {name}")
    processed = process_name(name)
    if not processed:
        print(f"Failed to process {name}, requeuing")
        queue.requeue(name)
Processing Alice
Processing Bob
Processing Charlie
Failed to process Charlie, requeuing
Processing Charlie
See Also
Source code in src/amltk/types.py
def __init__(self, generator: Iterable[T]) -> None:
    """Create a requeue from an iterable."""
    super().__init__()
    self.generator = iter(generator)

def append(item) #

Append an item to the queue.

Source code in src/amltk/types.py
def append(self, item: T) -> None:
    """Append an item to the queue."""
    self.generator = chain(self.generator, [item])

def requeue(item) #

Requeue an item.

Source code in src/amltk/types.py
def requeue(self, item: T) -> None:
    """Requeue an item."""
    self.generator = chain([item], self.generator)

def from_func(func, n=None)
classmethod
#

Create a Requeue from a function.

Source code in src/amltk/types.py
@classmethod
def from_func(cls, func: Callable[[], T], n: int | None = None) -> Requeue[T]:
    """Create a Requeue from a function."""
    repeater = repeat(None) if n is None else repeat(None, times=n)
    return cls(func() for _ in repeater)

def assert_never(value) #

Utility function for asserting that a value is never reached.

Source code in src/amltk/types.py
def assert_never(value: NoReturn) -> NoReturn:
    """Utility function for asserting that a value is never reached."""
    # This also works in runtime as well:
    raise AssertionError(f"This code should never be reached, got: {value}")

def safe_issubclass(cls, classes) #

Check if a class is a subclass of a given type.

This is a safe version of issubclass that relies on strings, which is useful for when the type is not importable.

PARAMETER DESCRIPTION
cls

The class to check

TYPE: type

classes

The type to check for.

TYPE: str | tuple[str, ...]

RETURNS DESCRIPTION
bool

bool

Source code in src/amltk/types.py
def safe_issubclass(cls: type, classes: str | tuple[str, ...]) -> bool:
    """Check if a class is a subclass of a given type.

    This is a safe version of issubclass that relies on strings,
    which is useful for when the type is not importable.

    Args:
        cls: The class to check
        classes: The type to check for.

    Returns:
        bool
    """

    def type_names(o: type) -> Iterator[str]:
        yield o.__qualname__
        for parent in o.__bases__:
            yield from type_names(parent)

    allowable_names = {classes} if isinstance(classes, str) else set(classes)
    return any(name in allowable_names for name in type_names(cls))

def safe_isinstance(obj, t) #

Check if an object is of a given type.

This is a safe version of isinstance that relies on strings, which is useful for when the type is not importable.

PARAMETER DESCRIPTION
obj

The object to check

TYPE: Any

t

The type to check for.

TYPE: str | tuple[str, ...]

RETURNS DESCRIPTION
bool

bool

Source code in src/amltk/types.py
def safe_isinstance(obj: Any, t: str | tuple[str, ...]) -> bool:
    """Check if an object is of a given type.

    This is a safe version of isinstance that relies on strings,
    which is useful for when the type is not importable.

    Args:
        obj: The object to check
        t: The type to check for.

    Returns:
        bool
    """
    return safe_issubclass(type(obj), t)