argparse_dataclass
argparse_dataclass copied to clipboard
Default help formatter is not working
example code: from dataclasses import dataclass, field from argparse_dataclass import ArgumentParser from argparse import ArgumentDefaultsHelpFormatter
@dataclass
class FineParams:
batch_size: int = field(default=300)
parser = ArgumentParser(GCPFineParams, formatter_class=ArgumentDefaultsHelpFormatter)
params: FineParams = parser.parse_args()
resulting non-defaults help printout.
diving into argparse code (argparse.py, ver 1.1, line 1861) shone that the ArgumentParser's _actions class members, batch_size action is stored as _StoreAction(option_strings=['--batch-size'], dest='batch_size', nargs=None, const=None, default=<dataclasses._MISSING_TYPE object at 0x7f8465889960>, type=<class 'int'>, choices=None, required=False, help=None, metavar=None)
I also have same problem, and I want to use default value on help message, too.
Cause
As far as I understand argparse_dataclass explicitly pass MISSING for non-required argument;
https://github.com/mivade/argparse_dataclass/blob/540640f3fe300b474732e486636d143187496c9b/argparse_dataclass.py#L393
This was introduced by this commit to avoid calling default_factory() unnecessarily.
(Possible) Solution
I think we can still pass default to ArgumentParser, and keep to use MISSING for default_factory() case.
Fortunately, only one of the default and default_factory can be specified (ref), I think we can set kwargs["default"] = field.default unconditionally.
What do you think?