{"record":{"id":"73d38e4c8819ade0","repo":"matplotlib/matplotlib","slug":"cannot-convert-b-r-to-bool","errorCode":null,"errorMessage":"Cannot convert {b!r} to bool","messagePattern":"Cannot convert (.+?) to bool","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/matplotlib/rcsetup.py","lineNumber":188,"sourceCode":"def _validate_date(s):\n    try:\n        np.datetime64(s)\n        return s\n    except ValueError:\n        raise ValueError(\n            f'{s!r} should be a string that can be parsed by numpy.datetime64')\n\n\ndef validate_bool(b):\n    \"\"\"Convert b to ``bool`` or raise.\"\"\"\n    if isinstance(b, str):\n        b = b.lower()\n    if b in ('t', 'y', 'yes', 'on', 'true', '1', 1, True):\n        return True\n    elif b in ('f', 'n', 'no', 'off', 'false', '0', 0, False):\n        return False\n    else:\n        raise ValueError(f'Cannot convert {b!r} to bool')\n\n\ndef validate_axisbelow(s):\n    try:\n        return validate_bool(s)\n    except ValueError:\n        if isinstance(s, str):\n            if s == 'line':\n                return 'line'\n    raise ValueError(f'{s!r} cannot be interpreted as'\n                     ' True, False, or \"line\"')\n\n\ndef validate_dpi(s):\n    \"\"\"Confirm s is string 'figure' or convert s to float or raise.\"\"\"\n    if s == 'figure':\n        return s\n    try:","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/matplotlib/matplotlib/blob/b379c1b69e012b142c0f496a52bcb30513802d72/lib/matplotlib/rcsetup.py#L170-L206","documentation":"validate_bool converts rcParams boolean values. Accepted truthy: 't', 'y', 'yes', 'on', 'true', '1' (case-insensitive via .lower()), int 1, True. Accepted falsy: 'f', 'n', 'no', 'off', 'false', '0', int 0, False. Everything else — other words, ints like 2, floats like 1.0 — raises ValueError('Cannot convert {b!r} to bool').","triggerScenarios":"rcParams['savefig.transparent'] = 'enable' or 'enabled'; rcParams['figure.autolayout'] = 1.0 (float, not accepted); passing 2 or '2'; strings with stray spaces like ' true' (no stripping is done).","commonSituations":"Descriptive on/off words from other tools' configs; numeric booleans from JSON/YAML that arrive as floats or non-0/1 ints; trailing whitespace in hand-edited matplotlibrc values.","solutions":["Use one of the accepted literals: true/false, yes/no, on/off, 1/0, t/f, y/n","In code prefer real Python bools: rcParams['savefig.transparent'] = True","Normalize loaded config values: map truthy words/numbers to Python bool before assignment"],"exampleFix":"# before\nplt.rcParams['figure.autolayout'] = 'enable'  # ValueError\n\n# after\nplt.rcParams['figure.autolayout'] = True  # or 'true', 'yes', 'on', '1'","handlingStrategy":"validation","validationCode":"TRUTHY = {'t', 'y', 'yes', 'on', 'true', '1', 1, True}\nFALSY = {'f', 'n', 'no', 'off', 'false', '0', 0, False}\nvalue = value.lower().strip() if isinstance(value, str) else value\nif value not in TRUTHY and value not in FALSY:\n    raise ValueError(f'unrecognized boolean {value!r}')\nplt.rcParams['figure.autolayout'] = value","typeGuard":null,"tryCatchPattern":"try:\n    plt.rcParams[key] = raw\nexcept ValueError as e:\n    if 'to bool' in str(e):\n        plt.rcParams[key] = str(raw).strip().lower() in ('1', 'true', 'yes', 'on', 't', 'y')\n    else:\n        raise","preventionTips":["Pass Python bools in code instead of strings","Normalize external config booleans (YAML 'no' quirks, 'True'/'FALSE' variants) before touching rcParams","Remember floats like 1.0 and ints other than 0/1 are rejected"],"tags":["matplotlib","rcparams","boolean-parsing","config-validation"],"backgroundTag":"invalid-boolean-value","analyzedSha":"b379c1b69e012b142c0f496a52bcb30513802d72","analyzedAt":"2026-08-21T23:31:55.468Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}