Skip to content

Non Hashable Value Error

non_hashable_value_error

Error raised when a field value cannot be converted to a hashable equivalent.

Raised when a value is neither natively hashable nor one of the
supported convertible types (list, dict).

NonHashableValueError

Bases: ValueErrorMixin, Error[str]

Raised when a value cannot be made hashable during __hash__ computation.

Automatic hashability conversion supports listtuple and
dictfrozenset of (key, value) pairs. Values of other
mutable types (e.g. bytearray, custom objects without __hash__)
trigger this error.

Example
error = NonHashableValueError(type_name="MyMutableClass", field_name="config")
raise error
Source code in src/forging_blocks/foundation/errors/non_hashable_value_error.py
class NonHashableValueError(ValueErrorMixin, Error[str]):
    """Raised when a value cannot be made hashable during ``__hash__`` computation.

    Automatic hashability conversion supports ``list`` → ``tuple`` and
    ``dict`` → ``frozenset`` of ``(key, value)`` pairs. Values of other
    mutable types (e.g. ``bytearray``, custom objects without ``__hash__``)
    trigger this error.

    Example:
        ```python
        error = NonHashableValueError(type_name="MyMutableClass", field_name="config")
        raise error
        ```
    """

    def __init__(self, type_name: str, field_name: str | None = None) -> None:
        """Initialise the error with the name of the type that failed conversion.

        Args:
            type_name: The ``type(value).__name__`` of the offending value.
            field_name: Optional field name where the value was encountered.

        """
        message = ErrorMessage(
            f"Cannot convert {type_name!r} to hashable. "
            f"Use tuple, frozenset, or immutable types "
            f"in fields hashed by @auto_hash."
            + (f" Field: {field_name!r}." if field_name is not None else "")
        )
        metadata = ErrorMetadata({"type_name": type_name})
        if field_name is not None:
            metadata.context["field_name"] = field_name
        super().__init__(message, metadata)

__init__(type_name: str, field_name: str | None = None) -> None

Initialise the error with the name of the type that failed conversion.

Parameters:

Name Type Description Default
type_name str

The type(value).__name__ of the offending value.

required
field_name str | None

Optional field name where the value was encountered.

None
Source code in src/forging_blocks/foundation/errors/non_hashable_value_error.py
def __init__(self, type_name: str, field_name: str | None = None) -> None:
    """Initialise the error with the name of the type that failed conversion.

    Args:
        type_name: The ``type(value).__name__`` of the offending value.
        field_name: Optional field name where the value was encountered.

    """
    message = ErrorMessage(
        f"Cannot convert {type_name!r} to hashable. "
        f"Use tuple, frozenset, or immutable types "
        f"in fields hashed by @auto_hash."
        + (f" Field: {field_name!r}." if field_name is not None else "")
    )
    metadata = ErrorMetadata({"type_name": type_name})
    if field_name is not None:
        metadata.context["field_name"] = field_name
    super().__init__(message, metadata)