Skip to content

Commit a950045

Browse files
hugovklohitredddy
andauthored
Correct clamp annotations for callable formatters and None (#516)
Co-authored-by: Chandra Tekulapally <chandralohit1234@gmail.com>
1 parent 4b806c4 commit a950045

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

‎src/humanize/number.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
TYPE_CHECKING = False
1717
if TYPE_CHECKING:
18+
from collections.abc import Callable
1819
from typing import TypeAlias
1920

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

459460

460461
def clamp(
461-
value: float,
462-
format: str = "{:}",
462+
value: float | None,
463+
format: str | Callable[[float], str] = "{:}",
463464
floor: float | None = None,
464465
ceil: float | None = None,
465466
floor_token: str = "<",
466467
ceil_token: str = ">",
467-
) -> str:
468+
) -> str | None:
468469
"""Returns number with the specified format, clamped between floor and ceil.
469470
470471
If the number is larger than ceil or smaller than floor, then the respective limit
@@ -488,7 +489,7 @@ def clamp(
488489
```
489490
490491
Args:
491-
value (int, float): Input number.
492+
value (int, float, None): Input number, or `None` to return `None`.
492493
format (str OR callable): Can either be a formatting string, or a callable
493494
function that receives value and returns a string.
494495
floor (int, float): Smallest value before clamping.
@@ -499,7 +500,8 @@ def clamp(
499500
to output.
500501
501502
Returns:
502-
str: Formatted number. The output is clamped between the indicated floor and
503+
str | None: Formatted number, or `None` when `value` is `None`.
504+
The output is clamped between the indicated floor and
503505
ceil. If the number is larger than ceil or smaller than floor, the output
504506
will be prepended with a token indicating as such.
505507

0 commit comments

Comments
 (0)