{"record":{"id":"d47c7b8cef6c6e0c","repo":"python/cpython","slug":"invalid-tuple-size-in-creation-of-decimal-from-lis","errorCode":null,"errorMessage":"Invalid tuple size in creation of Decimal from list or tuple.  The list or tuple should have exactly three elements.","messagePattern":"Invalid tuple size in creation of Decimal from list or tuple\\.  The list or tuple should have exactly three elements\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/_pydecimal.py","lineNumber":556,"sourceCode":"        if isinstance(value, Decimal):\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        # From an internal working value\n        if isinstance(value, _WorkRep):\n            self._sign = value.sign\n            self._int = str(value.int)\n            self._exp = int(value.exp)\n            self._is_special = False\n            return self\n\n        # tuple/list conversion (possibly from as_tuple())\n        if isinstance(value, (list,tuple)):\n            if len(value) != 3:\n                raise ValueError('Invalid tuple size in creation of Decimal '\n                                 'from list or tuple.  The list or tuple '\n                                 'should have exactly three elements.')\n            # process sign.  The isinstance test rejects floats\n            if not (isinstance(value[0], int) and value[0] in (0,1)):\n                raise ValueError(\"Invalid sign.  The first value in the tuple \"\n                                 \"should be an integer; either 0 for a \"\n                                 \"positive number or 1 for a negative number.\")\n            self._sign = value[0]\n            if value[2] == 'F':\n                # infinity: value[1] is ignored\n                self._int = '0'\n                self._exp = value[2]\n                self._is_special = True\n            else:\n                # process and validate the digits in value[1]\n                digits = []\n                for digit in value[1]:\n                    if isinstance(digit, int) and 0 <= digit <= 9:","sourceCodeStart":538,"sourceCodeEnd":574,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pydecimal.py#L538-L574","documentation":"Decimal(value) accepts a list or tuple argument, but only if it has exactly three elements in the shape returned by Decimal.as_tuple(): (sign, digits_tuple, exponent). This ValueError is raised when the sequence length differs from 3, i.e. the sequence cannot be interpreted as a decimal triple at all.","triggerScenarios":"Decimal((1, (4, 5))) (missing exponent); Decimal([0, (1,), 2, 'extra']); Decimal((sign, digits)) where exponent was dropped by an intermediate function; Decimal(()) empty tuple.","commonSituations":"Round-tripping as_tuple() output through code that strips or appends fields; building Decimals from rows of a database/CSV where a column is missing; passing a coordinate pair or other 2-tuple that was never a decimal triple.","solutions":["Supply the full triple: Decimal((sign, tuple_of_int_digits_0_9, exponent)) e.g. Decimal((0, (1, 4, 2), -2)) == Decimal('1.42')","If you meant to construct from a numeric string, pass the string instead: Decimal('1.42')","If the tuple came from as_tuple() and was transformed, re-check the transformation preserves all three fields"],"exampleFix":"// before\nd = Decimal((1, (4, 5)))  # ValueError: needs 3 elements\n\n// after\nd = Decimal((1, (4, 5), 0))  # Decimal('-45')","handlingStrategy":"validation","validationCode":"def is_decimal_triple(v):\n    return (isinstance(v, (list, tuple))\n            and len(v) == 3\n            and isinstance(v[0], int) and v[0] in (0, 1)\n            and all(isinstance(d, int) and 0 <= d <= 9 for d in v[1])\n            and (isinstance(v[2], int) or v[2] in ('F', 'n', 'N')))\n\n# d = Decimal(v) if is_decimal_triple(v) else ...","typeGuard":"from typing import Tuple, Iterable\n\ndef is_decimal_triple(v) -> bool:\n    return isinstance(v, (list, tuple)) and len(v) == 3","tryCatchPattern":"try:\n    d = Decimal(candidate)\nexcept (ValueError, TypeError) as e:\n    raise ValueError(f'malformed decimal triple {candidate!r}') from e","preventionTips":["Prefer Decimal(str) or Decimal(int) inputs; use tuples only for as_tuple() round-trips","When serializing, keep the triple shape untouched (sign, digits, exponent)","Schema-validate deserialized triples: length 3, sign in (0,1), digits ints 0-9, exponent int or F/n/N"],"tags":["decimal","constructor","tuple","validation","valueerror"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}