{"record":{"id":"57637125a5a0acd8","repo":"pytest-dev/pytest","slug":"invalid-truth-value-val-r","errorCode":null,"errorMessage":"invalid truth value {val!r}","messagePattern":"invalid truth value (.+?)","errorType":"exception","errorClass":"UsageError","httpStatus":null,"severity":"error","filePath":"src/_pytest/config/__init__.py","lineNumber":2242,"sourceCode":"    return tw\n\n\ndef _strtobool(val: str) -> bool:\n    \"\"\"Convert a string representation of truth to True or False.\n\n    True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values\n    are 'n', 'no', 'f', 'false', 'off', and '0'.  Raises ValueError if\n    'val' is anything else.\n\n    .. note:: Copied from distutils.util.\n    \"\"\"\n    val = val.lower()\n    if val in (\"y\", \"yes\", \"t\", \"true\", \"on\", \"1\"):\n        return True\n    elif val in (\"n\", \"no\", \"f\", \"false\", \"off\", \"0\"):\n        return False\n    else:\n        raise ValueError(f\"invalid truth value {val!r}\")\n\n\n@lru_cache(maxsize=50)\ndef parse_warning_filter(\n    arg: str, *, escape: bool\n) -> tuple[warnings._ActionKind, str, type[Warning], str, int]:\n    \"\"\"Parse a warnings filter string.\n\n    This is copied from warnings._setoption with the following changes:\n\n    * Does not apply the filter.\n    * Escaping is optional.\n    * Raises UsageError so we get nice error messages on failure.\n    \"\"\"\n    __tracebackhide__ = True\n    error_template = dedent(\n        f\"\"\"\\\n        while parsing the following warning configuration:","sourceCodeStart":2224,"sourceCodeEnd":2260,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/config/__init__.py#L2224-L2260","documentation":"Raised by pytest's internal _strtobool() helper (copied from distutils.util.strtobool) when a string does not match any recognized truthy or falsy value. Accepted true values: 'y','yes','t','true','on','1'; accepted false values: 'n','no','f','false','off','0'. The comparison is case-insensitive (val is lowercased first).","triggerScenarios":"Calling _strtobool() with a string outside the accepted set — e.g., 'maybe', '2', 'yep', '', or a string with surrounding whitespace that isn't stripped (since only .lower() is applied, not .strip()). pytest itself uses this for parsing boolean-like CLI values.","commonSituations":"Passing user-supplied or environment-variable strings that don't match the strict vocabulary. Whitespace or newlines in the input (not stripped). Localized values like 'si' or 'ja'. Empty string from an unset env var.","solutions":["Normalize the input before calling: strip whitespace and validate against the accepted set","Map custom truthy/falsy strings to 'true'/'false' before passing to _strtobool","Handle empty or None inputs separately before calling _strtobool"],"exampleFix":"# before\nval = _strtobool(user_input)\n\n# after\nuser_input = user_input.strip().lower()\nif user_input not in ('y','yes','t','true','on','1','n','no','f','false','off','0'):\n    raise ValueError(f\"Cannot interpret {user_input!r} as boolean\")\nval = _strtobool(user_input)","handlingStrategy":"validation","validationCode":"_TRUE = {'y', 'yes', 't', 'true', 'on', '1'}\n_FALSE = {'n', 'no', 'f', 'false', 'off', '0'}\ndef safe_strtobool(val: str) -> bool:\n    val = val.strip().lower()\n    if val in _TRUE:\n        return True\n    if val in _FALSE:\n        return False\n    raise ValueError(f\"invalid truth value {val!r}; expected one of {_TRUE | _FALSE}\")","typeGuard":"def is_valid_truth_string(val: str) -> bool:\n    return val.strip().lower() in {'y','yes','t','true','on','1','n','no','f','false','off','0'}","tryCatchPattern":"try:\n    result = _strtobool(user_input)\nexcept ValueError:\n    result = False  # or prompt user for valid input","preventionTips":["Strip and lowercase input before calling _strtobool to handle whitespace/case","Validate against the accepted set before calling","Handle empty strings and None separately before calling _strtobool"],"tags":["pytest","config","strtobool","string-parsing","boolean"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}