{"record":{"id":"114dc6cb61ed4822","repo":"python/cpython","slug":"the-third-value-in-the-tuple-must-be-an-integer-o","errorCode":null,"errorMessage":"The third value in the tuple must be an integer, or one of the strings 'F', 'n', 'N'.","messagePattern":"The third value in the tuple must be an integer, or one of the strings 'F', 'n', 'N'\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/_pydecimal.py","lineNumber":593,"sourceCode":"                        # skip leading zeros\n                        if digits or digit != 0:\n                            digits.append(digit)\n                    else:\n                        raise ValueError(\"The second value in the tuple must \"\n                                         \"be composed of integers in the range \"\n                                         \"0 through 9.\")\n                if value[2] in ('n', 'N'):\n                    # NaN: digits form the diagnostic\n                    self._int = ''.join(map(str, digits))\n                    self._exp = value[2]\n                    self._is_special = True\n                elif isinstance(value[2], int):\n                    # finite number: digits give the coefficient\n                    self._int = ''.join(map(str, digits or [0]))\n                    self._exp = value[2]\n                    self._is_special = False\n                else:\n                    raise ValueError(\"The third value in the tuple must \"\n                                     \"be an integer, or one of the \"\n                                     \"strings 'F', 'n', 'N'.\")\n            return self\n\n        if isinstance(value, float):\n            if context is None:\n                context = getcontext()\n            context._raise_error(FloatOperation,\n                \"strict semantics for mixing floats and Decimals are \"\n                \"enabled\")\n            value = Decimal.from_float(value)\n            self._exp  = value._exp\n            self._sign = value._sign\n            self._int  = value._int\n            self._is_special  = value._is_special\n            return self\n\n        raise TypeError(\"Cannot convert %r to Decimal\" % value)","sourceCodeStart":575,"sourceCodeEnd":611,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pydecimal.py#L575-L611","documentation":"In Decimal's tuple/list constructor, the third element (exponent) must be an int for finite numbers, or one of the exact strings 'F' (infinity), 'n', 'N' (NaN). Anything else — 'f', 'Inf', None, a float — raises this ValueError. Note the case-sensitivity: only uppercase 'F' means infinity while both 'n' and 'N' mean NaN.","triggerScenarios":"Decimal((0, (1,), 'f')) (lowercase f); Decimal((0, (1,), None)); Decimal((0, (1,), 2.0)) (float exponent not int); Decimal((0, (1,), 'inf')).","commonSituations":"Deserializing triples where special-value markers were lowercased by JSON or a normalizer; replacing the exponent with None for 'no exponent'; copying as_tuple() output of a NaN ((0, (0,), 'n') style) but mutating the marker string.","solutions":["Use an int exponent for finite values: Decimal((0, (1, 4), -1))","Use the exact markers: 'F' for infinity, 'n' or 'N' for NaN","If the marker may be arbitrary case, normalize it first: exp = exp.upper() if isinstance(exp, str) else exp, mapping 'INF'->'F', 'NAN'->'n'"],"exampleFix":"// before\nd = Decimal((0, (1,), marker))  # marker='f' or 'NaN' -> ValueError\n\n// after\nMARK = {'f': 'F', 'inf': 'F', 'n': 'n', 'nan': 'n'}\nd = Decimal((0, (1,), MARK.get(marker.lower(), marker)))","handlingStrategy":"validation","validationCode":"SPECIAL_EXP = ('F', 'n', 'N')\n\ndef valid_exp(e):\n    return isinstance(e, int) or e in SPECIAL_EXP\n\n# normalize common markers before constructing:\ndef norm_exp(e):\n    if isinstance(e, str):\n        return {'f': 'F', 'inf': 'F', 'n': 'n', 'nan': 'n'}.get(e.lower(), e)\n    return e","typeGuard":"def is_valid_third(e) -> bool:\n    return isinstance(e, int) or e in ('F', 'n', 'N')","tryCatchPattern":"try:\n    d = Decimal((sign, digits, exp))\nexcept ValueError as e:\n    if 'third value' in str(e):\n        d = Decimal((sign, digits, norm_exp(exp)))","preventionTips":["Remember case rules: 'F' = infinity (uppercase only), 'n'/'N' = NaN","Don't substitute None for missing exponents; use 0 for finite integers","Normalize marker strings at the deserialization boundary"],"tags":["decimal","constructor","tuple","exponent","special-values","valueerror"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}