SimpleParsing icon indicating copy to clipboard operation
SimpleParsing copied to clipboard

Specifying help text on a field causes the help to indicate a default of None even if there is no default

Open nacitar opened this issue 3 years ago • 1 comments

Describe the bug When help text is added to a field that does not have a default value, the help text indicates a default value of None when it should not.

To Reproduce

#!/usr/bin/env python3
# test_simple.py
from dataclasses import dataclass
from simple_parsing import ArgumentParser

@dataclass
class Options:
    list_items: list[str]  # SOMETHING

parser = ArgumentParser(add_option_string_dash_variants=True)
parser.add_arguments(Options, dest="options")
args=parser.parse_args()
print(args.options)

Expected behavior I expected no default to be indicated, as below:

$ ./test_simple.py --help
usage: test_simple.py [-h] --list-items list

optional arguments:
  -h, --help            show this help message and exit

Options ['options']:
  Options(list_items: list)

  --list-items list, --list_items list
                        SOMETHING

Actual behavior A default is indicated even though there is no default because the parameter is simply required.

$ ./test_simple.py --help
usage: test_simple.py [-h] --list-items list

optional arguments:
  -h, --help            show this help message and exit

Options ['options']:
  Options(list_items: list)

  --list-items list, --list_items list
                        SOMETHING (default: None)

Desktop (please complete the following information):

  • Python version: 3.9.7

nacitar avatar Oct 23 '21 06:10 nacitar

Hey there @nacitar , sorry for replying earlier.

This is a cute, crunchy little bug. I really like those.

For anyone that wants to help out, this is what I'm thinking:

  • This is most likely due to the help formatter used by default (the default value of the formatter_class argument of simple_parsing.ArgumentParser): the SimpleHelpFormatter.

What I suggest trying:

  1. Try using the original argparse.ArgumentDefaultsHelpFormatter. Check if the problem persists.
    • If the problem doesn't show up, then the bug is inside our help formatter subclass.
  2. If the problem persists, then try using no help formatter at all (passing None as formatter_class I think).
    • If the problem is fixed, then the bug is probably happening inside the help formatter from argparse.
  3. If the problem still persists, then it must be something in the way we create the value of the help argument to add_arguments of argparse, probably in the FieldWrapper in simple_parsing.

lebrice avatar Nov 19 '21 00:11 lebrice