Skip to content
Closed
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions src/click/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,22 @@ def __init__(
if (min_open or max_open) and clamp:
raise TypeError("Clamping is not supported for open bounds.")

def convert(
self, value: t.Any, param: Parameter | None, ctx: Context | None
) -> t.Any:
import math

rv = super().convert(value, param, ctx)

if math.isnan(rv):
self.fail(
_("{value} is not a valid number.").format(value=rv),
param,
ctx,
)

return rv

def _clamp(self, bound: float, dir: t.Literal[1, -1], open: bool) -> float:
if not open:
return bound
Expand Down
18 changes: 18 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ def test_range_fail(type, value, expect):
assert expect in exc_info.value.message


@pytest.mark.parametrize(
("type",),
[
(click.FloatRange(0.0, 1.0),),
(click.FloatRange(0.5, 1.5),),
(click.FloatRange(max=1.5),),
(click.FloatRange(),),
(click.FloatRange(0.0, 1.0, clamp=True),),
(click.FloatRange(0.0, 1.0, min_open=True),),
],
)
def test_float_range_rejects_nan(type):
"""FloatRange should reject NaN values since NaN is not a valid number
and NaN comparisons are always False, bypassing range checks."""
with pytest.raises(click.BadParameter, match="not a valid number"):
type.convert("nan", None, None)


def test_float_range_no_clamp_open():
with pytest.raises(TypeError):
click.FloatRange(0, 1, max_open=True, clamp=True)
Expand Down