Skip to content

Commit c8beb8f

Browse files
Fix cases where a suppressed default was visible (#53)
Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
1 parent 1fd3047 commit c8beb8f

File tree

5 files changed

+48
-12
lines changed

5 files changed

+48
-12
lines changed

.ruff.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ ignore = [
3030
]
3131

3232
[lint.per-file-ignores]
33+
"test/sample-default-supressed.py" = [
34+
"N999", # invalid module name
35+
]
3336
"test/sample-directive-opts.py" = [
3437
"N999", # invalid module name
3538
]

sphinxarg/ext.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ def render_list(l, markdown_help, settings=None):
8686
return all_children
8787

8888

89+
def _is_suppressed(item):
90+
"""Return whether item should not be printed."""
91+
if item is None:
92+
return True
93+
item = str(item).replace('"', '').replace("'", '')
94+
return item == '==SUPPRESS=='
95+
96+
8997
def print_action_groups(
9098
data,
9199
nested_content,
@@ -151,10 +159,7 @@ def print_action_groups(
151159
)
152160
if 'help' in entry:
153161
arg.append(entry['help'])
154-
if entry['default'] is not None and entry['default'] not in [
155-
'"==SUPPRESS=="',
156-
'==SUPPRESS==',
157-
]:
162+
if not _is_suppressed(entry['default']):
158163
# Put the default value in a literal block,
159164
# but escape backticks already in the string
160165
default_str = str(entry['default']).replace('`', r'\`')
@@ -414,10 +419,7 @@ def _format_optional_arguments(self, parser_info):
414419
opt_items = []
415420
for name in opt['name']:
416421
option_declaration = [nodes.option_string(text=name)]
417-
if opt['default'] is not None and opt['default'] not in [
418-
'"==SUPPRESS=="',
419-
'==SUPPRESS==',
420-
]:
422+
if not _is_suppressed(opt['default']):
421423
option_declaration += nodes.option_argument(
422424
'', text='=' + str(opt['default'])
423425
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Default suppressed
2+
==================
3+
4+
.. argparse::
5+
:filename: test/sample-default-supressed.py
6+
:prog: sample-default-suppressed
7+
:func: get_parser

test/sample-default-supressed.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from argparse import ArgumentParser
2+
3+
4+
def get_parser():
5+
parser = ArgumentParser(
6+
prog='sample-default-suppressed', description='Test suppression of version default'
7+
)
8+
parser.add_argument(
9+
'--version', help='print version number', action='version', version='1.2.3'
10+
)
11+
return parser

test/test_default_html.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,18 @@ def get_text(node):
3131
if be_found:
3232
if any(rex.search(get_text(node)) for node in nodes):
3333
return
34+
msg = (
35+
f'{check!r} not found in any node matching path {path} in {fname}: '
36+
f'{[node.text for node in nodes]!r}'
37+
)
3438
else:
3539
if all(not rex.search(get_text(node)) for node in nodes):
3640
return
41+
msg = (
42+
f'Found {check!r} in a node matching path {path} in {fname}: '
43+
f'{[node.text for node in nodes]!r}'
44+
)
3745

38-
msg = (
39-
f'{check!r} not found in any node matching path {path} in {fname}: '
40-
f'{[node.text for node in nodes]!r}'
41-
)
4246
raise AssertionError(msg)
4347

4448

@@ -82,6 +86,15 @@ def get_text(node):
8286
('.//section/dl/dd/p/code/span', r"\['\*.rst',"),
8387
],
8488
),
89+
(
90+
'default-suppressed.html',
91+
[
92+
('.//h1', 'Sample', False),
93+
('.//h1', 'Default suppressed'),
94+
('.//h2', 'Named Arguments'),
95+
('.//section/dl/dd/p', 'Default', False),
96+
],
97+
),
8598
],
8699
)
87100
@pytest.mark.sphinx('html', testroot='default-html')

0 commit comments

Comments
 (0)