Skip to content
Myr

check default_map in param.get_default

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

Applies overrides consistently for defaults, help text, prompts, and invoke().

authored by David Lord · committed by David Lord · parent 54102a4 · Browse files · 2 files+52 added−22 removed
This commit

State

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

Diff stats

2 files+52 added−22 removed

  • other:+52 added−22 removed

2 files+52 added−22 removed

modified:src/click/core.py+49 added-20 removed
1import enum2import errno3import inspect3import os4import sys5from contextlib import contextmanager6from contextlib import ExitStack
604 unmodified lines
611 if rv is None:612 self.obj = rv = object_type()613 return rv614616 def lookup_default(self, name):617 """Looks up the default for a parameter name. This by default618 looks into the :attr:`default_map` if available.615 def lookup_default(self, name, call=True):616 """Get the default for a parameter from :attr:`default_map`.617618 :param name: Name of the parameter.619 :param call: If the default is a callable, call it. Disable to620 return the callable instead.621622 .. versionchanged:: 8.0623 Added the ``call`` parameter.624 """625 if self.default_map is not None:621 rv = self.default_map.get(name)622 if callable(rv):623 rv = rv()624 return rv626 value = self.default_map.get(name)627628 if call and callable(value):629 return value()630631 return value632633 def fail(self, message):634 """Aborts the execution of the program with a specific error635 message.636
1289 unmodified lines
1926 if self.nargs != 1:1927 metavar += "..."1928 return metavar19291923 def get_default(self, ctx):1924 """Given a context variable this calculates the default value."""1925 # Otherwise go with the regular default.1926 if callable(self.default):1927 rv = self.default()1928 else:1929 rv = self.default1930 return self.type_cast_value(ctx, rv)1930 def get_default(self, ctx, call=True):1931 """Get the default for the parameter. Tries1932 :meth:`Context.lookup_value` first, then the local default.19331934 :param ctx: Current context.1935 :param call: If the default is a callable, call it. Disable to1936 return the callable instead.19371938 .. versionchanged:: 8.01939 Looks at ``ctx.default_map`` first.19401941 .. versionchanged:: 8.01942 Added the ``call`` parameter.1943 """1944 value = ctx.lookup_default(self.name, call=False)19451946 if value is None:1947 value = self.default19481949 if callable(value):1950 if not call:1951 # Don't type cast the callable.1952 return value19531954 value = value()19551956 return self.type_cast_value(ctx, value)19571958 def add_to_parser(self, parser, ctx):1959 pass19601961 def consume_value(self, ctx, opts):
419 unmodified lines
2381 else envvar2382 )2383 extra.append(f"env var: {var_str}")23842359 default_value = ctx.lookup_default(self.name) or self.default2385 default_value = self.get_default(ctx, call=False)23862387 if default_value is not None and (self.show_default or ctx.show_default):2388 if isinstance(self.show_default, str):2389 default_string = f"({self.show_default})"2390 elif isinstance(default_value, (list, tuple)):2391 default_string = ", ".join(str(d) for d in default_value)2365 elif inspect.isfunction(default_value):2392 elif callable(default_value):2393 default_string = "(dynamic)"2394 elif self.is_bool_flag and self.secondary_opts:2395 # For boolean flags that have distinct True/False opts,2396 # use the opt without prefix instead of the value.
1 unmodified line
2398 (self.opts if self.default else self.secondary_opts)[0]2399 )[1]2400 else:2401 default_string = default_value24022403 extra.append(f"default: {default_string}")24042405 if isinstance(self.type, _NumberRangeBase):2406 range_str = self.type._describe_range()
8 unmodified lines
2415 help = f"{help} [{extra_str}]" if help else f"[{extra_str}]"24162417 return ("; " if any_prefix_is_slash else " / ").join(rv), help24182391 def get_default(self, ctx):2419 def get_default(self, ctx, call=True):2420 # If we're a non boolean flag our default is more complex because2421 # we need to look at all flags in the same group to figure out2422 # if we're the the default one in which case we return the flag2423 # value as default.2424 if self.is_flag and not self.is_bool_flag:2425 for param in ctx.command.params:2426 if param.name == self.name and param.default:2427 return param.flag_value24282429 return None24302402 return super().get_default(ctx)2431 return super().get_default(ctx, call=call)24322433 def prompt_for_value(self, ctx):2434 """This is an alternative flow that can be activated in the full2435 value processing if a value does not exist. It will prompt the
131 unmodified lines
modified:CHANGES.rst+3 added-2 removed
132 unmodified lines
133 empty tuple. :issue:`472`134- Handle empty env vars as though the option were not passed. This135 extends the change introduced in 7.1 to be consistent in more cases.136 :issue:`1285`137- ``show_default`` considers ``default_map`` to display the138 proper default value. :issue:`1548`137- ``Parameter.get_default()`` checks ``Context.default_map`` to138 handle overrides consistently in help text, ``invoke()``, and139 prompts. :issue:`1548`140141142Version 7.1.2143-------------
635 unmodified lines