pypa/pip · error · OptionError

Invalid value {string!r} for option {optname}; you must give

Error message

Invalid value {string!r} for option {optname}; you must give an integer value

What it means

Pygments' get_int_opt raises OptionError ('Invalid value ...') when int(value) raises ValueError: the value is a string (or other int-convertible type) whose content is not a valid integer, e.g. 'abc', '1.5', or ''.

Source

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

    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')
    except ValueError:
        raise OptionError(f'Invalid value {string!r} for option {optname}; you '
                          'must give an integer value')

def get_list_opt(options, optname, default=None):
    """
    If the key `optname` from the dictionary `options` is a string,
    split it at whitespace and return it. If it is already a list
    or a tuple, it is returned as a list.
    """
    val = options.get(optname, default)
    if isinstance(val, str):
        return val.split()
    elif isinstance(val, (list, tuple)):
        return list(val)
    else:
        raise OptionError(f'Invalid type {val!r} for option {optname}; you '
                          'must give a list value')

View on GitHub (pinned to d7d0d0a394)

Solutions

  1. Set the value to a base-10 integer string or int.
  2. Strip non-digit characters before passing the options dict.
  3. Provide a sensible integer default and omit the key when unsure.

Example fix

# before
opts['tabsize'] = 'wide'
val = get_int_opt(opts, 'tabsize')

# after
opts['tabsize'] = '4'
val = get_int_opt(opts, 'tabsize')
Defensive patterns

Strategy: validation

Validate before calling

raw = opts.get('tabsize')
if not str(raw).lstrip('-').isdigit():
    raise ValueError('tabsize must be an integer')
get_int_opt(opts, 'tabsize')

Type guard

def is_int_str(v: str) -> bool:
    return isinstance(v, str) and v.lstrip('-').isdigit()

Try / catch

from pip._vendor.pygments.util import OptionError
try:
    val = get_int_opt(opts, 'tabsize')
except OptionError:
    val = 8

Prevention

When it happens

Trigger: Calling get_int_opt with options[optname] set to a non-numeric string such as 'wide', '1.0', or an empty string; common when reading a free-text field that is expected to be numeric.

Common situations: Config files with units or text in a numeric field; user-supplied CLI values not pre-validated; locale-specific number formats ('1,000').

Related errors


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