click-contrib / sphinx-click

A Sphinx plugin to automatically document click-based applications
MIT License
212 stars 57 forks source link

TypeError when reading list of tuple defaults #91

Closed teschmitt closed 2 years ago

teschmitt commented 2 years ago

I have an option to a subcommand set up like this:

@click.option("--group", default=[("foo", "bar")], nargs=2, type=click.Tuple([str, str]), multiple=True, show_default=True,)

So it's a multi-value, multiple option option. During make html I get a TypeError in sphinx_click/ext.py:

[…]
  File "[…]/.cache/pypoetry/virtualenvs/[…]/lib/python3.9/site-packages/sphinx_click/ext.py", line 86, in _get_help_record
    ', '.join('%s' % d for d in opt.default)
  File "/home/thomas/.cache/pypoetry/virtualenvs/onetwo-reporting-s9O3aHpb-py3.9/lib/python3.9/site-packages/sphinx_click/ext.py", line 86, in <genexpr>
    ', '.join('%s' % d for d in opt.default)
TypeError: not all arguments converted during string formatting

But I've managed to solve the issue by replacing the old-school %-formatting with a cast and f-strings in the else block in ext.py starting on line 83:

default_str = ', '.join(
    str(d) for d in opt.default
) if isinstance(opt.default, (list, tuple)) else opt.default
extras.append(f':default: {default_str}')

My versions:

Sorry for the cryptic title, please change if you have a better idea.

PS: I just noticed that if I declare default=[["foo", "bar"]] the error goes away also. Since the click docs aren't clear on how to define the defaults in this case, I'll still leave this here in case anybody wants to fix it in the future.