Skip to content

confingy.utils.hashing#

hashing #

hash_class #

hash_class(cls: type, algorithm: str = 'sha256') -> str

Create a hash of a class based on its bytecode. Only changes that affect code execution will change the hash.

The hash is deterministic across processes and is memoized per class, so mutating a class after it has been hashed will not update its hash.

Parameters:

Name Type Description Default
cls type

The class to hash

required
algorithm str

The hashing algorithm to use (default: 'sha256')

'sha256'

Returns:

Type Description
str

Hexadecimal hash string

Source code in src/confingy/utils/hashing.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def hash_class(cls: type, algorithm: str = "sha256") -> str:
    """
    Create a hash of a class based on its bytecode.
    Only changes that affect code execution will change the hash.

    The hash is deterministic across processes and is memoized per class, so
    mutating a class after it has been hashed will not update its hash.

    Args:
        cls: The class to hash
        algorithm: The hashing algorithm to use (default: 'sha256')

    Returns:
        Hexadecimal hash string
    """
    try:
        per_class = _HASH_CACHE.setdefault(cls, {})
    except TypeError:  # Not weak-referenceable
        return _compute_class_hash(cls, algorithm)
    if algorithm not in per_class:
        per_class[algorithm] = _compute_class_hash(cls, algorithm)
    return per_class[algorithm]