{"record":{"id":"498c540d99691a16","repo":"TheAlgorithms/Python","slug":"invalid-to-type-value-to-type-r-nconversion-498c54","errorCode":null,"errorMessage":"Invalid 'to_type' value: {to_type!r}.\\nConversion abbreviations are: {', '.join(METRIC_CONVERSION)}","messagePattern":"Invalid 'to_type' value: (.+?)\\.\\\\nConversion abbreviations are: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/length_conversion.py","lineNumber":121,"sourceCode":"    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":103,"sourceCodeEnd":133,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/length_conversion.py#L103-L133","documentation":"Raised by length_conversion(value, from_type, to_type) in conversions/length_conversion.py:121 when the destination unit fails the same normalization/membership check as from_type. It fires only after from_type already validated, so it pinpoints the second unit: anything not normalizing to mm/cm/m/km/in/ft/yd/mi — e.g. 'to_meter' typo, 'inches2', unsupported 'nm' — raises a ValueError listing valid abbreviations.","triggerScenarios":"length_conversion(4, 'inch', 'wrongUnit'), length_conversion(1, 'm', 'nmi') (nautical mile), or swapped-argument calls where a value/label lands in to_type.","commonSituations":"Unit pairs stored in separate config columns where the target column has dirtier data; plural forms not covered by rstrip('s') (e.g. 'inches' works, 'feet' works via table, but 'metres' fails); UI dropdowns that include units the library does not support.","solutions":["Use the documented abbreviations/full names for the target unit (mm, cm, m, km, in, ft, yd, mi)","Validate both units once at startup against METRIC_CONVERSION keys after applying the library's normalization (lower + rstrip('s') + TYPE_CONVERSION)","Restrict your UI's unit dropdown to exactly the supported set so invalid targets cannot be selected"],"exampleFix":"# before\nlength_conversion(1, 'm', 'nmi')  # ValueError\n\n# after\nlength_conversion(1, 'm', 'mi') * 1.15078  # nautical miles via mi if needed","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(to_type) not in METRIC_CONVERSION:\n    raise ValueError(f\"unsupported unit {to_type!r}; use {sorted(METRIC_CONVERSION)}\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate both units in one normalization pass at startup","Keep unit vocabularies in config aligned with the library's 8 supported units","Remember normalization is lower() + rstrip('s') — anything beyond that needs your own mapping"],"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"}