Skip to content

Commit 88e1a84

Browse files
committed
FIX Format string defaults correctly
Before this patch, string defaults are formatted without quotes around them. However, if the default is a tuple or list of strings, quotes will be included: ``` default="a" ==> :default: ``a`` default=("a",) ==> :default: ``('a')`` ``` This is a slightly annoying inconsistency, and leads to potential for confusion between `default="0"` and `default=0`. Most bothersome is that in the case of an empty string we get: ``` :default: ```` ``` Which makes docutils angry: ``` CRITICAL: Unexpected section title or transition. ```` ``` This fixes the trouble by formatting the repr of the default.
1 parent cee73bd commit 88e1a84

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
features:
3+
- |
4+
Now when the default value of an option is a string it will be rendered with
5+
quotes around it.
6+
fixes:
7+
- |
8+
If an option has an empty string default, docutils will no longer emit
9+
warnings saying `CRITICAL: Unexpected section title or transition.`

sphinx_click/ext.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,18 @@ def _write_opts(opts: ty.List[str]) -> str:
104104
else:
105105
show_default = ctx.show_default
106106

107-
if isinstance(show_default, str):
107+
if show_default and isinstance(show_default, str):
108108
# Starting from Click 7.0 show_default can be a string. This is
109109
# mostly useful when the default is not a constant and
110110
# documentation thus needs a manually written string.
111111
extras.append(':default: ``%s``' % show_default)
112-
elif opt.default is not None and show_default:
112+
elif show_default and opt.default is not None:
113113
extras.append(
114114
':default: ``%s``'
115115
% (
116116
', '.join(str(d) for d in opt.default)
117117
if isinstance(opt.default, (list, tuple))
118-
else opt.default,
118+
else repr(opt.default),
119119
)
120120
)
121121

tests/test_formatter.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ def test_defaults(self):
243243
'--only-show-default',
244244
show_default="Some default computed at runtime!",
245245
)
246+
@click.option('--string-default', default="abc", show_default=True)
247+
@click.option('--empty-string-default', default="", show_default=True)
246248
def foobar(bar):
247249
"""A sample command."""
248250
pass
@@ -277,6 +279,14 @@ def foobar(bar):
277279
.. option:: --only-show-default <only_show_default>
278280
279281
:default: ``Some default computed at runtime!``
282+
283+
.. option:: --string-default <string_default>
284+
285+
:default: ``'abc'``
286+
287+
.. option:: --empty-string-default <empty_string_default>
288+
289+
:default: ``''``
280290
"""
281291
).lstrip(),
282292
'\n'.join(output),

0 commit comments

Comments
 (0)