Skip to content

Commit b1c0c60

Browse files
committed
Fix subclassing HelpFormatter
1 parent 45bb5ba commit b1c0c60

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

Lib/argparse.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -2723,15 +2723,13 @@ def format_help(self):
27232723
return formatter.format_help()
27242724

27252725
def _get_formatter(self):
2726-
if isinstance(self.formatter_class, type) and issubclass(
2727-
self.formatter_class, HelpFormatter
2728-
):
2726+
try:
27292727
return self.formatter_class(
27302728
prog=self.prog,
27312729
prefix_chars=self.prefix_chars,
27322730
color=self.color,
27332731
)
2734-
else:
2732+
except TypeError:
27352733
return self.formatter_class(prog=self.prog)
27362734

27372735
# =====================

Lib/test/test_argparse.py

+24
Original file line numberDiff line numberDiff line change
@@ -5176,6 +5176,30 @@ class TestHelpTupleMetavarPositional(HelpTestCase):
51765176
version = ''
51775177

51785178

5179+
class TestHelpFormatter(HelpTestCase):
5180+
"""Test the HelpFormatter"""
5181+
5182+
# Test subclassing the help formatter
5183+
class MyFormatter(argparse.HelpFormatter):
5184+
def __init__(self, prog) -> None:
5185+
super().__init__(prog)
5186+
5187+
parser_signature = Sig(
5188+
prog="PROG",
5189+
formatter_class=MyFormatter,
5190+
description="Test with subclassing the help formatter",
5191+
)
5192+
usage = '''\
5193+
usage: PROG [-h]
5194+
'''
5195+
help = usage + '''\
5196+
5197+
Test with subclassing the help formatter
5198+
5199+
options:
5200+
-h, --help show this help message and exit
5201+
'''
5202+
51795203
class TestHelpRawText(HelpTestCase):
51805204
"""Test the RawTextHelpFormatter"""
51815205

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix subclassing :meth:`argparse.HelpFormatter`. Patch by Hugo van Kemenade.

0 commit comments

Comments
 (0)