pypa/pip · error · OptionError

Invalid type {string!r} for option {optname}; use 1/0, yes/n

Error message

Invalid type {string!r} for option {optname}; use 1/0, yes/no, true/false, on/off

What it means

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.

Source

Thrown at src/pip/_vendor/pygments/util.py:71

def get_bool_opt(options, optname, default=None):
    """
    Intuitively, this is `options.get(optname, default)`, but restricted to
    Boolean value. The Booleans can be represented as string, in order to accept
    Boolean value from the command line arguments. If the key `optname` is
    present in the dictionary `options` and is not associated with a Boolean,
    raise an `OptionError`. If it is absent, `default` is returned instead.

    The valid string values for ``True`` are ``1``, ``yes``, ``true`` and
    ``on``, the ones for ``False`` are ``0``, ``no``, ``false`` and ``off``
    (matched case-insensitively).
    """
    string = options.get(optname, default)
    if isinstance(string, bool):
        return string
    elif isinstance(string, int):
        return bool(string)
    elif not isinstance(string, str):
        raise OptionError(f'Invalid type {string!r} for option {optname}; use '
                          '1/0, yes/no, true/false, on/off')
    elif string.lower() in ('1', 'yes', 'true', 'on'):
        return True
    elif string.lower() in ('0', 'no', 'false', 'off'):
        return False
    else:
        raise OptionError(f'Invalid value {string!r} for option {optname}; use '
                          '1/0, yes/no, true/false, on/off')


def get_int_opt(options, optname, default=None):
    """As :func:`get_bool_opt`, but interpret the value as an integer."""
    string = options.get(optname, default)
    try:
        return int(string)
    except TypeError:
        raise OptionError(f'Invalid type {string!r} for option {optname}; you '
                          'must give an integer value')

View on GitHub (pinned to d7d0d0a394)

Solutions

  1. Provide an explicit string default (e.g. '1' or '0') when calling get_bool_opt.
  2. Ensure the config source yields a str/int/bool for that key.
  3. Coerce or guard the value to str before passing the options dict.

Example fix

# before
val = get_bool_opt(opts, 'noclasses')  # opts['noclasses'] = None

# after
val = get_bool_opt(opts, 'noclasses', default='0')
Defensive patterns

Strategy: type-guard

Validate before calling

def is_boolable(v):
    return isinstance(v, (bool, int, str))
if not is_boolable(opts.get('noclasses')):
    opts['noclasses'] = '0'

Type guard

def is_bool_option_value(v) -> bool:
    return isinstance(v, (bool, int, str))

Try / catch

from pip._vendor.pygments.util import OptionError
try:
    val = get_bool_opt(opts, 'noclasses', default='0')
except OptionError:
    val = False

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of pypa/pip@d7d0d0a394 (2026-08-04). Data as JSON: /data/errors/ce9d6479e752ad98.json. Report an issue: GitHub.