{"record":{"id":"9ded4b07ea6e339b","repo":"numpy/numpy","slug":"name-must-be-0","errorCode":null,"errorMessage":"{name} must be >= 0","messagePattern":"(.+?) must be >= 0","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"numpy/_core/arrayprint.py","lineNumber":982,"sourceCode":"        s = '[' + s[len(hanging_indent):] + ']'\n        return s\n\n    try:\n        # invoke the recursive part with an initial index and prefix\n        return recurser(index=(),\n                        hanging_indent=next_line_prefix,\n                        curr_width=line_width)\n    finally:\n        # recursive closures have a cyclic reference to themselves, which\n        # requires gc to collect (gh-10620). To avoid this problem, for\n        # performance, we break the cycle:\n        recurser = None\n\ndef _none_or_positive_arg(x, name):\n    if x is None:\n        return -1\n    if x < 0:\n        raise ValueError(f\"{name} must be >= 0\")\n    return x\n\nclass FloatingFormat:\n    \"\"\" Formatter for subtypes of np.floating \"\"\"\n    def __init__(self, data, precision, floatmode, suppress_small, sign=False,\n                 *, legacy=None):\n        # for backcompatibility, accept bools\n        if isinstance(sign, bool):\n            sign = '+' if sign else '-'\n\n        self._legacy = legacy\n        if self._legacy <= 113:\n            # when not 0d, legacy does not support '-'\n            if data.shape != () and sign == '-':\n                sign = ' '\n\n        self.floatmode = floatmode\n        if floatmode == 'unique':","sourceCodeStart":964,"sourceCodeEnd":1000,"githubUrl":"https://github.com/numpy/numpy/blob/44f1f77dd85fd0b91f9f0329f0f012f99bd3f3d6/numpy/_core/arrayprint.py#L964-L1000","documentation":"Raised by the internal helper _none_or_positive_arg() in numpy/_core/arrayprint.py, which validates numeric keyword arguments to array-printing / float-formatting functions. The contract is: the value may be None (a sentinel meaning 'unset', internally mapped to -1) or a non-negative number; anything strictly negative is rejected. The {name} placeholder carries the offending argument name (e.g. 'precision', 'pad_left', 'exp_digits', 'min_digits') so the message identifies which argument was at fault.","triggerScenarios":"Calling np.format_float_scientific / np.format_float_positional (or anything routed through them, such as np.array2string with formatter options) with a strictly negative value for precision, pad_left, pad_right, exp_digits, or min_digits. For example np.format_float_scientific(1.0, precision=-2) or np.format_float_positional(0.5, pad_left=-1).","commonSituations":"Off-by-one math on a computed width/precision before passing it to a formatter; passing a value that another API returns as -1 on 'unset' without re-checking; confusing the linewidth=-1 sentinel of np.set_printoptions (which means 'no wrapping') with the precision-family arguments, which do NOT accept -1.","solutions":["Check the offending named argument before the call: keep it None, or clamp to max(0, value).","If you intended 'unset', pass None explicitly rather than -1.","For line/width wrapping use np.set_printoptions(linewidth=-1) — do not reuse that -1 for precision/pad/min_digits."],"exampleFix":"// before\nnp.format_float_scientific(x, precision=width - 1)  # width == 0 -> -1\n// after\nnp.format_float_scientific(x, precision=max(0, width - 1))","handlingStrategy":"validation","validationCode":"def safe_pos_arg(value, name):\n    if value is None:\n        return None\n    if value < 0:\n        raise ValueError(f\"{name} must be >= 0, got {value!r}\")\n    return value\n\nprecision = safe_pos_arg(computed_precision, 'precision')\nnp.format_float_scientific(x, precision=precision)","typeGuard":"def is_none_or_non_negative(v) -> bool:\n    return v is None or (isinstance(v, (int, float)) and not isinstance(v, bool) and v >= 0)","tryCatchPattern":"try:\n    np.format_float_scientific(x, precision=p)\nexcept ValueError as e:\n    if 'must be >= 0' in str(e):\n        p = max(0, p) if p is not None else None\n        np.format_float_scientific(x, precision=p)\n    else:\n        raise","preventionTips":["Treat None as the only 'unset' sentinel for these arguments; never use -1.","Clamp user-supplied widths with max(0, value) before formatting.","Do not forward linewidth=-1 from np.set_printoptions into precision-family args."],"tags":["numpy","array-printing","argument-validation","valueerror"],"backgroundTag":null,"analyzedSha":"44f1f77dd85fd0b91f9f0329f0f012f99bd3f3d6","analyzedAt":"2026-08-11T21:31:04.068Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}