Skip to content

Commit a167365

Browse files
[3.10] gh-92446: Improve argparse choices docs (GH-94627) (#100529)
Based on the definition of the collections.abc classes, it is more accurate to use "sequence" instead of "container" when describing argparse choices. (cherry picked from commit ad3c99e) Co-authored-by: Guy Yagev <yourlefthandman8@gmail.com> Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
1 parent 9975d4e commit a167365

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Doc/library/argparse.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ The add_argument() method
702702

703703
* type_ - The type to which the command-line argument should be converted.
704704

705-
* choices_ - A container of the allowable values for the argument.
705+
* choices_ - A sequence of the allowable values for the argument.
706706

707707
* required_ - Whether or not the command-line option may be omitted
708708
(optionals only).
@@ -1124,7 +1124,7 @@ choices
11241124
^^^^^^^
11251125

11261126
Some command-line arguments should be selected from a restricted set of values.
1127-
These can be handled by passing a container object as the *choices* keyword
1127+
These can be handled by passing a sequence object as the *choices* keyword
11281128
argument to :meth:`~ArgumentParser.add_argument`. When the command line is
11291129
parsed, argument values will be checked, and an error message will be displayed
11301130
if the argument was not one of the acceptable values::
@@ -1138,9 +1138,9 @@ if the argument was not one of the acceptable values::
11381138
game.py: error: argument move: invalid choice: 'fire' (choose from 'rock',
11391139
'paper', 'scissors')
11401140

1141-
Note that inclusion in the *choices* container is checked after any type_
1141+
Note that inclusion in the *choices* sequence is checked after any type_
11421142
conversions have been performed, so the type of the objects in the *choices*
1143-
container should match the type_ specified::
1143+
sequence should match the type_ specified::
11441144

11451145
>>> parser = argparse.ArgumentParser(prog='doors.py')
11461146
>>> parser.add_argument('door', type=int, choices=range(1, 4))
@@ -1150,8 +1150,8 @@ container should match the type_ specified::
11501150
usage: doors.py [-h] {1,2,3}
11511151
doors.py: error: argument door: invalid choice: 4 (choose from 1, 2, 3)
11521152

1153-
Any container can be passed as the *choices* value, so :class:`list` objects,
1154-
:class:`set` objects, and custom containers are all supported.
1153+
Any sequence can be passed as the *choices* value, so :class:`list` objects,
1154+
:class:`tuple` objects, and custom sequences are all supported.
11551155

11561156
Use of :class:`enum.Enum` is not recommended because it is difficult to
11571157
control its appearance in usage, help, and error messages.

0 commit comments

Comments
 (0)