{"record":{"id":"a4e1cb88749f6bf3","repo":"RustPython/RustPython","slug":"cannot-convert-r-to-decimal","errorCode":null,"errorMessage":"Cannot convert %r to Decimal","messagePattern":"Cannot convert %r to Decimal","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/_pydecimal.py","lineNumber":608,"sourceCode":"                    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)\n\n    @classmethod\n    def from_number(cls, number):\n        \"\"\"Converts a real number to a decimal number, exactly.\n\n        >>> Decimal.from_number(314)              # int\n        Decimal('314')\n        >>> Decimal.from_number(0.1)              # float\n        Decimal('0.1000000000000000055511151231257827021181583404541015625')\n        >>> Decimal.from_number(Decimal('3.14'))  # another decimal instance\n        Decimal('3.14')\n        \"\"\"\n        if isinstance(number, (int, Decimal, float)):\n            return cls(number)\n        raise TypeError(\"Cannot convert %r to Decimal\" % number)\n\n    @classmethod\n    def from_float(cls, f):","sourceCodeStart":590,"sourceCodeEnd":626,"githubUrl":"https://github.com/RustPython/RustPython/blob/aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd/Lib/_pydecimal.py#L590-L626","documentation":"Decimal(value) accepts str, int, a 3-element list/tuple, float (under the FloatOperation signal), and Decimal; this TypeError is the fallback when value is none of those. Typical offenders are complex, None, bytes, and Fraction, which look numeric but have no defined exact conversion in the constructor.","triggerScenarios":"Decimal(Fraction(1, 3)); Decimal(None) from an unset config value; Decimal(complex(1, 2)); Decimal(b'1.5') with a bytes payload.","commonSituations":"Passing a Fraction expecting exact conversion; a defaulted-None variable reaching the constructor; unparsed bytes input from a socket or file.","solutions":["For Fraction use Decimal(f.numerator) / Decimal(f.denominator) (exact to context precision) or Decimal(str(f)).","Decode bytes before use: Decimal(b'1.5'.decode()).","Guard with isinstance(value, (str, int, float, Decimal, tuple, list)) and give None an explicit default like Decimal(0)."],"exampleFix":"# before\nfrom fractions import Fraction\nd = Decimal(Fraction(1, 3))  # TypeError\n\n# after\nf = Fraction(1, 3)\nd = Decimal(f.numerator) / Decimal(f.denominator)","handlingStrategy":"type-guard","validationCode":"if not isinstance(value, (str, int, float, Decimal, list, tuple)):\n    raise TypeError(f'cannot build Decimal from {type(value).__name__}')","typeGuard":"def can_convert_to_decimal(v):\n    return isinstance(v, (str, int, float, Decimal, list, tuple))","tryCatchPattern":"try:\n    d = Decimal(value)\nexcept TypeError:\n    d = None  # or convert explicitly, e.g. via str(value)","preventionTips":["Convert Fractions with numerator/denominator before Decimal.","Decode bytes to str before constructing.","Give optional parameters an explicit Decimal(0) default instead of None."],"tags":["python","decimal","typeerror","type-conversion"],"backgroundTag":"cannot-convert-to-decimal","analyzedSha":"aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd","analyzedAt":"2026-08-17T00:37:52.100Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}