From 98cebd8753dabb4e43c7305182f78183699926c6 Mon Sep 17 00:00:00 2001 From: Brian Zhang Date: Sat, 21 Nov 2026 17:46:12 -0500 Subject: [PATCH 1/6] feat: add --help-group option (#9452) --- src/_pytest/config/__init__.py | 3 +++ src/_pytest/helpconfig.py | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index f1593fb0336..c3e4dc8928c 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -1799,6 +1799,9 @@ def parse(self, args: list[str], addopts: bool = True) -> None: self._validate_plugins() self._warn_about_skipped_plugins() + if self.known_args_namespace.help_group is not None: + self.option.help_group = self.known_args_namespace.help_group + if self.known_args_namespace.confcutdir is None: if self.inipath is not None: confcutdir = str(self.inipath.parent) diff --git a/src/_pytest/helpconfig.py b/src/_pytest/helpconfig.py index 8d9d44699c1..bbc4c5cb771 100644 --- a/src/_pytest/helpconfig.py +++ b/src/_pytest/helpconfig.py @@ -76,6 +76,13 @@ def pytest_addoption(parser: Parser) -> None: dest="help", help="Show help message and configuration info", ) + group.addoption( # separate help flag that can be subsets + "--help-group", + action="store", + default=None, + dest="help_group", + help="Show help message and configuration info for a specific group of option(s). ", + ) group._addoption( # private to use reserved lower-case short option "-p", action="append", From d844588a8723840fc0a3c2a8894447d268b4bbda Mon Sep 17 00:00:00 2001 From: Brian Zhang Date: Sat, 21 Nov 2026 18:10:03 -0500 Subject: [PATCH 2/6] feat: validate --help-group option (#9452) --- src/_pytest/config/__init__.py | 12 +++++++++--- src/_pytest/helpconfig.py | 30 ++++++++++++++---------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index c3e4dc8928c..ba4cbd05132 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -1799,9 +1799,6 @@ def parse(self, args: list[str], addopts: bool = True) -> None: self._validate_plugins() self._warn_about_skipped_plugins() - if self.known_args_namespace.help_group is not None: - self.option.help_group = self.known_args_namespace.help_group - if self.known_args_namespace.confcutdir is None: if self.inipath is not None: confcutdir = str(self.inipath.parent) @@ -1823,6 +1820,15 @@ def parse(self, args: list[str], addopts: bool = True) -> None: else: raise + if self.known_args_namespace.help_group is not None: + available_groups = [group.name for group in self._parser._groups] + if self.known_args_namespace.help_group not in available_groups: + raise UsageError( + f"unknown help group {self.known_args_namespace.help_group!r}; " + f"available groups: {', '.join(available_groups)}" + ) + self.option.help_group = self.known_args_namespace.help_group + try: self._parser.parse(args, namespace=self.option) except PrintHelp: diff --git a/src/_pytest/helpconfig.py b/src/_pytest/helpconfig.py index bbc4c5cb771..d8efe0abde9 100644 --- a/src/_pytest/helpconfig.py +++ b/src/_pytest/helpconfig.py @@ -22,7 +22,7 @@ class HelpAction(argparse.Action): """An argparse Action that will raise a PrintHelp exception in order to skip - the rest of the argument parsing when --help is passed. + the rest of the argument parsing when a help option is passed. This prevents argparse from raising UsageError when `--help` is used along with missing required arguments when any are defined, for example by @@ -33,17 +33,11 @@ class HelpAction(argparse.Action): `namespace._raise_print_help = True`. Otherwise it just sets the option. """ - def __init__( - self, option_strings: Sequence[str], dest: str, *, help: str | None = None - ) -> None: - super().__init__( - option_strings=option_strings, - dest=dest, - nargs=0, - const=True, - default=False, - help=help, - ) + def __init__(self, option_strings: Sequence[str], dest: str, **kwargs: Any) -> None: + kwargs.setdefault("nargs", 0) + kwargs.setdefault("const", True) + kwargs.setdefault("default", False) + super().__init__(option_strings, dest, **kwargs) def __call__( self, @@ -52,7 +46,9 @@ def __call__( values: str | Sequence[Any] | None, option_string: str | None = None, ) -> None: - setattr(namespace, self.dest, self.const) + value = self.const if self.nargs == 0 else values + setattr(namespace, self.dest, value) + setattr(namespace, "help", True) if getattr(namespace, "_raise_print_help", False): raise PrintHelp @@ -76,12 +72,14 @@ def pytest_addoption(parser: Parser) -> None: dest="help", help="Show help message and configuration info", ) - group.addoption( # separate help flag that can be subsets + group.addoption( "--help-group", - action="store", + action=HelpAction, + nargs=None, default=None, dest="help_group", - help="Show help message and configuration info for a specific group of option(s). ", + metavar="GROUP", + help="Show help for a specific option group.", ) group._addoption( # private to use reserved lower-case short option "-p", From 082845511c5ac3a81c395c13ce4b764b2be2a663 Mon Sep 17 00:00:00 2001 From: Brian Zhang Date: Sat, 21 Nov 2026 18:40:35 -0500 Subject: [PATCH 3/6] fix: preserve HelpAction defaults (#9452) --- src/_pytest/config/__init__.py | 2 +- src/_pytest/helpconfig.py | 29 ++++++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index ba4cbd05132..12d0450a10e 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -1820,7 +1820,7 @@ def parse(self, args: list[str], addopts: bool = True) -> None: else: raise - if self.known_args_namespace.help_group is not None: + if self.known_args_namespace.help_group is not False: available_groups = [group.name for group in self._parser._groups] if self.known_args_namespace.help_group not in available_groups: raise UsageError( diff --git a/src/_pytest/helpconfig.py b/src/_pytest/helpconfig.py index d8efe0abde9..b8e93ca708f 100644 --- a/src/_pytest/helpconfig.py +++ b/src/_pytest/helpconfig.py @@ -33,11 +33,22 @@ class HelpAction(argparse.Action): `namespace._raise_print_help = True`. Otherwise it just sets the option. """ - def __init__(self, option_strings: Sequence[str], dest: str, **kwargs: Any) -> None: - kwargs.setdefault("nargs", 0) - kwargs.setdefault("const", True) - kwargs.setdefault("default", False) - super().__init__(option_strings, dest, **kwargs) + def __init__( + self, + option_strings: Sequence[str], + dest: str, + *, + help: str | None = None, + nargs: int | str | None = 0, + ) -> None: + super().__init__( + option_strings=option_strings, + dest=dest, + nargs=nargs, + const=True, + default=False, + help=help, + ) def __call__( self, @@ -46,8 +57,10 @@ def __call__( values: str | Sequence[Any] | None, option_string: str | None = None, ) -> None: - value = self.const if self.nargs == 0 else values - setattr(namespace, self.dest, value) + if self.nargs == 0: + setattr(namespace, self.dest, self.const) + else: + setattr(namespace, self.dest, values) setattr(namespace, "help", True) if getattr(namespace, "_raise_print_help", False): @@ -76,9 +89,7 @@ def pytest_addoption(parser: Parser) -> None: "--help-group", action=HelpAction, nargs=None, - default=None, dest="help_group", - metavar="GROUP", help="Show help for a specific option group.", ) group._addoption( # private to use reserved lower-case short option From c0ea3a07ef74b8185f5650a15717cbd6a3048fb1 Mon Sep 17 00:00:00 2001 From: dylany3 Date: Mon, 23 Nov 2026 18:44:53 -0500 Subject: [PATCH 4/6] test: add unit and regression tests for --help-group (#9452) --- testing/test_help_group.py | 192 +++++++++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 testing/test_help_group.py diff --git a/testing/test_help_group.py b/testing/test_help_group.py new file mode 100644 index 00000000000..0f7a954904c --- /dev/null +++ b/testing/test_help_group.py @@ -0,0 +1,192 @@ +# mypy: allow-untyped-defs +"""Unit and regression tests for ``--help-group`` (#9452). + +``pytest --help-group=`` shows the help for a single option group, so +projects can see their own options without scrolling through all of +``pytest --help``. +""" + +from __future__ import annotations + +import argparse + +from _pytest.config import ExitCode +from _pytest.helpconfig import HelpAction +from _pytest.pytester import Pytester +import pytest + + +CUSTOM_CONFTEST = """ +def pytest_addoption(parser): + group = parser.getgroup("custom", "custom options") + group.addoption("--foo", help="foo option help") + group.addoption("--bar-thing", action="store_true", help="bar option help") +""" + +FILTERING_PENDING = pytest.mark.xfail( + reason="--help-group output filtering is not implemented yet (#9452)", + strict=True, +) + + +@pytest.fixture +def custom_group(pytester: Pytester) -> None: + pytester.makeconftest(CUSTOM_CONFTEST) + + +# --- HelpAction --------------------------------------------------------------- + + +def test_help_action_without_value_sets_flag() -> None: + """``--help`` takes no value and just turns help on.""" + parser = argparse.ArgumentParser() + parser.add_argument("--help-x", action=HelpAction, dest="help") + namespace = parser.parse_args(["--help-x"]) + assert namespace.help is True + + +def test_help_action_with_value_stores_group_and_turns_on_help() -> None: + """``--help-group`` stores the group name and also turns help on.""" + parser = argparse.ArgumentParser() + parser.add_argument( + "--help-group", action=HelpAction, nargs=None, dest="help_group" + ) + namespace = parser.parse_args(["--help-group", "custom"]) + assert namespace.help_group == "custom" + assert namespace.help is True + + +def test_help_action_defaults_to_false() -> None: + parser = argparse.ArgumentParser() + parser.add_argument( + "--help-group", action=HelpAction, nargs=None, dest="help_group" + ) + assert parser.parse_args([]).help_group is False + + +# --- Normal filtering --------------------------------------------------------- + + +@FILTERING_PENDING +@pytest.mark.parametrize( + "args", [["--help-group=custom"], ["--help", "--help-group=custom"]] +) +def test_help_group_shows_only_that_group( + pytester: Pytester, custom_group: None, args: list[str] +) -> None: + result = pytester.runpytest(*args) + assert result.ret == ExitCode.OK + result.stdout.fnmatch_lines( + [ + "*custom options*", + "*--foo=FOO*foo option help*", + "*--bar-thing*bar option help*", + ] + ) + # Options from other groups are hidden. + result.stdout.no_fnmatch_line("*-k EXPRESSION*") + result.stdout.no_fnmatch_line("*--durations=N*") + result.stdout.no_fnmatch_line("*--version*") + + +@FILTERING_PENDING +def test_help_group_works_for_builtin_group(pytester: Pytester) -> None: + result = pytester.runpytest("--help-group=debugconfig") + assert result.ret == ExitCode.OK + result.stdout.fnmatch_lines(["*-V, --version*"]) + result.stdout.no_fnmatch_line("*-k EXPRESSION*") + + +def test_help_group_does_not_run_tests(pytester: Pytester, custom_group: None) -> None: + pytester.makepyfile("def test_fails(): assert False") + result = pytester.runpytest("--help-group=custom") + assert result.ret == ExitCode.OK + result.stdout.no_fnmatch_line("*1 failed*") + result.stdout.no_fnmatch_line("*test_fails*") + + +def test_help_group_ignores_missing_required_options(pytester: Pytester) -> None: + """Like ``--help``, it works even if a required option wasn't passed.""" + pytester.makeconftest( + """ + def pytest_addoption(parser): + group = parser.getgroup("custom", "custom options") + group.addoption("--needed", required=True, help="needed option help") + """ + ) + result = pytester.runpytest("--help-group=custom") + assert result.ret == ExitCode.OK + result.stdout.fnmatch_lines(["*--needed*needed option help*"]) + + +# --- Invalid commands --------------------------------------------------------- + + +def test_help_group_unknown_name(pytester: Pytester, custom_group: None) -> None: + result = pytester.runpytest("--help-group=nope") + assert result.ret == ExitCode.USAGE_ERROR + result.stderr.fnmatch_lines( + ["*unknown help group 'nope'; available groups:*general*custom*"] + ) + + +def test_help_group_empty_name(pytester: Pytester) -> None: + result = pytester.runpytest("--help-group=") + assert result.ret == ExitCode.USAGE_ERROR + result.stderr.fnmatch_lines(["*unknown help group ''*"]) + + +def test_help_group_missing_value(pytester: Pytester) -> None: + result = pytester.runpytest("--help-group") + assert result.ret == ExitCode.USAGE_ERROR + result.stderr.fnmatch_lines(["*--help-group: expected one argument*"]) + + +def test_help_group_unknown_name_does_not_run_tests(pytester: Pytester) -> None: + pytester.makepyfile("def test_fails(): assert False") + result = pytester.runpytest("--help-group=nope") + assert result.ret == ExitCode.USAGE_ERROR + result.stdout.no_fnmatch_line("*1 failed*") + + +# --- Regression: everything else works like before ------------------------------ + + +@pytest.mark.parametrize("flag", ["--help", "-h"]) +def test_full_help_still_shows_every_section(pytester: Pytester, flag: str) -> None: + result = pytester.runpytest(flag) + assert result.ret == ExitCode.OK + result.stdout.fnmatch_lines( + [ + "*-k EXPRESSION*", + "*--durations=N*", + "*-V, --version*", + "*--help-group=HELP_GROUP*", + "*configuration options*", + "Environment variables:", + "*to see*markers*pytest --markers*", + ] + ) + + +def test_full_help_still_shows_custom_options( + pytester: Pytester, custom_group: None +) -> None: + result = pytester.runpytest("--help") + assert result.ret == ExitCode.OK + result.stdout.fnmatch_lines( + ["*-k EXPRESSION*", "*custom options*", "*--foo=FOO*foo option help*"] + ) + + +def test_normal_run_has_help_options_off(pytester: Pytester) -> None: + pytester.makepyfile( + """ + def test_options(request): + assert request.config.option.help is False + assert request.config.option.help_group is False + """ + ) + result = pytester.runpytest() + assert result.ret == ExitCode.OK + result.assert_outcomes(passed=1) From 8a272d15cd971b5b6f50a12af71f09fcf1262cbd Mon Sep 17 00:00:00 2001 From: Tate Suarez Date: Fri, 4 Dec 2026 13:30:55 -0500 Subject: [PATCH 5/6] feat: filter --help output by option group (#9452) Co-authored-by: Claude Opus 5.5 --- src/_pytest/helpconfig.py | 21 +++++++++++++++++++++ testing/test_help_group.py | 7 ------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/_pytest/helpconfig.py b/src/_pytest/helpconfig.py index b8e93ca708f..92c7f720c6c 100644 --- a/src/_pytest/helpconfig.py +++ b/src/_pytest/helpconfig.py @@ -195,12 +195,33 @@ def pytest_cmdline_main(config: Config) -> int | ExitCode | None: return None +def _format_group_help(config: Config, name: str) -> str: + """Format the --help section for the option group called ``name``. + + The name has already been validated in ``Config._preparse``. + """ + group = next(g for g in config._parser._groups if g.name == name) + arggroup = group._arggroup + formatter = config._parser.optparser._get_formatter() + formatter.start_section(arggroup.title) + formatter.add_text(arggroup.description) + formatter.add_arguments(arggroup._group_actions) + formatter.end_section() + return formatter.format_help() + + def showhelp(config: Config) -> None: reporter: TerminalReporter | None = config.pluginmanager.get_plugin( "terminalreporter" ) assert reporter is not None tw = reporter._tw + + help_group = getattr(config.option, "help_group", False) + if help_group is not False: + tw.write(_format_group_help(config, help_group)) + return + tw.write(config._parser.optparser.format_help()) tw.line() tw.line( diff --git a/testing/test_help_group.py b/testing/test_help_group.py index 0f7a954904c..6d7222afd35 100644 --- a/testing/test_help_group.py +++ b/testing/test_help_group.py @@ -23,11 +23,6 @@ def pytest_addoption(parser): group.addoption("--bar-thing", action="store_true", help="bar option help") """ -FILTERING_PENDING = pytest.mark.xfail( - reason="--help-group output filtering is not implemented yet (#9452)", - strict=True, -) - @pytest.fixture def custom_group(pytester: Pytester) -> None: @@ -67,7 +62,6 @@ def test_help_action_defaults_to_false() -> None: # --- Normal filtering --------------------------------------------------------- -@FILTERING_PENDING @pytest.mark.parametrize( "args", [["--help-group=custom"], ["--help", "--help-group=custom"]] ) @@ -89,7 +83,6 @@ def test_help_group_shows_only_that_group( result.stdout.no_fnmatch_line("*--version*") -@FILTERING_PENDING def test_help_group_works_for_builtin_group(pytester: Pytester) -> None: result = pytester.runpytest("--help-group=debugconfig") assert result.ret == ExitCode.OK From 28d8a945b2b717192078a49dab0884098be28b24 Mon Sep 17 00:00:00 2001 From: Tate Suarez Date: Fri, 4 Dec 2026 13:55:50 -0500 Subject: [PATCH 6/6] docs: add changelog entry and author for --help-group (#9452) Co-authored-by: Claude Opus 5.5 --- AUTHORS | 1 + changelog/9452.feature.rst | 1 + 2 files changed, 2 insertions(+) create mode 100644 changelog/9452.feature.rst diff --git a/AUTHORS b/AUTHORS index 6fd2bec926e..1f30f713dfa 100644 --- a/AUTHORS +++ b/AUTHORS @@ -492,6 +492,7 @@ Tanvi Mehta Tanya Agarwal Tarcisio Fischer Tareq Alayan +Tate Suarez Tatiana Ovary Ted Xiao Terje Runde diff --git a/changelog/9452.feature.rst b/changelog/9452.feature.rst new file mode 100644 index 00000000000..b0c4ac9d7ba --- /dev/null +++ b/changelog/9452.feature.rst @@ -0,0 +1 @@ +Added the ``--help-group=`` command-line option, which shows ``--help`` output for only the given option group, such as one defined by a plugin or a project's ``conftest.py``.