Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Callable
from typing import TypeAlias

# This type can be better defined by typing.SupportsFloat
Expand Down Expand Up @@ -458,13 +459,13 @@ def scientific(value: NumberOrString, precision: int = 2) -> str:


def clamp(
value: float,
format: str = "{:}",
value: float | None,
format: str | Callable[[float], str] = "{:}",
floor: float | None = None,
ceil: float | None = None,
floor_token: str = "<",
ceil_token: str = ">",
) -> str:
) -> str | None:
"""Returns number with the specified format, clamped between floor and ceil.
If the number is larger than ceil or smaller than floor, then the respective limit
Expand All @@ -488,7 +489,7 @@ def clamp(
```
Args:
value (int, float): Input number.
value (int, float, None): Input number, or `None` to return `None`.
format (str OR callable): Can either be a formatting string, or a callable
function that receives value and returns a string.
floor (int, float): Smallest value before clamping.
Expand All @@ -499,7 +500,8 @@ def clamp(
to output.
Returns:
str: Formatted number. The output is clamped between the indicated floor and
str | None: Formatted number, or `None` when `value` is `None`.
The output is clamped between the indicated floor and
ceil. If the number is larger than ceil or smaller than floor, the output
will be prepended with a token indicating as such.
Expand Down
Loading