Reorder ParameterSource from most to least explicit
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Closes #2879
authored by Kevin Deldycke · committed by Kevin Deldycke · parent 60cad4a · Browse files · 4 files·+56 added−13 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+56 added−13 removed
- other:+56 added−13 removed
Provenance
- Commit
ac5cec5- Tree
a19ef74- Author
Kevin Deldycke <kevin@deldycke.com>- Committer
Kevin Deldycke <kevin@deldycke.com>- Parent
60cad4a
4 files changed+56 added−13 removed
modified:docs/commands-and-groups.md+29 added
340 unmodified lines341 allowed to read from the context, but not to perform any modifications on342 it.343 344 345+### Parameter Source Priority346+347+When a parameter's value is resolved, Click checks several sources348+in the following order of precedence:349+350+1. **Prompt** (`PROMPT`): interactively provided by the user at a prompt.351+2. **Command line** (`COMMANDLINE`): provided as a CLI argument or option.352+3. **Environment variable** (`ENVIRONMENT`): read from an environment variable.353+4. **Default map** (`DEFAULT_MAP`): looked up from {attr}`Context.default_map`.354+5. **Default** (`DEFAULT`): the default value defined on the parameter.355+356+{class}`~click.core.ParameterSource` members are ordered from most357+explicit to least explicit. Because it is an {class}`~enum.IntEnum`,358+you can use comparison operators to check how explicitly a value was359+provided:360+361+```python362+source = ctx.get_parameter_source("port")363+364+# True if the value was explicitly set (command line, prompt, or env var).365+if source < click.ParameterSource.DEFAULT_MAP:366+ ...367+368+# True if the value came from any kind of default.369+if source >= click.ParameterSource.DEFAULT_MAP:370+ ...371+```372+373+374 ### Detecting the Source of a Parameter375 376 In some situations it's helpful to understand whether or not an option377 or parameter came from the command line, the environment, the default24 unmodified linesmodified:src/click/__init__.py+1 added
12 unmodified lines13 from .core import Context as Context14 from .core import Group as Group15 from .core import Option as Option16 from .core import Parameter as Parameter17+from .core import ParameterSource as ParameterSource18 from .decorators import argument as argument19 from .decorators import command as command20 from .decorators import confirmation_option as confirmation_option21 from .decorators import group as group103 unmodified linesmodified:src/click/core.py+23 added-13 removed
138 unmodified lines139 140 return sorted(declaration_order, key=sort_key)141 142 143-class ParameterSource(enum.Enum):144- """This is an :class:`~enum.Enum` that indicates the source of a143+class ParameterSource(enum.IntEnum):144+ """This is an :class:`~enum.IntEnum` that indicates the source of a145 parameter's value.146 147 Use :meth:`click.Context.get_parameter_source` to get the148 source for a parameter by name.149 150+ Members are ordered from most explicit to least explicit source.151+ This allows comparison to check if a value was explicitly provided:152+153+ .. code-block:: python154+155+ source = ctx.get_parameter_source("port")156+ if source < click.ParameterSource.DEFAULT_MAP:157+ ... # value was explicitly set158+159+ .. versionchanged:: 8.3.3160+ Use :class:`~enum.IntEnum` and reorder members from most to161+ least explicit. Supports comparison operators.162+163 .. versionchanged:: 8.0164 Use :class:`~enum.Enum` and drop the ``validate`` method.165 166 .. versionchanged:: 8.0167 Added the ``PROMPT`` value.168 """169 170+ PROMPT = enum.auto()171+ """Used a prompt to confirm a default or provide a value."""172 COMMANDLINE = enum.auto()173 """The value was provided by the command line args."""174 ENVIRONMENT = enum.auto()175 """The value was provided with an environment variable."""161- DEFAULT = enum.auto()162- """Used the default specified by the parameter."""176 DEFAULT_MAP = enum.auto()177 """Used a default provided by :attr:`Context.default_map`."""165- PROMPT = enum.auto()166- """Used a prompt to confirm a default or provide a value."""178+ DEFAULT = enum.auto()179+ """Used the default specified by the parameter."""180 181 182 class Context:183 """The context is a special internal object that holds state relevant2391 unmodified lines2575 # Display a deprecation warning if necessary.2576 if (2577 self.deprecated2578 and value is not UNSET2566- and source not in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)2579+ and source < ParameterSource.DEFAULT_MAP2580 ):2581 extra_message = (2582 f" {self.deprecated}" if isinstance(self.deprecated, str) else ""2583 )713 unmodified lines3297 elif (3298 self.is_flag3299 and value is True3300 and not self.is_bool_flag3288- and source not in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)3301+ and source < ParameterSource.DEFAULT_MAP3302 ):3303 value = self.flag_value3304 3305 # Re-interpret a multiple option which has been sent as-is by the parser.1 unmodified line3307 # FLAG_NEEDS_VALUE sentinel) with the flag_value.3308 elif (3309 self.multiple3310 and value is not UNSET3298- and source not in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)3311+ and source < ParameterSource.DEFAULT_MAP3312 and any(v is FLAG_NEEDS_VALUE for v in value)3313 ):3314 value = [self.flag_value if v is FLAG_NEEDS_VALUE else v for v in value]3315 source = ParameterSource.COMMANDLINE3316 3317 # The value wasn't set, or used the param's default, prompt for one to the user3318 # if prompting is enabled.3319 elif (3307- (3308- value is UNSET3309- or source in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)3310- )3320+ (value is UNSET or source >= ParameterSource.DEFAULT_MAP)3321 and self.prompt is not None3322 and (self.required or self.prompt_required)3323 and not ctx.resilient_parsing3324 ):123 unmodified linesmodified:CHANGES.rst+3 added
24 unmodified lines25 - ``CliRunner``'s redirected streams now expose the original file descriptor26 via ``fileno()``, so that ``faulthandler``, ``subprocess``, and other27 C-level consumers no longer crash with ``io.UnsupportedOperation``.28 :issue:`2865`29+- Change :class:`ParameterSource` to an :class:`~enum.IntEnum` and reorder30+ its members from most to least explicit, so values can be compared to31+ check whether a parameter was explicitly provided. :issue:`2879` :pr:`3248`32 33 Version 8.3.234 -------------35 1323 unmodified lines