← Files AgentBrokoARCHIVED FILE

.venv/Lib/site-packages/eth_utils/numeric.py

1.16 KB · Sep 30, 2026 · 23:15 UTC

↓ Download file

from abc import (
    ABC,
    abstractmethod,
)
import decimal
import numbers
from typing import (
    Any,
    TypeVar,
    Union,
)


class Comparable(ABC):
    @abstractmethod
    def __lt__(self, other: Any) -> bool:
        ...

    @abstractmethod
    def __gt__(self, other: Any) -> bool:
        ...


TComparable = Union[Comparable, numbers.Real, int, float, decimal.Decimal]


TValue = TypeVar("TValue", bound=TComparable)


def clamp(lower_bound: TValue, upper_bound: TValue, value: TValue) -> TValue:
    # The `mypy` ignore statements here are due to doing a comparison of
    # `Union` types which isn't allowed. (per cburgdorf). This approach was
    # chosen over using `typing.overload` to define multiple signatures for
    # each comparison type here since the added value of "proper" typing
    # doesn't seem to justify the complexity of having a bunch of different
    # signatures defined. The external library perspective on this function
    # should still be adequate under this approach
    if value < lower_bound:  # type: ignore
        return lower_bound
    elif value > upper_bound:  # type: ignore
        return upper_bound
    else:
        return value

SHA-256: 46b5dd5c8f9b861904b33dda06dc47b8695cff6649bd41a9bcc738ef0c78d3c6