From b193cd673304390e786e3b386eb845d18ef0dede Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Thu, 9 May 2024 10:52:17 -0400 Subject: [PATCH] FIX If the `help` of an argument is `None`, don't fail This is a fix for a regression introduced in #134. It's possible that the argument has a `help` attribute, but the value of it is `None`. We then hit a type error where we pass `None` to `re.sub`. --- sphinx_click/ext.py | 5 +++-- tests/test_formatter.py | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/sphinx_click/ext.py b/sphinx_click/ext.py index f398aad..9274f73 100644 --- a/sphinx_click/ext.py +++ b/sphinx_click/ext.py @@ -218,9 +218,10 @@ def _format_argument(arg: click.Argument) -> ty.Generator[str, None, None]: ) ) # Subclasses of click.Argument may add a `help` attribute (like typer.main.TyperArgument) - if hasattr(arg, 'help'): + help = getattr(arg, 'help', None) + if help: yield '' - help_string = ANSI_ESC_SEQ_RE.sub('', getattr(arg, 'help')) + help_string = ANSI_ESC_SEQ_RE.sub('', help) for line in _format_help(help_string): yield _indent(line) diff --git a/tests/test_formatter.py b/tests/test_formatter.py index b55b9d9..6c4029d 100644 --- a/tests/test_formatter.py +++ b/tests/test_formatter.py @@ -185,6 +185,7 @@ def __init__(self, *args, help=None, **kwargs): @click.command() @click.option('--option', help='A sample option') @click.argument('ARG', help='A sample argument', cls=CustomArgument) + @click.argument('ARG_NO_HELP', cls=CustomArgument) def foobar(bar): """A sample command.""" pass @@ -200,7 +201,7 @@ def foobar(bar): .. program:: foobar .. code-block:: shell - foobar [OPTIONS] ARG + foobar [OPTIONS] ARG ARG_NO_HELP .. rubric:: Options @@ -216,6 +217,10 @@ def foobar(bar): A sample argument + + .. option:: ARG_NO_HELP + + Required argument """ ).lstrip(), '\n'.join(output),