Skip to content

Commit 7a2854e

Browse files
miss-islingtonhansthensavannahostrowski
authored
[3.14] gh-132558: Improve argparse docs on combining type and choices (GH-133827) (#139057)
gh-132558: Improve `argparse` docs on combining `type` and `choices` (GH-133827) (cherry picked from commit dd0840b) Co-authored-by: Hans Then <[email protected]> Co-authored-by: Savannah Bailey <[email protected]>
1 parent 9d0ca23 commit 7a2854e

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Doc/library/argparse.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,16 +1142,21 @@ if the argument was not one of the acceptable values::
11421142
game.py: error: argument move: invalid choice: 'fire' (choose from 'rock',
11431143
'paper', 'scissors')
11441144

1145-
Note that inclusion in the *choices* sequence is checked after any type_
1146-
conversions have been performed, so the type of the objects in the *choices*
1147-
sequence should match the type_ specified.
1148-
11491145
Any sequence can be passed as the *choices* value, so :class:`list` objects,
11501146
:class:`tuple` objects, and custom sequences are all supported.
11511147

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

1151+
Note that *choices* are checked after any type_
1152+
conversions have been performed, so objects in *choices*
1153+
should match the type_ specified. This can make *choices*
1154+
appear unfamiliar in usage, help, or error messages.
1155+
1156+
To keep *choices* user-friendly, consider a custom type wrapper that
1157+
converts and formats values, or omit type_ and handle conversion in
1158+
your application code.
1159+
11551160
Formatted choices override the default *metavar* which is normally derived
11561161
from *dest*. This is usually what you want because the user never sees the
11571162
*dest* parameter. If this display isn't desirable (perhaps because there are

0 commit comments

Comments
 (0)