lebrice / SimpleParsing

Simple, Elegant, Typed Argument Parsing with argparse
MIT License
401 stars 50 forks source link

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

Open nacitar opened 2 years ago

nacitar commented 2 years ago

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):

lebrice commented 2 years ago

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:

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.