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
2 changes: 1 addition & 1 deletion src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3110,7 +3110,7 @@ def get_help_extra(self, ctx: Context) -> types.OptionHelpExtra:
)[1]
elif self.is_bool_flag and not self.secondary_opts and not default_value:
default_string = ""
elif default_value == "":
elif isinstance(default_value, str) and default_value == "":
default_string = '""'
else:
default_string = str(default_value)
Expand Down
32 changes: 28 additions & 4 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -1259,12 +1259,36 @@ def test_show_default_string(runner):
assert "[default: (unlimited)]" in message


def test_show_default_with_empty_string(runner):
"""When show_default is True and default is set to an empty string."""
opt = click.Option(["--limit"], default="", show_default=True)
class _StrictEq:
"""Object whose ``__eq__`` raises on string comparison (like semver.Version)."""

def __eq__(self, other):
if isinstance(other, str):
raise ValueError("cannot compare to string")
return NotImplemented

def __str__(self):
return "strict"


@pytest.mark.parametrize(
("default", "expected"),
[
("", '[default: ""]'),
(_StrictEq(), "[default: strict]"),
],
ids=["empty-string", "non-string-comparable-object"],
)
def test_show_default_with_empty_string(runner, default, expected):
"""The empty-string check in help rendering must not break on objects
whose ``__eq__`` raises for string operands.

Regression test for https://github.com/pallets/click/issues/3298.
"""
opt = click.Option(["--limit"], default=default, show_default=True)
ctx = click.Context(click.Command("cli"))
message = opt.get_help_record(ctx)[1]
assert '[default: ""]' in message
assert expected in message


def test_do_not_show_no_default(runner):
Expand Down