Skip to content

Commit

Permalink
FIX Format string defaults correctly
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hoodmane authored and stephenfin committed May 15, 2024
1 parent 34470b6 commit 7f29172
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
features:
- |
Now when the default value of an option is a string it will be rendered with
quotes around it.
fixes:
- |
If an option has an empty string default, docutils will no longer emit
warnings saying ``CRITICAL: Unexpected section title or transition.``
8 changes: 4 additions & 4 deletions sphinx_click/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ def _write_opts(opts: ty.List[str]) -> str:
# Starting from Click 7.0 show_default can be a string. This is
# mostly useful when the default is not a constant and
# documentation thus needs a manually written string.
extras.append(':default: ``%s``' % show_default)
elif opt.default is not None and show_default:
extras.append(':default: ``%r``' % ANSI_ESC_SEQ_RE.sub('', show_default))
elif show_default and opt.default is not None:
extras.append(
':default: ``%s``'
% (
', '.join(str(d) for d in opt.default)
', '.join(repr(d) for d in opt.default)
if isinstance(opt.default, (list, tuple))
else opt.default,
else repr(opt.default),
)
)

Expand Down
16 changes: 13 additions & 3 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ def test_defaults(self):
'--only-show-default',
show_default="Some default computed at runtime!",
)
@click.option('--string-default', default="abc", show_default=True)
@click.option('--empty-string-default', default="", show_default=True)
def foobar(bar):
"""A sample command."""
pass
Expand All @@ -273,15 +275,23 @@ def foobar(bar):
.. option:: --param <param>
:default: ``Something computed at runtime``
:default: ``'Something computed at runtime'``
.. option:: --group <group>
:default: ``('foo', 'bar')``
.. option:: --only-show-default <only_show_default>
:default: ``Some default computed at runtime!``
:default: ``'Some default computed at runtime!'``
.. option:: --string-default <string_default>
:default: ``'abc'``
.. option:: --empty-string-default <empty_string_default>
:default: ``''``
"""
).lstrip(),
'\n'.join(output),
Expand Down Expand Up @@ -438,7 +448,7 @@ def foobar():
.. option:: --param <param>
:default: ``Something computed at runtime``
:default: ``'Something computed at runtime'``
A sample epilog.
"""
Expand Down

0 comments on commit 7f29172

Please sign in to comment.