Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2723,15 +2723,13 @@ def format_help(self):
return formatter.format_help()

def _get_formatter(self):
if isinstance(self.formatter_class, type) and issubclass(
self.formatter_class, HelpFormatter
):
try:
return self.formatter_class(
prog=self.prog,
prefix_chars=self.prefix_chars,
color=self.color,
)
else:
except TypeError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeError seems a bit generic (you're calling user code).

What do you think about this?

len([v.kind for (k, v) in inspect.signature(self.formatter_class).parameters.items() if k in ('y', 'z') and v.kind in (inspect._ParameterKind.POSITIONAL_OR_KEYWORD, inspect._ParameterKind.KEYWORD_ONLY)]) == 2

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't work: FAILED (failures=2, errors=156)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, it does, obviously need to replace y and z :)

return self.formatter_class(prog=self.prog)

# =====================
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5176,6 +5176,30 @@ class TestHelpTupleMetavarPositional(HelpTestCase):
version = ''


class TestHelpFormatter(HelpTestCase):
"""Test the HelpFormatter"""

# Test subclassing the help formatter
class MyFormatter(argparse.HelpFormatter):
def __init__(self, prog) -> None:
super().__init__(prog)

parser_signature = Sig(
prog="PROG",
formatter_class=MyFormatter,
description="Test with subclassing the help formatter",
)
usage = '''\
usage: PROG [-h]
'''
help = usage + '''\

Test with subclassing the help formatter

options:
-h, --help show this help message and exit
'''

class TestHelpRawText(HelpTestCase):
"""Test the RawTextHelpFormatter"""

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix subclassing :meth:`!argparse.HelpFormatter`. Patch by Hugo van Kemenade.
Loading