Skip to content
Myr

detect type from flag_value

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

authored by David Lord · committed by David Lord · parent b131f71 · Browse files · 4 files+25 added−21 removed
This commit

State

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

Diff stats

4 files+25 added−21 removed

  • tests:+7 added−0 removed
  • other:+18 added−21 removed

4 files+25 added−21 removed

modified:src/click/core.py+15 added-17 removed
11 unmodified lines
12from gettext import gettext as _13from gettext import ngettext14from itertools import repeat1516from . import types17from ._unicodefun import _verify_python_env18from .exceptions import Abort19from .exceptions import BadParameter20from .exceptions import ClickException
9 unmodified lines
30from .parser import split_opt31from .termui import confirm32from .termui import prompt33from .termui import style33from .types import _NumberRangeBase34from .types import BOOL35from .types import convert_type36from .types import IntRange37from .types import ParamType34from .utils import _detect_program_name35from .utils import _expand_args36from .utils import echo37from .utils import make_default_short_help
1967 unmodified lines
20052006 def __init__(2007 self,2008 param_decls: t.Optional[t.Sequence[str]] = None,2013 type: t.Optional[t.Union["ParamType", t.Any]] = None,2009 type: t.Optional[t.Union[types.ParamType, t.Any]] = None,2010 required: bool = False,2011 default: t.Optional[t.Union[t.Any, t.Callable[[], t.Any]]] = None,2012 callback: t.Optional[t.Callable[[Context, "Parameter", t.Any], t.Any]] = None,2013 nargs: t.Optional[int] = None,
16 unmodified lines
2030 ) -> None:2031 self.name, self.opts, self.secondary_opts = self._parse_decls(2032 param_decls or (), expose_value2033 )2034 self.type = types.convert_type(type, default)20352039 self.type = convert_type(type, default)20402036 # Default nargs to what the type tells us if we have that2037 # information available.2038 if nargs is None:2039 if self.type.is_composite:
393 unmodified lines
2433 variable in case a prefix is defined on the2434 context.2435 :param help: the help string.2436 :param hidden: hide this option from help outputs.24372438 .. versionchanged:: 8.0.12439 ``type`` is detected from ``flag_value`` if given.2440 """24412442 param_type_name = "option"2443
9 unmodified lines
2453 flag_value: t.Optional[t.Any] = None,2454 multiple: bool = False,2455 count: bool = False,2456 allow_from_autoenv: bool = True,2459 type: t.Optional[t.Union["ParamType", t.Any]] = None,2457 type: t.Optional[t.Union[types.ParamType, t.Any]] = None,2458 help: t.Optional[str] = None,2459 hidden: bool = False,2460 show_choices: bool = True,2461 show_envvar: bool = False,
42 unmodified lines
25042505 if flag_value is None:2506 flag_value = not self.default25072508 if is_flag and type is None:2509 # Re-guess the type from the flag value instead of the2510 # default.2511 self.type = types.convert_type(None, flag_value)25122513 self.is_flag: bool = is_flag2514 self.is_bool_flag = isinstance(self.type, types.BoolParamType)2515 self.flag_value: t.Any = flag_value25162513 if self.is_flag and isinstance(self.flag_value, bool) and type in [None, bool]:2514 self.type: "ParamType" = BOOL2515 self.is_bool_flag = True2516 else:2517 self.is_bool_flag = False25182517 # Counting2518 self.count = count2519 if count:2520 if type is None:2523 self.type = IntRange(min=0)2521 self.type = types.IntRange(min=0)2522 if default_is_missing:2523 self.default = 025242525 self.allow_from_autoenv = allow_from_autoenv
196 unmodified lines
2722 default_string = default_value27232724 extra.append(_("default: {default}").format(default=default_string))27252728 if isinstance(self.type, _NumberRangeBase):2726 if isinstance(self.type, types._NumberRangeBase):2727 range_str = self.type._describe_range()27282729 if range_str:2730 extra.append(range_str)
200 unmodified lines
modified:src/click/types.py+1 added-4 removed
998 unmodified lines
9991000 if ty is float:1001 return FLOAT10021003 # Booleans are only okay if not guessed. For is_flag options with1004 # flag_value, default=True indicates which flag_value is the1005 # default.1006 if ty is bool and not guessed_type:1003 if ty is bool:1004 return BOOL10051006 if guessed_type:1007 return STRING
45 unmodified lines
modified:tests/test_options.py+7 added
754 unmodified lines
755 return opt, a, b756757 result = runner.invoke(cli, args, standalone_mode=False, catch_exceptions=False)758 assert result.return_value == expect759760761def test_type_from_flag_value():762 param = click.Option(["-a", "x"], default=True, flag_value=4)763 assert param.type is click.INT764 param = click.Option(["-b", "x"], flag_value=8)765 assert param.type is click.INT
modified:CHANGES.rst+2 added
14 unmodified lines
15- The ``importlib_metadata`` backport package is installed on Python <16 3.8. :issue:`1889`17- Arguments with ``nargs=-1`` only use env var value if no command18 line values are given. :issue:`1903`19- Flag options guess their type from ``flag_value`` if given, like20 regular options do from ``default``. :issue:`1886`212223Version 8.0.024-------------
856 unmodified lines