Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default help formatter is not working #32

Open
assafge opened this issue Aug 28, 2022 · 1 comment
Open

Default help formatter is not working #32

assafge opened this issue Aug 28, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@assafge
Copy link

assafge commented Aug 28, 2022

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)

@ymd-h
Copy link

ymd-h commented Jun 7, 2023

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;

kwargs["default"] = MISSING

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?

@mivade mivade added the bug Something isn't working label Jun 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants