-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
Description
Bug report
Bug description:
import argparse
parser1 = argparse.ArgumentParser()
parser1.add_argument("--squashfs-only", action="store_const", const="squashfs", dest="rootfs_type")
parser1.add_argument("--rootfs-type", metavar="ROOTFSTYPE", default="squashfs")
print(parser1.parse_args())
parser2 = argparse.ArgumentParser()
parser2.add_argument("--rootfs-type", metavar="ROOTFSTYPE", default="squashfs")
parser2.add_argument("--squashfs-only", action="store_const", const="squashfs", dest="rootfs_type")
print(parser2.parse_args())Note the script adds the same two arguments to each parser, but in both possible orders. Output for me, with Python 3.11 and Python 3.13:
Namespace(rootfs_type=None)
Namespace(rootfs_type='squashfs')
AFAICS nothing in argparse docs indicates what the intended behavior in this case is, and it seems surprising and possibly not optimal that the actual behavior differs depending on what order the args are specified in.
This is a real-world issue - because of this, weldr/lorax@01dd27a broke lorax (the Fedora Linux distribution's script for building installer images). In that case, the expected behavior was that the value should be "squashfs" if neither arg was specified on the command line, but because the args were added in the parser1 order (--squashfs-only first), it is actually None.
CPython versions tested on:
3.11, 3.13
Operating systems tested on:
Linux
Metadata
Metadata
Assignees
Labels
Projects
Status