Skip to content

Commit 2cb4b13

Browse files
[3.11] gh-92446: Improve argparse choices docs (GH-94627) (#100528)
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 75b75df commit 2cb4b13

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
@@ -755,7 +755,7 @@ The add_argument() method
755755

756756
* type_ - The type to which the command-line argument should be converted.
757757

758-
* choices_ - A container of the allowable values for the argument.
758+
* choices_ - A sequence of the allowable values for the argument.
759759

760760
* required_ - Whether or not the command-line option may be omitted
761761
(optionals only).
@@ -1199,7 +1199,7 @@ choices
11991199
^^^^^^^
12001200

12011201
Some command-line arguments should be selected from a restricted set of values.
1202-
These can be handled by passing a container object as the *choices* keyword
1202+
These can be handled by passing a sequence object as the *choices* keyword
12031203
argument to :meth:`~ArgumentParser.add_argument`. When the command line is
12041204
parsed, argument values will be checked, and an error message will be displayed
12051205
if the argument was not one of the acceptable values::
@@ -1213,9 +1213,9 @@ if the argument was not one of the acceptable values::
12131213
game.py: error: argument move: invalid choice: 'fire' (choose from 'rock',
12141214
'paper', 'scissors')
12151215

1216-
Note that inclusion in the *choices* container is checked after any type_
1216+
Note that inclusion in the *choices* sequence is checked after any type_
12171217
conversions have been performed, so the type of the objects in the *choices*
1218-
container should match the type_ specified::
1218+
sequence should match the type_ specified::
12191219

12201220
>>> parser = argparse.ArgumentParser(prog='doors.py')
12211221
>>> parser.add_argument('door', type=int, choices=range(1, 4))
@@ -1225,8 +1225,8 @@ container should match the type_ specified::
12251225
usage: doors.py [-h] {1,2,3}
12261226
doors.py: error: argument door: invalid choice: 4 (choose from 1, 2, 3)
12271227

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

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

0 commit comments

Comments
 (0)