pypa/pip · error · OptionError
Invalid value {string!r} for option {optname}; use 1/0, yes/
Error message
Invalid value {string!r} for option {optname}; use 1/0, yes/no, true/false, on/off What it means
Pygments' get_bool_opt raises OptionError ('Invalid value ...') when the option value is a string that, after lowercasing, is not one of the recognized boolean literals (1/yes/true/on or 0/no/false/off). The type is acceptable (str) but the content is not a valid boolean spelling.
Source
Thrown at src/pip/_vendor/pygments/util.py:78
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')
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,View on GitHub (pinned to d7d0d0a394)
Solutions
- Use exactly one of: 1, yes, true, on (true) or 0, no, false, off (false), case-insensitive.
- Strip/normalize the string before it reaches get_bool_opt.
- Map your application's boolean spellings to the accepted set before building the options dict.
Example fix
# before opts['nowrap'] = 'enable' val = get_bool_opt(opts, 'nowrap') # after opts['nowrap'] = 'on' val = get_bool_opt(opts, 'nowrap')
Defensive patterns
Strategy: validation
Validate before calling
TRUE = {'1','yes','true','on'}
FALSE = {'0','no','false','off'}
raw = str(opts.get('nowrap','')).strip().lower()
if raw not in TRUE and raw not in FALSE:
raise ValueError('nowrap must be a boolean literal')
get_bool_opt(opts, 'nowrap') Type guard
def is_valid_bool_str(v: str) -> bool:
return v.strip().lower() in {'1','yes','true','on','0','no','false','off'} Try / catch
from pip._vendor.pygments.util import OptionError
try:
val = get_bool_opt(opts, 'nowrap')
except OptionError:
val = False Prevention
- Normalize boolean config strings to the accepted set before passing them on.
- Document the accepted literals in your config schema.
When it happens
Trigger: Calling get_bool_opt with options[optname] set to a string like 'enable', 't', 'yep', or any non-recognized token; often from CLI flags or config files using a custom yes/no spelling.
Common situations: Config files using 'enable'/'disable' or 't'/'f'; localized values; trailing whitespace that wasn't stripped before validation.
Related errors
- Value for option {} must be one of {}
- Invalid type {string!r} for option {optname}; use 1/0, yes/n
- Invalid type {string!r} for option {optname}; you must give
- Invalid value {string!r} for option {optname}; you must give
- Invalid type {val!r} for option {optname}; you must give a l
AI-assisted analysis of pypa/pip@d7d0d0a394 (2026-08-04).
Data as JSON: /data/errors/fd7c1f3d2ca7035e.json.
Report an issue: GitHub.