Skip to content

liblaf.peach.utils

Runtime helpers used by Peach protocols.

Functions:

  • is_implemented

    Return whether obj provides a concrete method implementation.

  • not_implemented

    Mark a protocol stub as intentionally unimplemented.

is_implemented

is_implemented(obj: Any, method: str | Callable) -> bool

Return whether obj provides a concrete method implementation.

Parameters:

  • obj (Any) –

    Object to inspect.

  • method (str | Callable) –

    Method name or callable whose __name__ is used.

Returns:

Examples:

>>> from liblaf.peach.utils import is_implemented, not_implemented
>>> class Hooks:
...     @not_implemented
...     def callback(self): ...
>>> is_implemented(Hooks(), "callback")
False
>>> class Concrete:
...     def callback(self): ...
>>> is_implemented(Concrete(), "callback")
True
Source code in src/liblaf/peach/utils/_implemented.py
def is_implemented(obj: Any, method: str | Callable) -> bool:
    """Return whether `obj` provides a concrete method implementation.

    Args:
        obj: Object to inspect.
        method: Method name or callable whose `__name__` is used.

    Returns:
        `False` for missing attributes, `None`, or methods decorated with
        [`not_implemented`][liblaf.peach.utils.not_implemented]; otherwise
        `True`.

    Examples:
        >>> from liblaf.peach.utils import is_implemented, not_implemented
        >>> class Hooks:
        ...     @not_implemented
        ...     def callback(self): ...
        >>> is_implemented(Hooks(), "callback")
        False
        >>> class Concrete:
        ...     def callback(self): ...
        >>> is_implemented(Concrete(), "callback")
        True
    """
    if not isinstance(method, str):
        method: str = method.__name__  # ty:ignore[unresolved-attribute]
    try:
        method: Any = getattr(obj, method)
    except AttributeError:
        return False
    return not (method is None or getattr(method, "__not_implemented__", False))

not_implemented

not_implemented[F](func: F) -> F

Mark a protocol stub as intentionally unimplemented.

The marker lets is_implemented distinguish inherited protocol placeholders from concrete hook implementations.

Source code in src/liblaf/peach/utils/_implemented.py
def not_implemented[F](func: F) -> F:
    """Mark a protocol stub as intentionally unimplemented.

    The marker lets
    [`is_implemented`][liblaf.peach.utils.is_implemented] distinguish inherited
    protocol placeholders from concrete hook implementations.
    """
    func.__not_implemented__ = True  # ty:ignore[unresolved-attribute]
    return func