[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 80d0a85d96)

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>
This commit is contained in:
Miss Islington (bot) 2026-04-03 21:10:19 +02:00 committed by GitHub
parent 594b5a05dc
commit f4c9bc899b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 1 deletions

View file

@ -1111,7 +1111,15 @@ User defined functions can be used as well:
The :func:`bool` function is not recommended as a type converter. All it does
is convert empty strings to ``False`` and non-empty strings to ``True``.
This is usually not what is desired.
This is usually not what is desired::
>>> parser = argparse.ArgumentParser()
>>> _ = parser.add_argument('--verbose', type=bool)
>>> parser.parse_args(['--verbose', 'False'])
Namespace(verbose=True)
See :class:`BooleanOptionalAction` or ``action='store_true'`` for common
alternatives.
In general, the ``type`` keyword is a convenience that should only be used for
simple conversions that can only raise one of the three supported exceptions.

View file

@ -0,0 +1,2 @@
Expand :mod:`argparse` documentation for ``type=bool`` with a demonstration
of the surprising behavior and pointers to common alternatives.