{"id":"ce9d6479e752ad98","repo":"pypa/pip","slug":"invalid-type-string-r-for-option-optname-use","errorCode":null,"errorMessage":"Invalid type {string!r} for option {optname}; use 1/0, yes/no, true/false, on/off","messagePattern":"Invalid type (.+?) for option (.+?); use 1/0, yes/no, true/false, on/off","errorType":"exception","errorClass":"OptionError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/pygments/util.py","lineNumber":71,"sourceCode":"def get_bool_opt(options, optname, default=None):\n    \"\"\"\n    Intuitively, this is `options.get(optname, default)`, but restricted to\n    Boolean value. The Booleans can be represented as string, in order to accept\n    Boolean value from the command line arguments. If the key `optname` is\n    present in the dictionary `options` and is not associated with a Boolean,\n    raise an `OptionError`. If it is absent, `default` is returned instead.\n\n    The valid string values for ``True`` are ``1``, ``yes``, ``true`` and\n    ``on``, the ones for ``False`` are ``0``, ``no``, ``false`` and ``off``\n    (matched case-insensitively).\n    \"\"\"\n    string = options.get(optname, default)\n    if isinstance(string, bool):\n        return string\n    elif isinstance(string, int):\n        return bool(string)\n    elif not isinstance(string, str):\n        raise OptionError(f'Invalid type {string!r} for option {optname}; use '\n                          '1/0, yes/no, true/false, on/off')\n    elif string.lower() in ('1', 'yes', 'true', 'on'):\n        return True\n    elif string.lower() in ('0', 'no', 'false', 'off'):\n        return False\n    else:\n        raise OptionError(f'Invalid value {string!r} for option {optname}; use '\n                          '1/0, yes/no, true/false, on/off')\n\n\ndef get_int_opt(options, optname, default=None):\n    \"\"\"As :func:`get_bool_opt`, but interpret the value as an integer.\"\"\"\n    string = options.get(optname, default)\n    try:\n        return int(string)\n    except TypeError:\n        raise OptionError(f'Invalid type {string!r} for option {optname}; you '\n                          'must give an integer value')","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_vendor/pygments/util.py#L53-L89","documentation":"Pygments' get_bool_opt raises OptionError ('Invalid type ...') when the option value is neither a bool, an int, nor a str (e.g. None, a list, a dict, a float). The bool/int/str branches are handled, so only a non-string non-numeric type reaches this error.","triggerScenarios":"Calling get_bool_opt(options, optname) where options[optname] is None (and no default supplied), or any non-str/non-int type such as a list or object; commonly when a config key is unset and defaulted to None, or when a value is parsed from JSON into an unexpected type.","commonSituations":"A missing key with default=None passed through; JSON/YAML config loading producing null that becomes None; passing a 0/1 as float (1.0) rather than int.","solutions":["Provide an explicit string default (e.g. '1' or '0') when calling get_bool_opt.","Ensure the config source yields a str/int/bool for that key.","Coerce or guard the value to str before passing the options dict."],"exampleFix":"# before\nval = get_bool_opt(opts, 'noclasses')  # opts['noclasses'] = None\n\n# after\nval = get_bool_opt(opts, 'noclasses', default='0')","handlingStrategy":"type-guard","validationCode":"def is_boolable(v):\n    return isinstance(v, (bool, int, str))\nif not is_boolable(opts.get('noclasses')):\n    opts['noclasses'] = '0'","typeGuard":"def is_bool_option_value(v) -> bool:\n    return isinstance(v, (bool, int, str))","tryCatchPattern":"from pip._vendor.pygments.util import OptionError\ntry:\n    val = get_bool_opt(opts, 'noclasses', default='0')\nexcept OptionError:\n    val = False","preventionTips":["Always pass an explicit default string to get_bool_opt.","Coerce unknown config types to str before building the options dict."],"tags":["pygments","config","validation","types"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}