{"record":{"id":"9e674ea9099683ff","repo":"TheAlgorithms/Python","slug":"invalid-to-type-value-to-type-r-nconversion","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/astronomical_length_scale_conversion.py","lineNumber":90,"sourceCode":"\n    from_sanitized = from_type.lower().strip(\"s\")\n    to_sanitized = to_type.lower().strip(\"s\")\n\n    from_sanitized = UNIT_SYMBOL.get(from_sanitized, from_sanitized)\n    to_sanitized = UNIT_SYMBOL.get(to_sanitized, to_sanitized)\n\n    if from_sanitized not in METRIC_CONVERSION:\n        msg = (\n            f\"Invalid 'from_type' value: {from_type!r}.\\n\"\n            f\"Conversion abbreviations are: {', '.join(METRIC_CONVERSION)}\"\n        )\n        raise ValueError(msg)\n    if to_sanitized not in METRIC_CONVERSION:\n        msg = (\n            f\"Invalid 'to_type' value: {to_type!r}.\\n\"\n            f\"Conversion abbreviations are: {', '.join(METRIC_CONVERSION)}\"\n        )\n        raise ValueError(msg)\n    from_exponent = METRIC_CONVERSION[from_sanitized]\n    to_exponent = METRIC_CONVERSION[to_sanitized]\n    exponent = 1\n\n    if from_exponent > to_exponent:\n        exponent = from_exponent - to_exponent\n    else:\n        exponent = -(to_exponent - from_exponent)\n\n    return value * pow(10, exponent)\n\n\nif __name__ == \"__main__\":\n    from doctest import testmod\n\n    testmod()\n","sourceCodeStart":72,"sourceCodeEnd":107,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/astronomical_length_scale_conversion.py#L72-L107","documentation":"Raised by conversions/astronomical_length_scale_conversion.py when the target unit (to_type) is not a recognized abbreviation in METRIC_CONVERSION after the same sanitization as from_type (lowercase, strip trailing 's', UNIT_SYMBOL alias lookup). It fires only after from_type has already validated, so the source unit was fine.","triggerScenarios":"convert('1', 'km', 'lightyear') when the accepted key is 'ly'; convert('1', 'm', 'pc.') with trailing punctuation; any target abbreviation not present in METRIC_CONVERSION.","commonSituations":"Asymmetric unit calls where from_type is correct but to_type uses a full name or wrong abbreviation; forgetting astronomical units are abbreviated ('ly', 'au') while metric prefixes are short ('nm', 'cm').","solutions":["Use the exact abbreviation listed in the error message for to_type, e.g. convert('1', 'km', 'ly')","Validate both units against the module's METRIC_CONVERSION keys before calling","Normalize external input (strip whitespace/punctuation) before passing"],"exampleFix":"# before\nconvert('1', 'au', 'kilometers')\n# ValueError: Invalid 'to_type' value: 'kilometers'.\n\n# after\nconvert('1', 'au', 'km')","handlingStrategy":"validation","validationCode":"from conversions.astronomical_length_scale_conversion import METRIC_CONVERSION\nto = to_type.lower().strip().removesuffix('s')\nif to not in METRIC_CONVERSION:\n    to = 'm'  # safe default or raise","typeGuard":"def is_valid_unit(unit: str) -> bool:\n    from conversions.astronomical_length_scale_conversion import METRIC_CONVERSION\n    return isinstance(unit, str) and unit.lower().strip().removesuffix('s') in METRIC_CONVERSION","tryCatchPattern":"try:\n    convert(value, from_type, to_type)\nexcept ValueError as e:\n    if \"'to_type'\" in str(e):\n        raise ValueError('check target unit spelling') from e\n    raise","preventionTips":["Validate both units with the same helper before converting","Use the module's own abbreviations in configs, not prose names","Add a units integration test covering every unit you use"],"tags":["conversions","units","validation","astronomy"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}