{"id":"cb89c8cd8ff13ae9","repo":"pytest-dev/pytest","slug":"number-is-negative","errorCode":null,"errorMessage":"number is negative","messagePattern":"number is negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/_pytest/config/__init__.py","lineNumber":2309,"sourceCode":"    except warnings._OptionError as e:\n        raise UsageError(error_template.format(error=str(e))) from None\n    try:\n        category: type[Warning] = _resolve_warning_category(category_)\n    except ImportError:\n        raise\n    except Exception:\n        exc_info = ExceptionInfo.from_current()\n        exception_text = exc_info.getrepr(style=\"native\")\n        raise UsageError(error_template.format(error=exception_text)) from None\n    if message and escape:\n        message = re.escape(message)\n    if module and escape:\n        module = re.escape(module) + r\"\\Z\"\n    if lineno_:\n        try:\n            lineno = int(lineno_)\n            if lineno < 0:\n                raise ValueError(\"number is negative\")\n        except ValueError as e:\n            raise UsageError(\n                error_template.format(error=f\"invalid lineno {lineno_!r}: {e}\")\n            ) from None\n    else:\n        lineno = 0\n    try:\n        re.compile(message)\n        re.compile(module)\n    except re.error as e:\n        raise UsageError(\n            error_template.format(error=f\"Invalid regex {e.pattern!r}: {e}\")\n        ) from None\n    return action, message, category, module, lineno\n\n\ndef _resolve_warning_category(category: str) -> type[Warning]:\n    \"\"\"","sourceCodeStart":2291,"sourceCodeEnd":2327,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/config/__init__.py#L2291-L2327","documentation":"This is the inner ValueError raised when the line-number field of a warning filter specification is a valid integer but negative. The 'number is negative' string is embedded into the lineno error template (error 63) that is actually shown to the user. Line numbers in warning filters must be non-negative.","triggerScenarios":"A filter string with a 5th field that parses as int but is < 0, e.g. 'ignore::DeprecationWarning:::module:-1' or -W 'ignore::DeprecationWarning:::mymod:-5'. The code does int(lineno_) then checks lineno < 0 and raises ValueError('number is negative').","commonSituations":"Off-by-one or sign mistakes when hand-crafting a lineno-scoped filter; accidentally passing a negative value from a templated/scripted filterwarnings generation; misreading the filter field order and putting a value meant elsewhere into the lineno slot.","solutions":["Use 0 (or omit the field) to mean 'any line number'.","Use a specific positive line number to target a particular source line.","Re-check field order: action:message:category:module:lineno — the 5th field is lineno."],"exampleFix":"# before\n[pytest]\nfilterwarnings = [\"ignore::DeprecationWarning::mymod:-1\"]\n\n# after\n[pytest]\nfilterwarnings = [\"ignore::DeprecationWarning::mymod:0\"]","handlingStrategy":"validation","validationCode":"def validate_lineno_field(spec: str) -> int:\n    parts = spec.split(':')\n    if len(parts) >= 5 and parts[4].strip():\n        lineno = int(parts[4].strip())\n        if lineno < 0:\n            raise ValueError(f'lineno must be >= 0, got {lineno}')\n        return lineno\n    return 0","typeGuard":"def is_valid_lineno(value: str) -> bool:\n    try:\n        return int(value) >= 0\n    except ValueError:\n        return False","tryCatchPattern":null,"preventionTips":["Omit the lineno field unless you need line-scoped filtering.","Generate filter strings programmatically with a helper that validates each field."],"tags":["pytest","config","warnings","filterwarnings","validation"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}