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

[WIP] Use store_true for bool args #1646

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pytorch_lightning/trainer/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,16 @@ def add_argparse_args(cls, parent_parser: ArgumentParser) -> ArgumentParser:
def allowed_type(x):
return bool(parsing.strtobool(x))

# Bool args with default of True parsed as flags not key value pair
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But don't we add the argument twice to the parser in such a case? One time here, and another one there:
https://github.com/PyTorchLightning/pytorch-lightning/blob/8614074dc3d97ec5b9b3296b297010f60e36e2cb/pytorch_lightning/trainer/trainer.py#L630-L635

I think it could be handled like so:

(the code is non-working sketch)

if arg_types == (bool,) and arg_default is False:
    action = 'store_true'
    default = None
    arg_type = None
else:
   action = None
   default = arg_default
   arg_type = allowed_type
...
parser.add_argument(
    f'--{arg}',
    default=default,
    type=arg_type,
    dest=arg,
    action=action,
    type = type
    help='autogenerated by pl.Trainer'
)
...

So, I think, it could be generalized a little

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this is the way I did it originally but ended up just goin with the one I pushed. I'll change it back to something like this. Thank you!

Copy link
Contributor

@williamFalcon williamFalcon Apr 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i removed parsing.strtobool(x) on purpose... this fails on some python versions.

If we're going to use strtobool please keep the implementation i added (which is copy paste from strtobool but that works on any python version

if arg_types == (bool,) and arg_default is False:
parser.add_argument(
f'--{arg}',
action='store_true',
dest=arg,
help='autogenerated by pl.Trainer'
)
continue

if arg == 'gpus':
allowed_type = Trainer.allowed_type
arg_default = Trainer.arg_default
Expand Down