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

Fix argparse default value bug #2526

Merged
merged 2 commits into from
Jul 9, 2020

Conversation

EspenHa
Copy link
Contributor

@EspenHa EspenHa commented Jul 6, 2020

What does this PR do?

Fixes #1916.

Essentially this PR will make parse_argparser properly do the inverse of what add_argparse_args does.

The reason for the issue is that PL auto-generates an argparser that accepts flag style options, i.e. --auto_lr_find leads to args.auto_lr_find is True. The only way to provide a negative flag (for options that default to True) is to use --auto_lr_find False. Because some parameters (like auto_lr_find) can be both a boolean and a string, the generated parser for these arguments uses nargs="?", which means that the default value becomes None, regardless of the default value in __init__. This means that for the flag-style options, parse_argparser needs to convert some of the None values to booleans.

Not sure if I should format with black or not, as that would create a large diff that is unrelated to the changes needed for the issue.

Also I think the argparse solution is not very robust. This PR simply fixes the immediate issue, but I think it is quite likely that something similar could happen again.

Before submitting

  • Was this discussed/approved via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together? Otherwise, we ask you to create a separate PR for every change.
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?
  • Did you verify new and existing tests pass locally with your changes?
  • If you made a notable change (that affects users), did you update the CHANGELOG?

PR review

Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.

Did you have fun?

Make sure you had fun coding 🙃

@mergify mergify bot requested a review from a team July 6, 2020 09:49
@codecov
Copy link

codecov bot commented Jul 6, 2020

Codecov Report

Merging #2526 into master will increase coverage by 1%.
The diff coverage is 100%.

@@          Coverage Diff           @@
##           master   #2526   +/-   ##
======================================
+ Coverage      88%     89%   +1%     
======================================
  Files          69      69           
  Lines        5628    5635    +7     
======================================
+ Hits         4963    5023   +60     
+ Misses        665     612   -53     

Comment on lines +806 to +809
if k in types_default and v is None:
# We need to figure out if the None is due to using nargs="?" or if it comes from the default value
arg_types, arg_default = types_default[k]
if bool in arg_types and isinstance(arg_default, bool):
Copy link
Member

Choose a reason for hiding this comment

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

it took me awhile to understand it... :]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it gets a bit complicated. Took me a while to understand it as well :P

@Borda Borda added the ready PRs ready to be merged label Jul 8, 2020
@mergify mergify bot requested a review from a team July 8, 2020 15:25
@williamFalcon williamFalcon merged commit b3ebfec into Lightning-AI:master Jul 9, 2020
@EspenHa EspenHa deleted the fix_argparse_bug branch September 30, 2020 11:49
@Borda Borda added this to the 0.10.0 milestone Oct 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Trainer.parse_argparser does not yield sensible default for default_root_dir
3 participants