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
Provenance
- Commit
ad52b19- Tree
66298b2- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
b131f71
4 files changed+25 added−21 removed
modified:src/click/core.py+15 added-17 removed
11 unmodified lines12 from gettext import gettext as _13 from gettext import ngettext14 from itertools import repeat15 16+from . import types17 from ._unicodefun import _verify_python_env18 from .exceptions import Abort19 from .exceptions import BadParameter20 from .exceptions import ClickException9 unmodified lines30 from .parser import split_opt31 from .termui import confirm32 from .termui import prompt33 from .termui import style33-from .types import _NumberRangeBase34-from .types import BOOL35-from .types import convert_type36-from .types import IntRange37-from .types import ParamType34 from .utils import _detect_program_name35 from .utils import _expand_args36 from .utils import echo37 from .utils import make_default_short_help1967 unmodified lines2005 2006 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 lines2030 ) -> 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)2035 2039- self.type = convert_type(type, default)2040-2036 # 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 lines2433 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.2437+2438+ .. versionchanged:: 8.0.12439+ ``type`` is detected from ``flag_value`` if given.2440 """2441 2442 param_type_name = "option"2443 9 unmodified lines2453 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 lines2504 2505 if flag_value is None:2506 flag_value = not self.default2507 2508+ 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)2512+2513 self.is_flag: bool = is_flag2514+ self.is_bool_flag = isinstance(self.type, types.BoolParamType)2515 self.flag_value: t.Any = flag_value2516 2513- 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 = False2518-2517 # 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 = 02524 2525 self.allow_from_autoenv = allow_from_autoenv196 unmodified lines2722 default_string = default_value2723 2724 extra.append(_("default: {default}").format(default=default_string))2725 2728- if isinstance(self.type, _NumberRangeBase):2726+ if isinstance(self.type, types._NumberRangeBase):2727 range_str = self.type._describe_range()2728 2729 if range_str:2730 extra.append(range_str)200 unmodified linesmodified:src/click/types.py+1 added-4 removed
998 unmodified lines999 1000 if ty is float:1001 return FLOAT1002 1003- # 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 BOOL1005 1006 if guessed_type:1007 return STRING45 unmodified linesmodified:tests/test_options.py+7 added
754 unmodified lines755 return opt, a, b756 757 result = runner.invoke(cli, args, standalone_mode=False, catch_exceptions=False)758 assert result.return_value == expect759+760+761+def 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.INTmodified:CHANGES.rst+2 added
14 unmodified lines15 - 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`21 22 23 Version 8.0.024 -------------856 unmodified lines