Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/bug-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ about: Report a bug in Click (not other projects which depend on Click)
---

<!--
This issue tracker is a tool to address bugs in Click itself. Please use
GitHub Discussions or the Pallets Discord for questions about your own code.
This issue tracker is a tool to address bugs in Click itself. Please use the
Pallets Discord (https://discord.gg/pallets) for questions about your own code.

Replace this comment with a clear outline of what the bug is.
-->
Expand Down
3 changes: 0 additions & 3 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
blank_issues_enabled: false
contact_links:
- name: Questions on Discussions
url: https://github.com/pallets/click/discussions/
about: Ask questions about your own code on the Discussions tab.
- name: Questions on Chat
url: https://discord.gg/pallets
about: Ask questions about your own code on our Discord chat.
3 changes: 1 addition & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ Released 2026-08-24
convention. {issue}`2877` {pr}`3642`
- Add {func}`custom_version_option`, a `--version` option whose output is
produced by a callback, covering cases {func}`version_option` intentionally
does not. The feature set of {func}`version_option` is now frozen; see
[discussion #3527](https://github.com/pallets/click/discussions/3527). {pr}`3581`
does not. The feature set of {func}`version_option` is now frozen. {pr}`3581`
- `style()` and `secho()` no longer silently drop the 256-color index `0`
(black) passed as `fg` or `bg`, and now validate color arguments. Invalid
colors raise a `ValueError` instead of a `TypeError`. {pr}`3677`
Expand Down
2 changes: 1 addition & 1 deletion docs/upgrade-guides.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This guide assumes the user is on version 8.3.X.
### Deprecations

For each deprecation, provide a brief explanation, and direct users to new function / class if available.
- `CliRunner.isolated_filesystem()` is deprecated and will be removed in Click 9.0. The helper predates Python 3 and modern pytest, and it relies on `os.chdir`, which mutates process-global state and is therefore not thread-safe. Replace it with a temporary directory (`tempfile.TemporaryDirectory`, or pytest's `tmp_path` fixture) and pass absolute paths to the command instead of relying on the current working directory. To run commands in parallel, use process-based isolation (such as `pytest-xdist`) rather than threads, since `CliRunner.invoke()` also redirects the process-global standard streams and other interpreter-wide state. See [#3700](https://github.com/pallets/click/issues/3700), [#3501](https://github.com/pallets/click/issues/3501) and the [testing guide](testing.md#running-tests-in-parallel).
- `CliRunner.isolated_filesystem()` is deprecated and will be removed in Click 9.0. The helper predates Python 3 and modern pytest, and it relies on `os.chdir`, which mutates process-global state and is therefore not thread-safe. Replace it with a temporary directory (`tempfile.TemporaryDirectory`, or pytest's `tmp_path` fixture) and pass absolute paths to the command instead of relying on the current working directory. To run commands in parallel, use process-based isolation (such as `pytest-xdist`) rather than threads, since `CliRunner.invoke()` also redirects the process-global standard streams and other interpreter-wide state. See the [testing guide](testing.md#running-tests-in-parallel).

### Removals with prior deprecation

Expand Down
8 changes: 3 additions & 5 deletions src/click/_termui_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,9 @@ def _resolve_pager_command(cmd_parts: list[str]) -> tuple[Path, list[str]] | Non
if not cmd_filepath:
return None

# Normalize to an absolute path without resolving symlinks: multi-call
# binaries such as busybox derive their identity from the link they are
# invoked through, so resolving less -> busybox makes them misbehave.
# See https://github.com/pallets/click/issues/2943 and
# https://github.com/pallets/click/pull/2944
# Normalize to an absolute path without resolving symlinks: multi-call binaries such
# as busybox derive their identity from the link they are invoked through, so
# resolving less -> busybox makes them misbehave.
return Path(cmd_filepath).absolute(), cmd_parts[1:]


Expand Down
83 changes: 31 additions & 52 deletions src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,9 @@ def __init__(
self._depth = 0
self._parameter_source = {}
# Tracks whether the option that currently owns each parameter slot in
# :attr:`params` had its ``default`` set explicitly by the user. Used
# to tie-break feature-switch groups where multiple options share a
# parameter name and both fall back to their default value.
# Refs: https://github.com/pallets/click/issues/3403
# :attr:`params` had its ``default`` set explicitly by the user. Used to
# tie-break feature-switch groups where multiple options share a parameter name
# and both fall back to their default value.
self._param_default_explicit = {}
self._exit_stack = ExitStack()

Expand Down Expand Up @@ -897,10 +896,7 @@ def invoke(
# We explicitly hide the :attr:`UNSET` value to the user, as we
# choose to make it an implementation detail. And because ``invoke``
# has been designed as part of Click public API, we return ``None``
# instead. Refs:
# https://github.com/pallets/click/issues/3066
# https://github.com/pallets/click/issues/3065
# https://github.com/pallets/click/pull/3068
# instead.
if default_value is UNSET:
default_value = None
kwargs[param.name] = param.type_cast_value(ctx, default_value)
Expand Down Expand Up @@ -1384,9 +1380,6 @@ def parse_args(self, ctx: Context, args: list[str]) -> list[str]:
#
# Waiting until after the initial parse to convert allows us to treat `UNSET`
# more like a missing value when multiple params use the same name.
# Refs:
# https://github.com/pallets/click/issues/3071
# https://github.com/pallets/click/pull/3079
for name, value in ctx.params.items():
if value is UNSET:
ctx.params[name] = None
Expand Down Expand Up @@ -2342,11 +2335,9 @@ def __init__(
# arbitrage the default value of coupled Parameters sharing the same
# self.name, like flag options),
# - and the actual value of the default.
# It is confusing and is the source of many issues discussed in:
# https://github.com/pallets/click/pull/3030
# In the future, we might think of splitting it in two, not unlike
# Option.is_flag and Option.flag_value: we could have something like
# Parameter.is_default and Parameter.default_value.
# It is confusing and is the source of many issues. In the future, we might
# think of splitting it in two, not unlike Option.is_flag and Option.flag_value:
# we could have something like Parameter.is_default and Parameter.default_value.
default: t.Any | t.Callable[[], t.Any] | None = UNSET,
callback: t.Callable[[Context, Parameter, t.Any], t.Any] | None = None,
nargs: int | None = None,
Expand Down Expand Up @@ -2381,11 +2372,10 @@ def __init__(
self.multiple = multiple
self.expose_value = expose_value
self.default = default
# Whether the user passed ``default`` explicitly to the constructor.
# Captured before any auto-derived default (like ``False`` for boolean
# flags in :class:`Option`) replaces the :data:`UNSET` sentinel, so it
# remains ``False`` when the default was inferred rather than chosen.
# Refs: https://github.com/pallets/click/issues/3403
# Whether the user passed ``default`` explicitly to the constructor. Captured
# before any auto-derived default (like ``0`` for a ``count`` :class:`Option`)
# replaces the :data:`UNSET` sentinel, so it remains ``False`` when the default
# was inferred rather than chosen.
self._default_explicit = default is not UNSET
self.is_eager = is_eager
self.metavar = metavar
Expand Down Expand Up @@ -2661,10 +2651,10 @@ def process_value(self, ctx: Context, value: t.Any) -> t.Any:
:meta private:
"""
# shelter `type_cast_value` from ever seeing an `UNSET` value by handling the
# cases in which `UNSET` gets special treatment explicitly at this layer
#
# Refs:
# https://github.com/pallets/click/issues/3069
# cases in which `UNSET` gets special treatment explicitly at this layer.
# Subclasses can override the public `type_cast_value`, so the internal sentinel
# must not reach it. A missing value skips the call rather than passing `None`,
# which `value_is_missing` would not detect.
if value is UNSET:
if self.multiple or self.nargs == -1:
value = ()
Expand All @@ -2686,12 +2676,8 @@ def process_value(self, ctx: Context, value: t.Any) -> t.Any:
if not unset_keys:
value = self.callback(ctx, self, value)

# Legacy case: provide a temporarily manipulated context to the callback
# to hide UNSET values as None.
#
# Refs:
# https://github.com/pallets/click/issues/3136
# https://github.com/pallets/click/pull/3137
# Legacy case: provide a temporarily manipulated context to the callback to
# hide UNSET values as None.
else:
# Add another layer to the context stack to clearly hint that the
# context is temporarily modified.
Expand Down Expand Up @@ -2831,8 +2817,8 @@ def handle_parse_result(
# to UNSET, which will be interpreted as a missing value.
value = UNSET

# Arbitrate the slot when several parameters target the same variable
# name (feature-switch groups). See: https://github.com/pallets/click/issues/3403
# Arbitrate the slot when several parameters target the same variable name
# (feature-switch groups).
slot_empty = existing_value is UNSET
more_explicit = existing_source is not None and source < existing_source
same_source = existing_source is not None and source == existing_source
Expand Down Expand Up @@ -3056,9 +3042,8 @@ def __init__(
# sentinel when the user didn't pass them. Auto-derived values are resolved
# lazily in :meth:`_resolve_lazy_default` and :attr:`flag_activation_value`.
# Keeping the raw values means ``is UNSET`` reliably answers "did the user pass
# this?": #3403 needs it for arbitration, and any future feature needing the
# same distinction can reuse it without reintroducing parallel "was it
# explicit?" tracking.
# this?", and any future feature needing the same distinction can reuse it
# without reintroducing parallel "was it explicit?" tracking.
self.flag_value = flag_value
self.count = count
if count and self.default is UNSET:
Expand Down Expand Up @@ -3100,11 +3085,11 @@ def _infer_flag_kind(
"""Resolve ``is_flag`` and the parser hint ``_flag_needs_value``.

Returns ``(is_flag, flag_needs_value)``, where ``_flag_needs_value`` tells the
parser this option is a flag that cannot be used standalone and needs a value.
The parser uses it to decide whether to treat the next CLI token as the flag's
value or as a new option. If ``prompt`` is enabled with
``prompt_required=False``, it opens the door for an interactive value, hence the
initial condition. Ref: https://github.com/pallets/click/issues/3084
parser that the value of this option can be omitted: the option then prompts for
a value if ``prompt`` is set, and otherwise uses its flag value. The parser uses
it to decide whether to treat the next CLI token as the option's value or as a
new option. If ``prompt`` is enabled with ``prompt_required=False``, it opens
the door for an interactive value, hence the initial condition.
"""
needs_value = self.prompt is not None and not self.prompt_required

Expand Down Expand Up @@ -3242,9 +3227,8 @@ def get_default(
Several auto-derived defaults are resolved lazily here rather than eagerly in
:meth:`__init__`. This keeps :attr:`default` raw at construction time, so
``self.default is UNSET`` reliably answers "did the user pass a default?" for
feature-switch-group arbitration and for anything else that needs to distinguish
"absent" from a chosen value. The resolution rules live in
:meth:`_resolve_lazy_default`.
anything that needs to distinguish "absent" from a chosen value. The resolution
rules live in :meth:`_resolve_lazy_default`.

.. versionchanged:: 8.5.0
``UNSET`` defaults for boolean and multi flags are now resolved here instead
Expand All @@ -3262,8 +3246,7 @@ def get_default(
# Only invoke the value as a callable when the lazy resolver passed it through
# unchanged. If the resolver substituted ``True`` -> :attr:`flag_value`, the
# result is the programmer-supplied ``flag_value`` (often a class or enum),
# which must NOT be instantiated here. See
# https://github.com/pallets/click/issues/3121.
# which must NOT be instantiated here.
if value is raw and call and callable(value):
value = value()
return value
Expand Down Expand Up @@ -3544,9 +3527,8 @@ def prompt_for_value(self, ctx: Context) -> t.Any:
# 3) still declare a non-boolean default.
# This forced casting into a boolean is necessary to align any non-boolean
# default to the prompt, which is going to be a [y/n]-style confirmation
# because the option is still a boolean flag. That way, instead of [y/n],
# we get [Y/n] or [y/N] depending on the truthy value of the default.
# Refs: https://github.com/pallets/click/pull/3030#discussion_r2289180249
# because the option is still a boolean flag. That way, instead of [y/n], we
# get [Y/n] or [y/N] depending on the truthy value of the default.
else:
default = bool(default)
return confirm(self.prompt, default)
Expand Down Expand Up @@ -3701,9 +3683,6 @@ def consume_value(
def process_value(self, ctx: Context, value: t.Any) -> t.Any:
# process_value has to be overridden on Options in order to capture
# `value == UNSET` cases before `type_cast_value()` gets called.
#
# Refs:
# https://github.com/pallets/click/issues/3069
if self.is_flag and not self.required and self.is_bool_flag and value is UNSET:
value = False

Expand Down
14 changes: 6 additions & 8 deletions src/click/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,12 @@ def version_option(
resolves to the ``Pillow`` distribution.

.. note::
The parameters and message variables accepted by this option are
frozen: no new slots will be added, to keep the common case simple
and predictable. If you need values it does not expose, such as a
file path, the Python version, or git metadata, use
:func:`custom_version_option` to render the output yourself.

Rationale: `discussion #3527
<https://github.com/pallets/click/discussions/3527>`_.
The parameters and message variables accepted by this option are frozen: no new
slots will be added, to keep the common case simple and predictable. Extra
values have no single right format: a path can point to the entry point or to
the module, and a Python version can be the full string or only ``X.Y``. If you
need values it does not expose, such as a file path, the Python version, or git
metadata, use :func:`custom_version_option` to render the output yourself.

:param version: The version number to show. If not provided, Click
will try to detect it.
Expand Down
27 changes: 11 additions & 16 deletions src/click/shell_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,11 @@ def __getattr__(self, name: str) -> t.Any:
%(complete_func)s_setup;
"""

# See ZshComplete.format_completion below, and issue #2703, before
# changing this script.
# See ZshComplete.format_completion below before changing this script.
#
# (TL;DR: _describe is picky about the format, but this Zsh script snippet
# is already widely deployed. So freeze this script, and use clever-ish
# handling of colons in ZshComplet.format_completion.)
# (TL;DR: _describe is picky about the format, but this Zsh script snippet is already
# widely deployed. So freeze this script, and use clever-ish handling of colons in
# ZshComplete.format_completion.)
_SOURCE_ZSH = """\
#compdef %(prog_name)s

Expand Down Expand Up @@ -465,18 +464,14 @@ def get_completion_args(self) -> tuple[list[str], str]:

def format_completion(self, item: CompletionItem[str]) -> str:
help_ = item.help or "_"
# The zsh completion script uses `_describe` on items with help
# texts (which splits the item help from the item value at the
# first unescaped colon) and `compadd` on items without help
# text (which uses the item value as-is and does not support
# colon escaping). So escape colons in the item value if and
# only if the item help is not the sentinel "_" value, as used
# by the completion script.
# The zsh completion script uses `_describe` on items with help texts (which
# splits the item help from the item value at the first unescaped colon) and
# `compadd` on items without help text (which uses the item value as-is and does
# not support colon escaping). So escape colons in the item value if and only if
# the item help is not the sentinel "_" value, as used by the completion script.
#
# (The zsh completion script is potentially widely deployed, and
# thus harder to fix than this method.)
#
# See issue #1812 and issue #2703 for further context.
# (The zsh completion script is potentially widely deployed, and thus harder to
# fix than this method.)
value = item.value.replace(":", r"\:") if help_ != "_" else item.value
return f"{item.type}\n{value}\n{help_}"

Expand Down
Loading
Loading