Skip to content
Myr

add open intervals to FloatRange

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by robintully · committed by David Lord · parent d15f740 · Browse files · 3 files+28 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+28 added−4 removed

  • tests:+18 added−0 removed
  • other:+10 added−4 removed

3 files+28 added−4 removed

modified:src/click/types.py+9 added-4 removed
1import os2import operator3import stat4from datetime import datetime56from ._compat import _get_argv_encoding
332 unmodified lines
339class FloatRange(FloatParamType):340 """A parameter that works similar to :data:`click.FLOAT` but restricts341 the value to fit into a range. The default behavior is to fail if the342 value falls outside the range, but it can also be silently clamped342 between the two edges.343 between the two edges. The exclusive argument permits open intervals.344345 See :ref:`ranges` for an example.346 """347348 name = "float range"349349 def __init__(self, min=None, max=None, clamp=False):350 def __init__(self, min=None, max=None, clamp=False, exclusive=False):351 self.min = min352 self.max = max353 self.clamp = clamp354 self.exclusive = exclusive355356 def convert(self, value, param, ctx):357 rv = super().convert(value, param, ctx)358
1 unmodified line
360 if self.min is not None and rv < self.min:361 return self.min362 if self.max is not None and rv > self.max:363 return self.max364 less_than = operator.le if self.exclusive else operator.lt365 greater_than = operator.ge if self.exclusive else operator.gt366367 if (368 self.min is not None364 and rv < self.min369 and less_than(rv, self.min)370 or self.max is not None366 and rv > self.max371 and greater_than(rv, self.max)372 ):373 if self.min is None:374 self.fail(375 f"{rv} is bigger than the maximum valid value {self.max}.",
365 unmodified lines
modified:tests/test_basic.py+18 added
479 unmodified lines
480 result = runner.invoke(clamp, ["--x=-1.0"])481 assert not result.exception482 assert result.output == "0\n"483484 @click.command()485 @click.option('--x', type=click.FloatRange(0, 5, exclusive=True))486 def exclude(x):487 click.echo(x)488489 result = runner.invoke(exclude, ['--x=4.99'])490 assert not result.exception491 assert result.output == '4.99\n'492493 result = runner.invoke(exclude, ['--x=5.0'])494 assert result.exit_code == 2495 assert 'Invalid value for "--x": 5.0 is not in the valid range of 0 to 5.\n' \496 in result.output497498 result = runner.invoke(exclude, ['--x=-3e8'])499 assert result.exit_code == 2500 assert 'Invalid value for "--x": -300000000.0 is not in the valid range of 0 to 5.\n' \501 in result.output502503def test_required_option(runner):504 @click.command()505 @click.option("--foo", required=True)506 def cli(foo):
92 unmodified lines
modified:CHANGES.rst+1 added
18 unmodified lines
19- If validation fails for a prompt with ``hide_input=True``, the value20 is not shown in the error message. :issue:`1460`21- An ``IntRange`` option shows the accepted range in its help text.22 :issue:`1525`23- ``FloatRange`` min and max can be exclusive. :issue:`1100`24- An option defined with duplicate flag names (``"--foo/--foo"``)25 raises a ``ValueError``. :issue:`1465`26- ``echo()`` will not fail when using pytest's ``capsys`` fixture on27 Windows. :issue:`1590`
685 unmodified lines