Skip to content

bpo-46101: argparse bug fix when using parents & subcommands #30146

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

Closed
wants to merge 2 commits into from
Closed

bpo-46101: argparse bug fix when using parents & subcommands #30146

wants to merge 2 commits into from

Conversation

Lucas-C
Copy link
Contributor

@Lucas-C Lucas-C commented Dec 16, 2021

Here is some minimal code reproducing the issue:

import argparse

common_opts_parser = argparse.ArgumentParser(add_help=False)
common_opts_parser.add_argument("--endpoint", choices=("prod", "dev"), default="dev")

parser = argparse.ArgumentParser(parents=[common_opts_parser])
subparsers = parser.add_subparsers(required=True)

subcmd_cmd = subparsers.add_parser("subcmd", parents=[common_opts_parser])
subcmd_cmd.add_argument("--debug", action="store_true")

print(parser.parse_args())

Everything works fine / as expected when specifying the common optional arg last:

$ ./bug_repro.py subcmd --endpoint=dev
Namespace(endpoint='dev', debug=False)

However when specifying the --endpoint between the program and the subcommand, the value provided is ignored:

$ ./bug_repro.py --endpoint=dev subcmd
Namespace(endpoint=None, debug=False)

Tested under Windows / Python 3.11.0a0 with:

python -m test -v -m test_common_opts test_argparse

https://bugs.python.org/issue46101

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

Recognized GitHub username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@creich

This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@Lucas-C
Copy link
Contributor Author

Lucas-C commented Dec 16, 2021

Please not that the @the-knights-who-say-ni bot got my username wrong 😉

I have already signed the CLA agreement.

@Lucas-C
Copy link
Contributor Author

Lucas-C commented Dec 16, 2021

This PR is ready for review:
the CI pipeline is passing, and I dot not plan to add commits.

@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Jan 16, 2022
@Lucas-C
Copy link
Contributor Author

Lucas-C commented Feb 10, 2022

Closing as @paul.j3 rejected the Python bug: https://bugs.python.org/issue46101

@Lucas-C Lucas-C closed this Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review stale Stale PR or inactive for long period of time.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants