From: Joshua Swanson <22283299+joshuaswanson@users.noreply.github.com> Date: Fri, 3 Apr 2026 18:07:03 +0000 (+0200) Subject: gh-126676: Expand argparse docs for type=bool with warning and alternatives (#146435) X-Git-Tag: v3.15.0a8~55 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=80d0a85d969d305c7436dc54f8939d7b6f441b5f;p=thirdparty%2FPython%2Fcpython.git gh-126676: Expand argparse docs for type=bool with warning and alternatives (#146435) Co-authored-by: joshuaswanson Co-authored-by: Savannah Ostrowski --- diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 5a463ee9821d..8ba11b7d12d5 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -1118,7 +1118,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. diff --git a/Misc/NEWS.d/next/Documentation/2026-03-25-00-00-00.gh-issue-126676.052336.rst b/Misc/NEWS.d/next/Documentation/2026-03-25-00-00-00.gh-issue-126676.052336.rst new file mode 100644 index 000000000000..d2e275fdf083 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2026-03-25-00-00-00.gh-issue-126676.052336.rst @@ -0,0 +1,2 @@ +Expand :mod:`argparse` documentation for ``type=bool`` with a demonstration +of the surprising behavior and pointers to common alternatives.