Skip to content

Conversation

@serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Dec 7, 2025

Support groups preceded by positional arguments or followed or intermixed with other optional arguments. Support empty groups.

…n argparse

Support groups preceded by positional arguments or followed or intermixed
with other optional arguments. Support empty groups.
Copy link
Member Author

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

The code of _get_actions_usage_parts() was completely rewritten, besides formatting and colorizing separate arguments. It now uses completely different algorithm.

It does not support groups with multiple positional arguments -- only the first of them is shown included in the group. I am not sure that it is worth to spent effort on supporting the case which AFAIK does not work anyway. We can do this if we will get bug reports.

group = parser.add_mutually_exclusive_group()
with self.assertRaises(ValueError):
parser.parse_args(['-h'])
self.assertEqual(parser.format_usage(), 'usage: PROG [-h]\n')
Copy link
Member Author

Choose a reason for hiding this comment

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

ValueError was only raised to hide IndexError. Now this is not needed, empty groups are simply ignored (as well as groups with all suppressed arguments).

Comment on lines +3678 to +3688
('-a', NS(a=True, b=False, c=False, x=False, y=False, z=False)),
('-b', NS(a=False, b=True, c=False, x=False, y=False, z=False)),
('-c', NS(a=False, b=False, c=True, x=False, y=False, z=False)),
('-a -x', NS(a=True, b=False, c=False, x=True, y=False, z=False)),
('-y -b', NS(a=False, b=True, c=False, x=False, y=True, z=False)),
('-x -y -c', NS(a=False, b=False, c=True, x=True, y=True, z=False)),
]
successes_when_not_required = [
('', NS(a=False, b=False, c=False, x=False, y=False)),
('-x', NS(a=False, b=False, c=False, x=True, y=False)),
('-y', NS(a=False, b=False, c=False, x=False, y=True)),
('', NS(a=False, b=False, c=False, x=False, y=False, z=False)),
('-x', NS(a=False, b=False, c=False, x=True, y=False, z=False)),
('-y', NS(a=False, b=False, c=False, x=False, y=True, z=False)),
Copy link
Member Author

Choose a reason for hiding this comment

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

All these changes are not important. They are just the result of adding yet one argument -z (to increase coverage), no other changes.

Comment on lines +3691 to +3695
usage_when_not_required = '''\
usage: PROG [-h] [-x] [-a | -b | -c] [-y] [-z]
'''
usage_when_required = '''\
usage: PROG [-h] [-x] (-a | -b | -c) [-y] [-z]
Copy link
Member Author

Choose a reason for hiding this comment

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

The old result was incorrect (group was not formatted).

Comment on lines +3765 to +3769
('X A', NS(a='A', b=False, c=False, x='X', y=False, z=False)),
('X -b', NS(a=None, b=True, c=False, x='X', y=False, z=False)),
('X -c', NS(a=None, b=False, c=True, x='X', y=False, z=False)),
('X A -y', NS(a='A', b=False, c=False, x='X', y=True, z=False)),
('X -y -b', NS(a=None, b=True, c=False, x='X', y=True, z=False)),
Copy link
Member Author

Choose a reason for hiding this comment

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

Should be ignored as above.

Comment on lines +3776 to +3780
usage_when_not_required = '''\
usage: PROG [-h] [-y] [-z] x [-b | -c | a]
'''
usage_when_required = '''\
usage: PROG [-h] [-y] [-z] x (-b | -c | a)
Copy link
Member Author

Choose a reason for hiding this comment

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

Yest one incorrect result in old tests.

Comment on lines +5001 to +5003
usage: PROG [-h]
[-v | -q | -x [EXTRA_LONG_OPTION_NAME] |
-y [YET_ANOTHER_LONG_OPTION] | positional]
Copy link
Member Author

Choose a reason for hiding this comment

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

The line break is now forced not before the first positional argument, but before the first group containing a positional argument.

Copy link
Member

@savannahostrowski savannahostrowski left a comment

Choose a reason for hiding this comment

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

LGTM. Verified the new behavior against various edge cases (empty groups, non-contiguous members, intermixed optionals/positionals) - all work as expected! Thanks Serhiy, this makes the algorithm easier to follow.

@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Dec 7, 2025
@serhiy-storchaka serhiy-storchaka merged commit 1db9f56 into python:main Dec 7, 2025
56 checks passed
@miss-islington-app
Copy link

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

@serhiy-storchaka serhiy-storchaka deleted the argparse-help-mutually-exclusive-group branch December 7, 2025 19:36
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 7, 2025
…n argparse (pythonGH-142381)

Support groups preceded by positional arguments or followed or intermixed
with other optional arguments. Support empty groups.
(cherry picked from commit 1db9f56)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@miss-islington-app
Copy link

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 1db9f56bff5bbb0292b131ea8a928612acb7ec16 3.13

@bedevere-app
Copy link

bedevere-app bot commented Dec 7, 2025

GH-142385 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Dec 7, 2025
@savannahostrowski
Copy link
Member

savannahostrowski commented Dec 7, 2025

Wondering if backporting is worth the risk - this changes user-visible output which could break tests.

@serhiy-storchaka serhiy-storchaka removed the needs backport to 3.13 bugs and security fixes label Dec 7, 2025
serhiy-storchaka added a commit that referenced this pull request Dec 7, 2025
…in argparse (GH-142381) (GH-142385)

Support groups preceded by positional arguments or followed or intermixed
with other optional arguments. Support empty groups.
(cherry picked from commit 1db9f56)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@serhiy-storchaka
Copy link
Member Author

This algorithm does not support nested mutually exclusive groups (removed in 3.14), so it cannot be backported to 3.13.

It changes the output in the same way as previous bugfixes. For example, it changes the position of the forced line break for a group containing positional argument, but before #142312 the output for such group was incorrect. If the output was correct, it should not be changed.

@bedevere-bot
Copy link

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot s390x Fedora Stable LTO + PGO 3.x (tier-3) has failed when building commit 1db9f56.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1627/builds/1686) and take a look at the build logs.
  4. Check if the failure is related to this commit (1db9f56) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/1627/builds/1686

Failed tests:

  • test_profiling

Failed subtests:

  • test_sampling_with_collapsed_export - test.test_profiling.test_sampling_profiler.test_integration.TestSampleProfilerIntegration.test_sampling_with_collapsed_export

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.cstratak-fedora-stable-s390x.lto-pgo/build/Lib/test/test_profiling/test_sampling_profiler/test_integration.py", line 525, in test_sampling_with_collapsed_export
    self.assertIn(":", part)
    ~~~~~~~~~~~~~^^^^^^^^^^^
AssertionError: ':' not found in '<GC>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants