Skip to content

Commit f4c9bc8

Browse files
miss-islingtonjoshuaswansonsavannahostrowski
authored
[3.14] gh-126676: Expand argparse docs for type=bool with warning and alternatives (GH-146435) (#148048)
gh-126676: Expand argparse docs for type=bool with warning and alternatives (GH-146435) (cherry picked from commit 80d0a85) Co-authored-by: Joshua Swanson <22283299+joshuaswanson@users.noreply.github.com> Co-authored-by: joshuaswanson <joshuaswanson@users.noreply.github.com> Co-authored-by: Savannah Ostrowski <savannah@python.org>
1 parent 594b5a0 commit f4c9bc8

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Doc/library/argparse.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,15 @@ User defined functions can be used as well:
11111111

11121112
The :func:`bool` function is not recommended as a type converter. All it does
11131113
is convert empty strings to ``False`` and non-empty strings to ``True``.
1114-
This is usually not what is desired.
1114+
This is usually not what is desired::
1115+
1116+
>>> parser = argparse.ArgumentParser()
1117+
>>> _ = parser.add_argument('--verbose', type=bool)
1118+
>>> parser.parse_args(['--verbose', 'False'])
1119+
Namespace(verbose=True)
1120+
1121+
See :class:`BooleanOptionalAction` or ``action='store_true'`` for common
1122+
alternatives.
11151123

11161124
In general, the ``type`` keyword is a convenience that should only be used for
11171125
simple conversions that can only raise one of the three supported exceptions.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Expand :mod:`argparse` documentation for ``type=bool`` with a demonstration
2+
of the surprising behavior and pointers to common alternatives.

0 commit comments

Comments
 (0)