Reorder `ParameterSource` (#3248)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents 76552ff, ac5cec5 · 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
c7e1ba8- Tree
a733a46- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
76552ff- Parent 2
ac5cec5
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 )732 unmodified lines3316 elif (3317 self.is_flag3318 and value is True3319 and not self.is_bool_flag3307- and source not in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)3320+ and source < ParameterSource.DEFAULT_MAP3321 ):3322 value = self.flag_value3323 3324 # Re-interpret a multiple option which has been sent as-is by the parser.1 unmodified line3326 # FLAG_NEEDS_VALUE sentinel) with the flag_value.3327 elif (3328 self.multiple3329 and value is not UNSET3317- and source not in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)3330+ and source < ParameterSource.DEFAULT_MAP3331 and any(v is FLAG_NEEDS_VALUE for v in value)3332 ):3333 value = [self.flag_value if v is FLAG_NEEDS_VALUE else v for v in value]3334 source = ParameterSource.COMMANDLINE3335 3336 # The value wasn't set, or used the param's default, prompt for one to the user3337 # if prompting is enabled.3338 elif (3326- (3327- value is UNSET3328- or source in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)3329- )3339+ (value is UNSET or source >= ParameterSource.DEFAULT_MAP)3340 and self.prompt is not None3341 and (self.required or self.prompt_required)3342 and not ctx.resilient_parsing3343 ):123 unmodified linesmodified:CHANGES.rst+3 added
32 unmodified lines33 - ``CliRunner``'s redirected streams now expose the original file descriptor34 via ``fileno()``, so that ``faulthandler``, ``subprocess``, and other35 C-level consumers no longer crash with ``io.UnsupportedOperation``.36 :issue:`2865`37+- Change :class:`ParameterSource` to an :class:`~enum.IntEnum` and reorder38+ its members from most to least explicit, so values can be compared to39+ check whether a parameter was explicitly provided. :issue:`2879` :pr:`3248`40 41 Version 8.3.242 -------------43 1323 unmodified lines