numeric.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from abc import (
  2. ABC,
  3. abstractmethod,
  4. )
  5. import decimal
  6. import numbers
  7. from typing import (
  8. Any,
  9. TypeVar,
  10. Union,
  11. )
  12. class Comparable(ABC):
  13. @abstractmethod
  14. def __lt__(self, other: Any) -> bool:
  15. ...
  16. @abstractmethod
  17. def __gt__(self, other: Any) -> bool:
  18. ...
  19. TComparable = Union[Comparable, numbers.Real, int, float, decimal.Decimal]
  20. TValue = TypeVar("TValue", bound=TComparable)
  21. def clamp(lower_bound: TValue, upper_bound: TValue, value: TValue) -> TValue:
  22. # The `mypy` ignore statements here are due to doing a comparison of
  23. # `Union` types which isn't allowed. (per cburgdorf). This approach was
  24. # chosen over using `typing.overload` to define multiple signatures for
  25. # each comparison type here since the added value of "proper" typing
  26. # doesn't seem to justify the complexity of having a bunch of different
  27. # signatures defined. The external library perspective on this function
  28. # should still be adequate under this approach
  29. if value < lower_bound: # type: ignore
  30. return lower_bound
  31. elif value > upper_bound: # type: ignore
  32. return upper_bound
  33. else:
  34. return value