From f452b05c04aef865bffa26ea7a21310ed5a17383 Mon Sep 17 00:00:00 2001 From: mayuriphad Date: Sat, 3 Oct 2026 11:22:42 +0530 Subject: [PATCH] Don't show '(default: None)' for required arguments without a default SimpleHelpFormatter inherits argparse's ArgumentDefaultsHelpFormatter, which appends '(default: None)' to every option. A required field with no default therefore claimed a default it does not have. Skip the default suffix for required actions whose default is None. Optional arguments are unchanged. Updates test_issue_48, which pinned the old output for two required fields, and adds a regression test. Fixes #96 --- simple_parsing/help_formatter.py | 7 ++++++- test/test_help_required_default.py | 19 +++++++++++++++++++ test/test_issue_48.py | 4 ++-- 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 test/test_help_required_default.py diff --git a/simple_parsing/help_formatter.py b/simple_parsing/help_formatter.py index 5e5e3811..2dcdb290 100644 --- a/simple_parsing/help_formatter.py +++ b/simple_parsing/help_formatter.py @@ -79,7 +79,12 @@ def _get_metavar_for_type(self, t: type) -> str: return get_metavar(t) or str(t) def _get_help_string(self, action: Action) -> Optional[str]: - help = super()._get_help_string(action=action) + if action.required and action.default is None: + # A required argument with no default has no default to show. argparse's + # ArgumentDefaultsHelpFormatter would still append "(default: None)". + help = argparse.HelpFormatter._get_help_string(self, action=action) + else: + help = super()._get_help_string(action=action) if help is not None: help = help.replace(TEMPORARY_TOKEN, "") return help diff --git a/test/test_help_required_default.py b/test/test_help_required_default.py new file mode 100644 index 00000000..13b4851e --- /dev/null +++ b/test/test_help_required_default.py @@ -0,0 +1,19 @@ +from dataclasses import dataclass + +from simple_parsing import ArgumentParser + + +@dataclass +class Options: + list_items: list[str] # SOMETHING + + +def test_required_field_without_default_does_not_show_default_none(capsys): + parser = ArgumentParser(add_option_string_dash_variants=True) + parser.add_arguments(Options, dest="options") + + parser.print_help() + help_text = capsys.readouterr().out + + assert "SOMETHING" in help_text + assert "(default: None)" not in help_text diff --git a/test/test_issue_48.py b/test/test_issue_48.py index 904e9996..f164b39b 100644 --- a/test/test_issue_48.py +++ b/test/test_issue_48.py @@ -39,11 +39,11 @@ def test_issue_48(): -s str, --start_date str Start date from which to collect data about base users. Input in iso format (YYYY-MM-DD). The date is - included in the data (default: None) + included in the data -e str, --end_date str End date for collecting base users. Input in iso format (YYYY-MM-DD). The date is included in the data. - Should not be before `start_date` (default: None) + Should not be before `start_date` """ ), )