Skip to content

treex.Opaque

Source code in treeo/utils.py
class Opaque(tp.Generic[A]):
    def __init__(self, value: A, predicate: tp.Optional[OpaquePredicate]):
        self.value = value
        self.predicate = predicate

    def __repr__(self):
        return f"Hidden({self.value})"

    def __eq__(self, other):
        if self.predicate is not None:
            return self.predicate(self, other)
        else:
            # if both are Opaque and their values are of the same type
            if isinstance(other, Opaque) and type(self.value) == type(other.value):
                # if they are array-like also compare their shapes and dtypes
                if isinstance(self.value, ArrayLike):
                    other_value = tp.cast(ArrayLike, other.value)
                    return (
                        self.value.shape == other_value.shape
                        and self.value.dtype == other_value.dtype
                    )
                else:
                    # else they are equal
                    return True
            else:
                return False