Skip to content
Closed

AI spam #3548

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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Unreleased
Colorama is no longer a dependency and is not used. :issue:`2986` :pr:`3505`
- :class:`Argument` accepts a ``help`` parameter, and help output includes
a ``Positional arguments`` section when argument help is available. :issue:`2983` :pr:`3473`
- :class:`FloatRange` with ``clamp`` enabled always returns a ``float``,
even when the boundary was given as an ``int``. :issue:`3547` :pr:`3548`


Version 8.4.2
Expand Down
9 changes: 8 additions & 1 deletion src/click/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,10 @@ class FloatRange(_NumberRangeBase[float], FloatParamType):
boundary instead of failing. This is not supported if either
boundary is marked ``open``.

.. versionchanged:: 8.5
A clamped value is always a ``float``, even when the boundary was
given as an ``int``.

.. versionchanged:: 8.0
Added the ``min_open`` and ``max_open`` parameters.
"""
Expand All @@ -693,7 +697,10 @@ def __init__(

def _clamp(self, bound: float, dir: t.Literal[1, -1], open: bool) -> float:
if not open:
return bound
# The boundary may have been passed as an ``int`` (e.g.
# ``FloatRange(0, 1)``); coerce it so clamping always returns
# a ``float``.
return self._number_class(bound)

# Could use math.nextafter here, but clamping an
# open float range doesn't seem to be particularly useful. It's
Expand Down
8 changes: 8 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ def test_float_range_no_clamp_open():
sneaky.convert("1.5", None, None)


@pytest.mark.parametrize("value", ["2.5", "-2.5"])
def test_float_range_clamp_returns_float(value):
"""Clamping always returns a ``float``, even when the boundary was
given as an ``int``. https://github.com/pallets/click/issues/3547"""
result = click.FloatRange(0, 1, clamp=True).convert(value, None, None)
assert type(result) is float


@pytest.mark.parametrize(
("nargs", "multiple", "default", "expect"),
[
Expand Down
Loading