Skip to content

Conversation

@wchargin
Copy link
Contributor

@wchargin wchargin commented Oct 21, 2019

Summary:
With old versions of argparse, any set_defaults on the base parser
will take precedence over any set_defaults on the subparsers. This is
Python bug #9351, fixed in af26c15110b7. Even though
the versions of argparse bundled with Python 2.7 and 3.5 each include
the fix, we can still avoid this pattern for compatibility.

To see if your Python is affected, run:

import argparse
parser = argparse.ArgumentParser()
parser.set_defaults(test="AFFECTED")
parser.add_subparsers().add_parser("sub").set_defaults(test="OK")
print(parser.parse_args(["sub"]).test)

If it prints AFFECTED, then this patch affects your Python.

Googlers, see http://b/143087149 for context.

Test Plan:
Running under an old version of argparse, existing tests fail before
this change and pass after.

wchargin-branch: program-defaults

DO NOT SUBMIT

Will write description and test plan later… got to go… opening PR now to
start CI. :-)

wchargin-branch: program-defaults
@wchargin wchargin requested a review from nfelt October 21, 2019 18:47

with argparse_util.allow_missing_subcommand():
flags = base_parser.parse_args(argv[1:]) # Strip binary name from argv.
if getattr(flags, _SUBCOMMAND_FLAG, None) is None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe worth adding a TODO/note here so we can revert after we drop support for old python?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

wchargin-branch: program-defaults
wchargin-source: 8503cff78bd97157a3714758a65947d766c5b18e
@wchargin wchargin merged commit ff3bd97 into master Oct 21, 2019
@wchargin wchargin deleted the wchargin-program-defaults branch October 21, 2019 20:15
wchargin added a commit to wchargin/tensorboard that referenced this pull request Oct 29, 2019
Summary:
With old versions of `argparse`, any `set_defaults` on the base parser
will take precedence over any `set_defaults` on the subparsers. This is
Python [bug #9351][bpo-9351], fixed in [af26c15110b7][fix]. Even though
the versions of argparse bundled with Python 2.7 and 3.5 each include
the fix, we can still avoid this pattern for compatibility.

To see if your Python is affected, run:

```python
import argparse
parser = argparse.ArgumentParser()
parser.set_defaults(test="AFFECTED")
parser.add_subparsers().add_parser("sub").set_defaults(test="OK")
print(parser.parse_args(["sub"]).test)
```

If it prints `AFFECTED`, then this patch affects your Python.

Googlers, see <http://b/143087149> for context.

[bpo-9351]: https://bugs.python.org/issue9351
[fix]: http://github.com/python/cpython/commit/af26c15110b76195e62a06d17e39176d42c0511c

Test Plan:
Running under an old version of `argparse`, existing tests fail before
this change and pass after.

wchargin-branch: program-defaults
@wchargin wchargin mentioned this pull request Oct 29, 2019
wchargin added a commit that referenced this pull request Oct 29, 2019
Summary:
With old versions of `argparse`, any `set_defaults` on the base parser
will take precedence over any `set_defaults` on the subparsers. This is
Python [bug #9351][bpo-9351], fixed in [af26c15110b7][fix]. Even though
the versions of argparse bundled with Python 2.7 and 3.5 each include
the fix, we can still avoid this pattern for compatibility.

To see if your Python is affected, run:

```python
import argparse
parser = argparse.ArgumentParser()
parser.set_defaults(test="AFFECTED")
parser.add_subparsers().add_parser("sub").set_defaults(test="OK")
print(parser.parse_args(["sub"]).test)
```

If it prints `AFFECTED`, then this patch affects your Python.

Googlers, see <http://b/143087149> for context.

[bpo-9351]: https://bugs.python.org/issue9351
[fix]: http://github.com/python/cpython/commit/af26c15110b76195e62a06d17e39176d42c0511c

Test Plan:
Running under an old version of `argparse`, existing tests fail before
this change and pass after.

wchargin-branch: program-defaults
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants