{"record":{"id":"bffe38b5bcb822c0","repo":"python/cpython","slug":"lineno-must-be-an-int-0","errorCode":null,"errorMessage":"lineno must be an int >= 0","messagePattern":"lineno must be an int >= 0","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/_py_warnings.py","lineNumber":277,"sourceCode":"                or \"once\"\n    'message' -- a regex that the warning message must match\n    'category' -- a class that the warning must be a subclass of\n    'module' -- a regex that the module name must match\n    'lineno' -- an integer line number, 0 matches all warnings\n    'append' -- if true, append to the list of filters\n    \"\"\"\n    if action not in {\"error\", \"ignore\", \"always\", \"all\", \"default\", \"module\", \"once\"}:\n        raise ValueError(f\"invalid action: {action!r}\")\n    if not isinstance(message, str):\n        raise TypeError(\"message must be a string\")\n    if not isinstance(category, type) or not issubclass(category, Warning):\n        raise TypeError(\"category must be a Warning subclass\")\n    if not isinstance(module, str):\n        raise TypeError(\"module must be a string\")\n    if not isinstance(lineno, int):\n        raise TypeError(\"lineno must be an int\")\n    if lineno < 0:\n        raise ValueError(\"lineno must be an int >= 0\")\n\n    if message or module:\n        import re\n\n    if message:\n        message = re.compile(message, re.I)\n    else:\n        message = None\n    if module:\n        module = re.compile(module)\n    else:\n        module = None\n\n    _wm._add_filter(action, message, category, module, lineno, append=append)\n\n\ndef simplefilter(action, category=Warning, lineno=0, append=False):\n    \"\"\"Insert a simple entry into the list of warnings filters (at the front).","sourceCodeStart":259,"sourceCodeEnd":295,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_py_warnings.py#L259-L295","documentation":"Raised by warnings.filterwarnings when lineno is an int but negative. Line numbers in filter entries must be >= 0, with 0 meaning 'match warnings on any line'; a negative value never matches anything and almost certainly signals a computation bug, so it is rejected with ValueError.","triggerScenarios":"warnings.filterwarnings('ignore', lineno=-1); lineno computed as lineno_of_match - offset that underflows; sentinel values like -1 passed to mean 'none'.","commonSituations":"Using -1 as a 'not set' sentinel from C-style code; arithmetic on line numbers (e.g. frame.f_lineno - 2) that can go below zero; config validation that accepts negatives.","solutions":["Use 0 to match all lines, or the exact positive line number","Clamp/validate computed values: lineno = max(0, computed)","Replace -1 sentinels with 0 or omit the argument"],"exampleFix":"# before\nwarnings.filterwarnings('ignore', lineno=match.start - offset)  # can be -1 -> ValueError\n\n# after\nwarnings.filterwarnings('ignore', lineno=max(0, match.start - offset))","handlingStrategy":"validation","validationCode":"def as_lineno(value) -> int:\n    if value is None:\n        return 0\n    value = int(value)\n    if value < 0:\n        return 0  # or raise, per your policy\n    return value\n\nimport warnings\nwarnings.filterwarnings('ignore', lineno=as_lineno(computed))","typeGuard":"def is_valid_lineno(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 0","tryCatchPattern":null,"preventionTips":["Clamp computed line numbers with max(0, n)","Use 0 instead of -1 sentinels for 'unspecified'","Validate config ranges (lineno >= 0) when parsing settings files"],"tags":["warnings","validation","lineno","filters"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}