Skip to content

Commit

Permalink
Fix cases where a suppressed default was visible (#53)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
  • Loading branch information
michele-riva and AA-Turner authored Jul 15, 2024
1 parent 1fd3047 commit c8beb8f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ ignore = [
]

[lint.per-file-ignores]
"test/sample-default-supressed.py" = [
"N999", # invalid module name
]
"test/sample-directive-opts.py" = [
"N999", # invalid module name
]
Expand Down
18 changes: 10 additions & 8 deletions sphinxarg/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ def render_list(l, markdown_help, settings=None):
return all_children


def _is_suppressed(item):
"""Return whether item should not be printed."""
if item is None:
return True
item = str(item).replace('"', '').replace("'", '')
return item == '==SUPPRESS=='


def print_action_groups(
data,
nested_content,
Expand Down Expand Up @@ -151,10 +159,7 @@ def print_action_groups(
)
if 'help' in entry:
arg.append(entry['help'])
if entry['default'] is not None and entry['default'] not in [
'"==SUPPRESS=="',
'==SUPPRESS==',
]:
if not _is_suppressed(entry['default']):
# Put the default value in a literal block,
# but escape backticks already in the string
default_str = str(entry['default']).replace('`', r'\`')
Expand Down Expand Up @@ -414,10 +419,7 @@ def _format_optional_arguments(self, parser_info):
opt_items = []
for name in opt['name']:
option_declaration = [nodes.option_string(text=name)]
if opt['default'] is not None and opt['default'] not in [
'"==SUPPRESS=="',
'==SUPPRESS==',
]:
if not _is_suppressed(opt['default']):
option_declaration += nodes.option_argument(
'', text='=' + str(opt['default'])
)
Expand Down
7 changes: 7 additions & 0 deletions test/roots/test-default-html/default-suppressed.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Default suppressed
==================

.. argparse::
:filename: test/sample-default-supressed.py
:prog: sample-default-suppressed
:func: get_parser
11 changes: 11 additions & 0 deletions test/sample-default-supressed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from argparse import ArgumentParser


def get_parser():
parser = ArgumentParser(
prog='sample-default-suppressed', description='Test suppression of version default'
)
parser.add_argument(
'--version', help='print version number', action='version', version='1.2.3'
)
return parser
21 changes: 17 additions & 4 deletions test/test_default_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ def get_text(node):
if be_found:
if any(rex.search(get_text(node)) for node in nodes):
return
msg = (
f'{check!r} not found in any node matching path {path} in {fname}: '
f'{[node.text for node in nodes]!r}'
)
else:
if all(not rex.search(get_text(node)) for node in nodes):
return
msg = (
f'Found {check!r} in a node matching path {path} in {fname}: '
f'{[node.text for node in nodes]!r}'
)

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


Expand Down Expand Up @@ -82,6 +86,15 @@ def get_text(node):
('.//section/dl/dd/p/code/span', r"\['\*.rst',"),
],
),
(
'default-suppressed.html',
[
('.//h1', 'Sample', False),
('.//h1', 'Default suppressed'),
('.//h2', 'Named Arguments'),
('.//section/dl/dd/p', 'Default', False),
],
),
],
)
@pytest.mark.sphinx('html', testroot='default-html')
Expand Down

0 comments on commit c8beb8f

Please sign in to comment.