{"record":{"id":"57080f00c627b6cb","repo":"TheAlgorithms/Python","slug":"invalid-from-type-value-from-type-r-nconvers-57080f","errorCode":null,"errorMessage":"Invalid 'from_type' value: {from_type!r}.\\nConversion abbreviations are: {', '.join(METRIC_CONVERSION)}","messagePattern":"Invalid 'from_type' value: (.+?)\\.\\\\nConversion abbreviations are: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/length_conversion.py","lineNumber":115,"sourceCode":"    0.3\r\n    >>> length_conversion(3, \"mm\", \"in\")\r\n    0.1181103\r\n    >>> length_conversion(4, \"wrongUnit\", \"inch\")\r\n    Traceback (most recent call last):\r\n      ...\r\n    ValueError: Invalid 'from_type' value: 'wrongUnit'.\r\n    Conversion abbreviations are: mm, cm, m, km, in, ft, yd, mi\r\n    \"\"\"\r\n    new_from = from_type.lower().rstrip(\"s\")\r\n    new_from = TYPE_CONVERSION.get(new_from, new_from)\r\n    new_to = to_type.lower().rstrip(\"s\")\r\n    new_to = TYPE_CONVERSION.get(new_to, new_to)\r\n    if new_from not in METRIC_CONVERSION:\r\n        msg = (\r\n            f\"Invalid 'from_type' value: {from_type!r}.\\n\"\r\n            f\"Conversion abbreviations are: {', '.join(METRIC_CONVERSION)}\"\r\n        )\r\n        raise ValueError(msg)\r\n    if new_to not in METRIC_CONVERSION:\r\n        msg = (\r\n            f\"Invalid 'to_type' value: {to_type!r}.\\n\"\r\n            f\"Conversion abbreviations are: {', '.join(METRIC_CONVERSION)}\"\r\n        )\r\n        raise ValueError(msg)\r\n    return (\r\n        value\r\n        * METRIC_CONVERSION[new_from].from_factor\r\n        * METRIC_CONVERSION[new_to].to_factor\r\n    )\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    import doctest\r\n\r\n    doctest.testmod()\r\n","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/length_conversion.py#L97-L133","documentation":"Raised by length_conversion(value, from_type, to_type) in conversions/length_conversion.py:115 when the normalized source unit is not in METRIC_CONVERSION. Normalization lowercases and strips a trailing 's', and maps long names via TYPE_CONVERSION (e.g. 'Meters'->'m'), so anything that still is not one of mm/cm/m/km/in/ft/yd/mi after that — 'wrongUnit', 'nm', 'Mtr' — raises a ValueError whose second line lists the valid abbreviations.","triggerScenarios":"length_conversion(4, 'wrongUnit', 'inch'), length_conversion(1, 'NM', 'm') (nanometer not supported), length_conversion(1, 'metre', 'ft') ('metre' is not in TYPE_CONVERSION; only 'meter' is).","commonSituations":"British spellings ('metre', 'kilometre') that the mapping table does not include; unsupported units (nm, um, nautical mile, 'ly'); abbreviations like 'in.' or 'FT' with punctuation; data-driven unit columns from spreadsheets.","solutions":["Use one of the documented keys/abbreviations: mm, cm, m, km, in, ft, yd, mi (case-insensitive, optional trailing 's')","If you need other units, convert to metres yourself first (1 nm = 1e-9 m) and call with 'm'","Whitelist-check units against sorted(METRIC_CONVERSION) and TYPE_CONVERSION keys before calling"],"exampleFix":"# before\nlength_conversion(4, 'metre', 'feet')  # 'metre' not mapped -> ValueError\n\n# after\nlength_conversion(4, 'meter', 'feet')  # 13.12336","handlingStrategy":"validation","validationCode":"from conversions.length_conversion import METRIC_CONVERSION, TYPE_CONVERSION\ndef norm(u: str) -> str:\n    u = u.lower().rstrip('s')\n    return TYPE_CONVERSION.get(u, u)\nif norm(from_type) not in METRIC_CONVERSION:\n    raise ValueError(f\"unsupported unit {from_type!r}; use {sorted(METRIC_CONVERSION)}\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer the 2-letter abbreviations (mm, cm, m, km, in, ft, yd, mi) to avoid spelling-table gaps","Watch for British spellings ('metre') that the table does not map","Restrict unit dropdowns to the supported set"],"tags":["python","value-error","units","length","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}