pypa/pip · error · OptionError
Invalid type {string!r} for option {optname}; you must give
Error message
Invalid type {string!r} for option {optname}; you must give an integer value What it means
Pygments' get_int_opt raises OptionError ('Invalid type ...') when int(value) raises TypeError, which happens for values that cannot be converted at all because they are the wrong type (e.g. None, a list, a dict). The conversion attempt fails before any content parsing.
Source
Thrown at src/pip/_vendor/pygments/util.py:88
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')
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 'View on GitHub (pinned to d7d0d0a394)
Solutions
- Supply a numeric default (e.g. default=0) when calling get_int_opt.
- Ensure the config source provides a str or int for that key.
- Pre-validate that the value is not None before passing the options dict.
Example fix
# before val = get_int_opt(opts, 'tabsize') # opts['tabsize'] = None # after val = get_int_opt(opts, 'tabsize', default=8)
Defensive patterns
Strategy: type-guard
Validate before calling
v = opts.get('tabsize')
if not isinstance(v, (str, int)):
opts['tabsize'] = 8 Type guard
def is_int_option_value(v) -> bool:
return v is not None and isinstance(v, (str, int)) and not isinstance(v, bool) Try / catch
from pip._vendor.pygments.util import OptionError
try:
val = get_int_opt(opts, 'tabsize', default=8)
except OptionError:
val = 8 Prevention
- Always supply an integer default to get_int_opt.
- Reject None for numeric keys at the config boundary.
When it happens
Trigger: Calling get_int_opt(options, optname) where options[optname] is None (no default), a list, a dict, or any non-numeric/non-string object; int() of such a value raises TypeError.
Common situations: A missing integer config key whose default resolved to None; a value loaded as a structured type (list/object) instead of a scalar; passing a float where int() on float would actually succeed, so this specifically signals a non-convertible type.
Related errors
- Invalid type {string!r} for option {optname}; use 1/0, yes/n
- Invalid type {val!r} for option {optname}; you must give a l
- Value for option {} must be one of {}
- Invalid value {string!r} for option {optname}; use 1/0, yes/
- Invalid value {string!r} for option {optname}; you must give
AI-assisted analysis of pypa/pip@d7d0d0a394 (2026-08-04).
Data as JSON: /data/errors/e0648f2efadbbaa4.json.
Report an issue: GitHub.