-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-86463: Fix default prog in subparsers if usage is used in the main parser #125891
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
gh-86463: Fix default prog in subparsers if usage is used in the main parser #125891
Conversation
…e main parser The usage parameter of argparse.ArgumentParser no longer affects the default value of the prog parameter in subparsers. Previously the full custom usage of the main parser was used as the prog prefix in subparsers.
Hmm, I'm a bit worried that this could be a pretty significant breaking change for folks relying on the current behaviour where |
I have found less than a hundred of projects that use I had some doubts, but now my confidence grew. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for sharing that GitHub search! At a cursory glance, I don't see that pattern being relied upon today, but there are thousands of results, so it's hard to tell.
That said, another thought I had if we wanted to be conservative here is to not backport the changes to 3.12 and 3.13. That way, it's perhaps even less risky, and we can more deeply communicate the behaviour change as an improvement in the release notes/what's new?
I considered this as a bug fix, but if you insist, we may cancel backporting. |
I think it could be one of those cases where the bug has become expected behaviour. I think we can just denote that this is a behaviour change in 3.14. |
Co-authored-by: Savannah Ostrowski <savannahostrowski@gmail.com>
I spent some more time playing around with this and I actually don't know that we should do this. Here's a concrete example to showcase why:
Before this change, I could see the exact command that was run as part of the usage string without any additional intervention:
But after, the output changes to this, which doesn't seem right:
The comment above the changed line explicitly states that this is intentionally passed down so it seems like this was designed behavior and not a bug. The original issue seems more of a formatting issue, so it looks more correct to me to handle it from that angle rather than removing this behavior. Even if I pass |
Yes, you can specify a custom usage message which does not broke subparsers. But this is not the case in a vast majority of cases. In 95% or more cases the custom usage message breaks the usage message and error messages in subparsers. Clearly there is an issue and we should fix it. I initially planned to backport this change and then add a warning if the user specify a custom usage message for the main parser but does not specify 'prog' or 'usage' for subparsers. But then I decided that changing the default behavior together with adding a suggestion in the documentation can be enough. Changes in the documentation alone will not be enough. The comment you refer to states: "prog defaults to the usage message of this parser, skipping optional arguments and with no "usage:" prefix". But the code cannot skip optional arguments if a custom usage message is used, because a custom usage message replaces the generated usage message in which you can skip optional arguments. Clearly the comment did not mean such case. It is also does not contain all details -- more precisely it should be "...at the moment, not including subparser commands and the following positional arguments". But the custom usage message usually (in vast majority of cases) contains the full line, including subparser commands and options. This is the issue. This is why this code does not work for custom usage message. |
I don't disagree with the necessity of fixing the bug. It's definitely a real issue. I just don't know that I agree that the right fix should be to stop supporting this propagation of the custom usage message down into the subparser. It seems like we should probably try to fix the formatting issues in the usage message or in error messages. |
This is the way to fix these issues. |
I still disagree, but I'll trust you on this one if you feel strongly! You've already addressed my concerns about not backporting the change, I don't want to block this any further. 😊 |
When a custom usage message is specified for the main parser, you may also want to | ||
consider passing the ``prog`` argument to :meth:`~ArgumentParser.add_subparsers` | ||
or the ``prog`` and the ``usage`` arguments to | ||
:meth:`~_SubParsersAction.add_parser`, to ensure consistent command prefixes and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was merged with a failing lint check on the CI.
https://github.com/python/cpython/actions/runs/11701457525/job/32587601941
Please could you trim the trailing space here?
-:meth:`~_SubParsersAction.add_parser`, to ensure consistent command prefixes and
+:meth:`~_SubParsersAction.add_parser`, to ensure consistent command prefixes and
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will not be available in the next 2 hours.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…e main parser (pythonGH-125891) The usage parameter of argparse.ArgumentParser no longer affects the default value of the prog parameter in subparsers. Previously the full custom usage of the main parser was used as the prog prefix in subparsers.
The usage parameter of argparse.ArgumentParser no longer affects the default value of the prog parameter in subparsers.
Previously the full custom usage of the main parser was used as the prog prefix in subparsers.
📚 Documentation preview 📚: https://cpython-previews--125891.org.readthedocs.build/