TheAlgorithms/Python · error · ValueError
Invalid 'from_type' value: {from_type!r}.\nConversion abbrev
Error message
Invalid 'from_type' value: {from_type!r}.\nConversion abbreviations are: {', '.join(METRIC_CONVERSION)} What it means
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.
Source
Thrown at conversions/astronomical_length_scale_conversion.py:84
>>> length_conversion(4, "wrongUnit", "inch")
Traceback (most recent call last):
...
ValueError: Invalid 'from_type' value: 'wrongUnit'.
Conversion abbreviations are: m, km, Mm, Gm, Tm, Pm, Em, Zm, Ym
"""
from_sanitized = from_type.lower().strip("s")
to_sanitized = to_type.lower().strip("s")
from_sanitized = UNIT_SYMBOL.get(from_sanitized, from_sanitized)
to_sanitized = UNIT_SYMBOL.get(to_sanitized, to_sanitized)
if from_sanitized not in METRIC_CONVERSION:
msg = (
f"Invalid 'from_type' value: {from_type!r}.\n"
f"Conversion abbreviations are: {', '.join(METRIC_CONVERSION)}"
)
raise ValueError(msg)
if to_sanitized not in METRIC_CONVERSION:
msg = (
f"Invalid 'to_type' value: {to_type!r}.\n"
f"Conversion abbreviations are: {', '.join(METRIC_CONVERSION)}"
)
raise ValueError(msg)
from_exponent = METRIC_CONVERSION[from_sanitized]
to_exponent = METRIC_CONVERSION[to_sanitized]
exponent = 1
if from_exponent > to_exponent:
exponent = from_exponent - to_exponent
else:
exponent = -(to_exponent - from_exponent)
return value * pow(10, exponent)
View on GitHub (pinned to f5988cc097)
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
Example fix
# before
convert('1', 'kilometres', 'meters')
# ValueError: Invalid 'from_type' value: 'kilometres'.
# after
convert('1', 'km', 'm') Defensive patterns
Strategy: validation
Validate before calling
from conversions.astronomical_length_scale_conversion import METRIC_CONVERSION
u = from_type.lower().strip().removesuffix('s')
if u not in METRIC_CONVERSION:
raise ValueError(f'unknown source unit {from_type!r}; valid: {sorted(METRIC_CONVERSION)}') Type guard
def is_valid_unit(unit: str) -> bool:
from conversions.astronomical_length_scale_conversion import METRIC_CONVERSION
return isinstance(unit, str) and unit.lower().strip().removesuffix('s') in METRIC_CONVERSION Try / catch
try:
convert(value, from_type, to_type)
except ValueError as e:
if "'from_type'" in str(e):
raise ValueError('check source unit spelling') from e
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Invalid 'to_type' value: {to_type!r}.\nConversion abbreviati
- Empty string was passed to the function
- Non-binary value was passed to the function
- Non-binary value was passed to the function
- Non-binary value was passed to the function
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/d5d1dc6eeb573369.
Report an issue: GitHub.