bool('False') is True
bool('False') is True in Python means bool() on a string only checks whether it is empty. Every non-empty string, including "False", "0" and "no", is True.
How do I fix bool('False') is True?
In Python, bool("False") is True because bool() on a string only checks whether it is empty. Every non-empty string, including "False", "0" and "no", is truthy. Compare the text instead: value.strip().lower() in ("1", "true", "yes"). This matters for environment variables, config files and form fields.
- 1. Find bool() on textSearch for bool(os.environ[...]), bool(config[...]) or a bare if on a form field value.
- 2. Normalise then compareUse strip() and lower(), then check membership in an explicit tuple of accepted words.
- 3. Reject unknown valuesRaise an error for text that is neither a true nor a false word, instead of treating it as True.
What does bool('False') is True mean?
Truthiness is about emptiness, not meaning: "" is falsy and any other string is truthy. Config files, environment variables, query strings and form fields all deliver text, so a setting written as false is still on.
Why does bool('False') is True happen?
1. Parsing a config value with bool()
The word is never read.
Breaks
debug = bool(os.environ["DEBUG"]) # "false" -> TrueWorks
debug = os.environ["DEBUG"].strip().lower() in ("1", "true", "yes")2. Checking a form checkbox value
Unchecked boxes often send "off" or "0".
Breaks
if form["subscribe"]:Works
if form.get("subscribe") == "on":How to find it in your own code
Normalise the text (strip, lower) and compare against the exact words you accept. Treat anything unexpected as an error rather than silently true.
Try it: fix this bug
easyparse_flag("False") returns True.
def parse_flag(text):
return bool(text)Still not sure why yours breaks?
Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.