Skip to content
Myr

Fix speculative speculative empty string check

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

Closes #3298

authored by Kevin Deldycke · committed by Kevin Deldycke · parent 04ef3a6 · Browse files · 2 files+29 added−5 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+29 added−5 removed

  • tests:+28 added−4 removed
  • other:+1 added−1 removed

2 files+29 added−5 removed

modified:src/click/core.py+1 added-1 removed
3108 unmodified lines
3109 (self.opts if default_value else self.secondary_opts)[0]3110 )[1]3111 elif self.is_bool_flag and not self.secondary_opts and not default_value:3112 default_string = ""3113 elif default_value == "":3113 elif isinstance(default_value, str) and default_value == "":3114 default_string = '""'3115 else:3116 default_string = str(default_value)3117
320 unmodified lines
modified:tests/test_options.py+28 added-4 removed
1257 unmodified lines
1258 message = opt.get_help_record(ctx)[1]1259 assert "[default: (unlimited)]" in message126012611262def test_show_default_with_empty_string(runner):1263 """When show_default is True and default is set to an empty string."""1264 opt = click.Option(["--limit"], default="", show_default=True)1262class _StrictEq:1263 """Object whose ``__eq__`` raises on string comparison (like semver.Version)."""12641265 def __eq__(self, other):1266 if isinstance(other, str):1267 raise ValueError("cannot compare to string")1268 return NotImplemented12691270 def __str__(self):1271 return "strict"127212731274@pytest.mark.parametrize(1275 ("default", "expected"),1276 [1277 ("", '[default: ""]'),1278 (_StrictEq(), "[default: strict]"),1279 ],1280 ids=["empty-string", "non-string-comparable-object"],1281)1282def test_show_default_with_empty_string(runner, default, expected):1283 """The empty-string check in help rendering must not break on objects1284 whose ``__eq__`` raises for string operands.12851286 Regression test for https://github.com/pallets/click/issues/3298.1287 """1288 opt = click.Option(["--limit"], default=default, show_default=True)1289 ctx = click.Context(click.Command("cli"))1290 message = opt.get_help_record(ctx)[1]1267 assert '[default: ""]' in message1291 assert expected in message129212931294def test_do_not_show_no_default(runner):1295 """When show_default is True and no default is set do not show None."""
1232 unmodified lines