python / cpython

The Python programming language
https://www.python.org
Other
63.11k stars 30.22k forks source link

argparse: usage text of arguments in mutually exclusive groups no longer wraps in Python 3.13 #121151

Closed hamdanal closed 3 weeks ago

hamdanal commented 3 months ago

Bug report

Bug description:

Prior to Python 3.13, long usage text of arguments in mutually exclusive groups would wrap to multiple lines. This is no longer the case in Python 3.13:

import argparse

parser = argparse.ArgumentParser(
    prog="PROG", formatter_class=lambda prog: argparse.HelpFormatter(prog, width=80)
)
meg = parser.add_mutually_exclusive_group()
meg.add_argument("--op1", metavar="MET", nargs="?")
meg.add_argument("--op2", metavar=("MET1", "MET2"), nargs="*")
meg.add_argument("--op3", nargs="*")
meg.add_argument("--op4", metavar=("MET1", "MET2"), nargs="+")
meg.add_argument("--op5", nargs="+")
meg.add_argument("--op6", nargs=3)
meg.add_argument("--op7", metavar=("MET1", "MET2", "MET3"), nargs=3)
parser.print_help()

Python 3.12 output:

usage: PROG [-h] [--op1 [MET] | --op2 [MET1 [MET2 ...]] | --op3 [OP3 ...] |
            --op4 MET1 [MET2 ...] | --op5 OP5 [OP5 ...] | --op6 OP6 OP6 OP6 |
            --op7 MET1 MET2 MET3]

Python 3.13 output:

usage: PROG [-h]
            [--op1 [MET] | --op2 [MET1 [MET2 ...]] | --op3 [OP3 ...] | --op4 MET1 [MET2 ...] | --op5 OP5 [OP5 ...] | --op6 OP6 OP6 OP6 | --op7 MET1 MET2 MET3]

This is a regression I introduced in #105039. I am working on a fix for this.

/cc @encukou (sorry for the regression!)

CPython versions tested on:

3.12, 3.13

Operating systems tested on:

Linux

Linked PRs

savannahostrowski commented 3 weeks ago

Looks like this issue should have been closed with the merging of the above PRs.