{"record":{"id":"20f3f111b535f31c","repo":"TheAlgorithms/Python","slug":"invalid-from-type-or-to-type-value-from-type","errorCode":null,"errorMessage":"Invalid 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\nSupported values are: {', '.join(WEIGHT_TYPE_CHART)}","messagePattern":"Invalid 'from_type' or 'to_type' value: (.+?), (.+?)\nSupported values are: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/weight_conversion.py","lineNumber":312,"sourceCode":"    >>> weight_conversion(\"atomic-mass-unit\",\"ounce\",2)\n    1.1714775914938915e-25\n    >>> weight_conversion(\"atomic-mass-unit\",\"carrat\",2)\n    1.660540199e-23\n    >>> weight_conversion(\"atomic-mass-unit\",\"atomic-mass-unit\",2)\n    1.999999998903455\n    >>> weight_conversion(\"slug\", \"kilogram\", 1)\n    Traceback (most recent call last):\n    ...\n    ValueError: Invalid 'from_type' or 'to_type' value: 'slug', 'kilogram'\n    Supported values are: kilogram, gram, milligram, metric-ton, long-ton, short-ton, \\\npound, stone, ounce, carrat, atomic-mass-unit\n    \"\"\"\n    if to_type not in KILOGRAM_CHART or from_type not in WEIGHT_TYPE_CHART:\n        msg = (\n            f\"Invalid 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\\n\"\n            f\"Supported values are: {', '.join(WEIGHT_TYPE_CHART)}\"\n        )\n        raise ValueError(msg)\n    return value * KILOGRAM_CHART[to_type] * WEIGHT_TYPE_CHART[from_type]\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":294,"sourceCodeEnd":320,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/weight_conversion.py#L294-L320","documentation":"Raised by weight_conversion() in conversions/weight_conversion.py when from_type is not in WEIGHT_TYPE_CHART or to_type is not in KILOGRAM_CHART. Supported units: kilogram, gram, milligram, metric-ton, long-ton, short-ton, pound, stone, ounce, carrat, atomic-mass-unit (note the misspelled 'carrat' is the required token). Argument order is (from_type, to_type, value) — value last.","triggerScenarios":"Calling weight_conversion('slug', 'kilogram', 1) as in the doctest; using 'carat' (correct spelling — rejected); swapping arguments so a number lands in from_type; using 'ton' instead of 'metric-ton'.","commonSituations":"Assuming standard spellings ('carat', 'ton') instead of this library's tokens; argument-order mistakes because most sibling converters take (value, from, to) while this one takes (from, to, value); adding unsupported units like slug, troy ounce, or microgram.","solutions":["Use exact tokens: kilogram, gram, milligram, metric-ton, long-ton, short-ton, pound, stone, ounce, carrat, atomic-mass-unit.","Fix argument order: weight_conversion(from_type, to_type, value) — value goes last.","Alias-map user input: {'carat': 'carrat', 'ton': 'metric-ton', 'tonne': 'metric-ton', 'lb': 'pound', 'oz': 'ounce', 'amu': 'atomic-mass-unit'}."],"exampleFix":"# before\nweight_conversion(1, 'kilogram', 'gram')  # ValueError: Invalid 'from_type' or 'to_type' value: 1, 'kilogram'\n\n# after\nweight_conversion('kilogram', 'gram', 1)  # 1000.0","handlingStrategy":"validation","validationCode":"from conversions.weight_conversion import WEIGHT_TYPE_CHART, KILOGRAM_CHART\n\nfrom_type = ALIASES.get(from_type, from_type)\nto_type = ALIASES.get(to_type, to_type)\nif from_type not in WEIGHT_TYPE_CHART or to_type not in KILOGRAM_CHART:\n    raise ValueError(f'unsupported weight unit(s) {from_type!r}, {to_type!r}')\nweight_conversion(from_type, to_type, value)","typeGuard":"from conversions.weight_conversion import WEIGHT_TYPE_CHART\n\ndef is_weight_unit(u: object) -> bool:\n    return isinstance(u, str) and u in WEIGHT_TYPE_CHART","tryCatchPattern":"try:\n    weight_conversion(f, t, v)\nexcept ValueError as e:\n    if 'from_type' or 'to_type' in str(e):\n        raise UserInputError(f'unknown weight unit {f!r}->{t!r}') from e\n    raise","preventionTips":["Argument order is (from_type, to_type, value) — value is last","The library token is 'carrat', not 'carat'; 'metric-ton', not 'ton'","Alias lb/oz/amu at your boundary before calling"],"tags":["python","validation","units","weight","conversion"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}