{"record":{"id":"f5e7b74ca323c907","repo":"numpy/numpy","slug":"threshold-must-be-numeric","errorCode":null,"errorMessage":"threshold must be numeric","messagePattern":"threshold must be numeric","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"numpy/_core/arrayprint.py","lineNumber":107,"sourceCode":"    elif legacy == '1.21':\n        options['legacy'] = 121\n    elif legacy == '1.25':\n        options['legacy'] = 125\n    elif legacy == '2.1':\n        options['legacy'] = 201\n    elif legacy == '2.2':\n        options['legacy'] = 202\n    elif legacy is None:\n        pass  # OK, do nothing.\n    else:\n        warnings.warn(\n            \"legacy printing option can currently only be '1.13', '1.21', \"\n            \"'1.25', '2.1', '2.2' or `False`\", stacklevel=3)\n\n    if threshold is not None:\n        # forbid the bad threshold arg suggested by stack overflow, gh-12351\n        if not isinstance(threshold, numbers.Number):\n            raise TypeError(\"threshold must be numeric\")\n        if np.isnan(threshold):\n            raise ValueError(\"threshold must be non-NAN, try \"\n                             \"sys.maxsize for untruncated representation\")\n\n    if precision is not None:\n        # forbid the bad precision arg as suggested by issue #18254\n        try:\n            options['precision'] = operator.index(precision)\n        except TypeError as e:\n            raise TypeError('precision must be an integer') from e\n\n    return options\n\n\n@set_module('numpy')\ndef set_printoptions(precision=None, threshold=None, edgeitems=None,\n                     linewidth=None, suppress=None, nanstr=None,\n                     infstr=None, formatter=None, sign=None, floatmode=None,","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/numpy/numpy/blob/e117b3ca4edacf581f440dccfd7f3242f0312afa/numpy/_core/arrayprint.py#L89-L125","documentation":"Raised as TypeError by set_printoptions when threshold is not an instance of numbers.Number. threshold controls how many elements trigger summarization; this guard (added per gh-12351) rejects a common bad value such as a string that was historically suggested online.","triggerScenarios":"np.set_printoptions(threshold='all'); threshold=np.inf supplied as a string; passing a non-numeric object.","commonSituations":"Copying outdated Stack Overflow advice; deserializing threshold from JSON as a string; UI controls returning text.","solutions":["Pass an int (e.g. 1000) or sys.maxsize to disable summarization","Coerce external input: threshold = int(threshold) before calling","Ensure threshold is a numbers.Number (int, float that is not NaN)"],"exampleFix":"# before\nnp.set_printoptions(threshold='all')\n# after\nimport sys\nnp.set_printoptions(threshold=sys.maxsize)","handlingStrategy":"type-guard","validationCode":"import numbers\ndef safe_threshold(thr):\n    if not isinstance(thr, numbers.Number):\n        raise TypeError(f\"threshold must be numeric, got {type(thr).__name__}\")\n    return int(thr)\n# np.set_printoptions(threshold=safe_threshold(user_thr))","typeGuard":"import numbers\ndef is_numeric_threshold(thr) -> bool:\n    return isinstance(thr, numbers.Number) and not isinstance(thr, bool)","tryCatchPattern":"try:\n    np.set_printoptions(threshold=thr)\nexcept TypeError as e:\n    if 'threshold must be numeric' in str(e):\n        import sys\n        np.set_printoptions(threshold=sys.maxsize)\n    else:\n        raise","preventionTips":["Coerce config-sourced thresholds to int at load time","Use sys.maxsize for untruncated output, not a string","Validate numeric options before applying"],"tags":["printing","config","threshold","type-error"],"analyzedSha":"e117b3ca4edacf581f440dccfd7f3242f0312afa","analyzedAt":"2026-08-07T01:25:31.049Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}