FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-133653: Fix subclassing `HelpFormatter` by hugovk · Pull Request #133668 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 11 additions & 3 deletions Lib/argparse.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2723,9 +2723,17 @@ def format_help(self):
return formatter.format_help()

def _get_formatter(self):
if isinstance(self.formatter_class, type) and issubclass(
self.formatter_class, HelpFormatter
):
import inspect
if len(
[v.kind for (k, v) in
inspect.signature(self.formatter_class).parameters.items()
if k in ('prefix_chars', 'color')
and v.kind in (
inspect._ParameterKind.POSITIONAL_OR_KEYWORD,
inspect._ParameterKind.KEYWORD_ONLY,
)
]
) == 2:
return self.formatter_class(
prog=self.prog,
prefix_chars=self.prefix_chars,
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_argparse.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix subclassing :meth:`!argparse.HelpFormatter`. Patch by Hugo van Kemenade.

Back | FazBrowse Home | New Git URL