Skip to content

content

content #

value_hash — a value's identity is the merkle of the tree it stores as.

value_hash(value, schema=None) #

The merkle content hash of a CGType value; equal values hash equal.

A value on disk and unchanged hashes in place; anything else is stored into a workspace first, so both forms of an equal value hit the same key.

Parameters:

Name Type Description Default
value object

Any CGType instance.

required
schema type[CGType] | None

The type to store an in-memory value as; defaults to its own type.

None

Raises:

Type Description
TypeError

If value is not a CGType instance.

ScratchNotConfigured

If no scratch is configured.

Source code in capturegraph-lib/capturegraph/recipes/values/content.py
def value_hash(value: object, schema: type[CGType] | None = None) -> ContentHash:
    """The merkle content hash of a ``CGType`` value; equal values hash equal.

    A value on disk and unchanged hashes in place; anything else is stored
    into a workspace first, so both forms of an equal value hit the same key.

    Args:
        value: Any ``CGType`` instance.
        schema: The type to store an in-memory value as; defaults to its own type.

    Raises:
        TypeError: If ``value`` is not a ``CGType`` instance.
        ScratchNotConfigured: If no scratch is configured.
    """
    scratch = active_scratch()
    if isinstance(value, _PathScalar):
        return scratch.hash_tree(Path(value))
    if not isinstance(value, CGType):
        raise TypeError(f"value_hash needs a CGType value, got {type(value).__name__}")
    cgtype = schema if schema is not None else type(value)
    base = known_base(value)
    if base is not None and _loads_equal(cgtype, base, value):
        return scratch.hash_tree(base)
    return _hash_stored(scratch, value, cgtype)