Desk Clarity

Everyday document & browser help

argparse reads False as true: choose a flag or an explicit text converter

By ON Lab · Sources checked November 8, 2026

type=bool applies Python's normal truth-value conversion to an option's text. A nonempty string such as "False" is therefore true. Decide whether your command should accept a presence flag, a positive/negative flag pair or a value such as --enabled false. Then test that exact interface rather than changing the default and hoping the conversion changes.

The historical reader explicitly wanted true/false text and short variants. We checked current argparse guidance and reproduced the relevant behavior with Python 3.9.6. The reader's additional claim about an empty string did not match our direct parse_args test: an actual empty string became false. Preserve that difference instead of repeating every statement in an old question as a verified fact.

Reproduce the conversion before changing the interface

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--enabled", type=bool)
for value in ["False", "false", "0", ""]:
    result = parser.parse_args(["--enabled", value])
    print(repr(value), result.enabled)

In our recorded run, the first three strings became true and the empty string became false. These were exact list values passed to parse_args, so no shell quoting was involved. If a command launched from your shell behaves differently, inspect the actual received arguments before attributing the difference to argparse.

type is a conversion function, not a declaration that the parser should recognize English truth words. Changing default=True to default=False affects an absent option, not the conversion of a supplied nonempty string.

For a presence flag, use the corresponding action

parser.add_argument("--enabled", action="store_true")

This interface means that supplying --enabled selects true. It is not a value-taking option whose next token should be False. The corresponding store_false action supports an option that selects false. Read the parser help and choose a name whose meaning matches the action.

Changing a previously value-taking interface to a flag is an interface change. Update callers, examples and tests together. Do not silently accept an old --enabled False invocation and assume the following word was consumed as the option's value.

For a positive and negative pair

parser.add_argument(
    "--enabled",
    action=argparse.BooleanOptionalAction,
    default=None,
)

BooleanOptionalAction is available from Python 3.9. For this option it provides --enabled and --no-enabled. We tested absence as None, the positive form as true and the negative form as false. The explicit None default lets this example distinguish an unspecified setting from either deliberate choice.

That third state can be useful when a command-line option overrides a configuration file, but argparse does not design the configuration merge for you. Decide which source wins and preserve the distinction through that merge. An older Python runtime without this action needs another documented interface or implementation.

For true/false text, define accepted values

def parse_boolean(value):
    normalized = value.casefold()
    if normalized in ("true", "t"):
        return True
    if normalized in ("false", "f"):
        return False
    raise argparse.ArgumentTypeError("expected true/t or false/f")

parser.add_argument("--enabled", type=parse_boolean)

This small original converter matches the requested text interface and explicitly chooses its vocabulary. It accepts upper- and lower-case versions through casefold. It deliberately does not interpret every nonempty value as true and does not quietly guess what an unfamiliar spelling means.

In our local test, False and f produced false, TRUE produced true, and maybe caused the parser to report the conversion error and exit with status 2. If your interface should also accept yes/no or 1/0, add and test those exact values deliberately. This example does not include them.

Check the absent, supplied and invalid cases

CaseWhat to verify
No option suppliedThe intended default or unspecified state
Positive selectionThe exact supported positive syntax
Negative selectionThe exact supported negative syntax
Invalid valueA clear error rather than a guessed true value

Use separate parser instances when comparing interface designs. A demonstration that adds the same option repeatedly to one parser can fail on an option conflict before it reaches the conversion you meant to test.

If a wrapper or subprocess changes argument boundaries, first use the subprocess argument receiver comparison. Once the intended tokens arrive, this guide addresses their semantic conversion. Correct token boundaries do not make bool("False") false.

Checked November 8, 2026. The type conversion, three-state BooleanOptionalAction and explicit converter tests were run locally. No original application, shell wrapper or configuration merge was tested. Historical reader demand is distinct from measured current search traffic.

Sources