{"record":{"id":"d5d1dc6eeb573369","repo":"TheAlgorithms/Python","slug":"invalid-from-type-value-from-type-r-nconvers","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/astronomical_length_scale_conversion.py","lineNumber":84,"sourceCode":"    >>> length_conversion(4, \"wrongUnit\", \"inch\")\n    Traceback (most recent call last):\n      ...\n    ValueError: Invalid 'from_type' value: 'wrongUnit'.\n    Conversion abbreviations are: m, km, Mm, Gm, Tm, Pm, Em, Zm, Ym\n    \"\"\"\n\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","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/astronomical_length_scale_conversion.py#L66-L102","documentation":"Raised by conversions/astronomical_length_scale_conversion.py when the source unit (from_type) is not a recognized metric abbreviation after sanitization (lowercased, trailing 's' stripped, alias-mapped via UNIT_SYMBOL). Valid keys are the METRIC_CONVERSION dict entries (m, cm, um/nm-scale prefixes, km, au, ly, etc.). The message lists all accepted abbreviations.","triggerScenarios":"convert('1', 'meter', 'cm') if 'meter' has no alias (only 'm' or aliases in UNIT_SYMBOL survive); typos like 'kms' -> 'km' is fine but 'kilometre' or 'KM.' fail; passing '' or None.lower()-crashing None actually raises AttributeError first.","commonSituations":"Passing full unit names instead of abbreviations; locale-specific spellings ('metre', 'kilometer'); trailing punctuation or hidden whitespace (only leading/trailing spaces via strip('s') mishandle ' s'); case is handled, plurals are handled, but arbitrary whitespace inside the string is not.","solutions":["Use the exact abbreviations printed in the error message, e.g. convert('1', 'km', 'm')","Strip and lowercase your own input and map full names to abbreviations before calling","Check the module's UNIT_SYMBOL dict for supported aliases and use one of those spellings"],"exampleFix":"# before\nconvert('1', 'kilometres', 'meters')\n# ValueError: Invalid 'from_type' value: 'kilometres'.\n\n# after\nconvert('1', 'km', 'm')","handlingStrategy":"validation","validationCode":"from conversions.astronomical_length_scale_conversion import METRIC_CONVERSION\nu = from_type.lower().strip().removesuffix('s')\nif u not in METRIC_CONVERSION:\n    raise ValueError(f'unknown source unit {from_type!r}; valid: {sorted(METRIC_CONVERSION)}')","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 \"'from_type'\" in str(e):\n        raise ValueError('check source unit spelling') from e\n    raise","preventionTips":["Whitelist units in your UI dropdown instead of free text","Keep a single units constant mapped to the module's abbreviations","Print METRIC_CONVERSION keys in your own error messages"],"tags":["conversions","units","validation","astronomy"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}