pytest-dev/pytest · error · ValueError

invalid truth value {val!r}

Error message

invalid truth value {val!r}

What it means

Raised by _strtobool (config/__init__.py:2227-2242), the distutils-derived truthiness parser used to coerce INI bool values. Only the lowercase tokens y/yes/t/true/on/1 (True) and n/no/f/false/off/0 (False) are accepted; anything else raises ValueError naming the bad token. Used by the 'bool' branch of _getini_ini for INI-mode bool options.

Source

Thrown at src/_pytest/config/__init__.py:2242

    return tw


def _strtobool(val: str) -> bool:
    """Convert a string representation of truth to True or False.

    True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
    are 'n', 'no', 'f', 'false', 'off', and '0'.  Raises ValueError if
    'val' is anything else.

    .. note:: Copied from distutils.util.
    """
    val = val.lower()
    if val in ("y", "yes", "t", "true", "on", "1"):
        return True
    elif val in ("n", "no", "f", "false", "off", "0"):
        return False
    else:
        raise ValueError(f"invalid truth value {val!r}")


@lru_cache(maxsize=50)
def parse_warning_filter(
    arg: str, *, escape: bool
) -> tuple[warnings._ActionKind, str, type[Warning], str, int]:
    """Parse a warnings filter string.

    This is copied from warnings._setoption with the following changes:

    * Does not apply the filter.
    * Escaping is optional.
    * Raises UsageError so we get nice error messages on failure.
    """
    __tracebackhide__ = True
    error_template = dedent(
        f"""\
        while parsing the following warning configuration:

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Use one of the accepted tokens: yes/no, true/false, on/off, y/n, t/f, 1/0 (case-insensitive).
  2. For pyproject.toml, prefer a native TOML boolean (true/false) which bypasses _strtobool entirely.
  3. Strip stray whitespace/quotes from the ini value.

Example fix

# before (pytest.ini)
[pytest]
addopts = enable

# after
addopts = true
Defensive patterns

Strategy: validation

Validate before calling

TRUTHY = {'y','yes','t','true','on','1'}
FALSY = {'n','no','f','false','off','0'}
def parse_bool(token: str) -> bool:
    t = token.strip().lower()
    if t in TRUTHY: return True
    if t in FALSY: return False
    raise ValueError(f'invalid truth value {t!r}')

Type guard

def is_valid_truth_token(token: str) -> bool:
    return token.strip().lower() in {'y','yes','t','true','on','1','n','no','f','false','off','0'}

Try / catch

try:
    val = config.getini(name)
except ValueError:
    # normalize the ini bool token to true/false and reload

Prevention

When it happens

Trigger: Setting a bool ini option (in pytest.ini or pyproject.toml ini_options read in ini mode) to a string outside the accepted set, e.g. addopts = enable, x = 2, or x = YEP.

Common situations: Free-form yes/no variants ("yep","disable","enabled"); uppercase tokens are fine after .lower() but mixed nonsense fails; non-numeric junk passed as a bool; misconfigured CI config templates.

Related errors


AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04). Data as JSON: /data/errors/57637125a5a0acd8.json. Report an issue: GitHub.