Skip to content

identity

identity #

A recipe function's cache identity: an explicit version ("name/1") or a body digest.

function_identity(func, identity) #

The cache identity for func.

Parameters:

Name Type Description Default
func Callable[..., object]

The decorated function.

required
identity str | None

An explicit versioned identity ("name/1"), or None for the automatic source-digest form.

required

Returns:

Type Description
str

The identity string keyed into every cache entry for this function.

Raises:

Type Description
ValueError

If an explicit identity is not of the "name/N" shape.

Source code in capturegraph-lib/capturegraph/recipes/calls/identity.py
def function_identity(func: Callable[..., object], identity: str | None) -> str:
    """The cache identity for ``func``.

    Args:
        func: The decorated function.
        identity: An explicit versioned identity (``"name/1"``), or ``None``
            for the automatic source-digest form.

    Returns:
        The identity string keyed into every cache entry for this function.

    Raises:
        ValueError: If an explicit ``identity`` is not of the ``"name/N"`` shape.
    """
    if identity is not None:
        if not _VERSIONED.fullmatch(identity):
            raise ValueError(
                f"pure_function identity {identity!r} must look like 'name/1' "
                f"(a name, a slash, a version number)"
            )
        return identity
    body = inspect.unwrap(func)
    digest = hash_bytes(_code_bytes(body.__code__)).hex
    return f"{body.__module__}.{body.__qualname__}@{digest}"