TheAlgorithms/Python · error · ValueError
Invalid 'to_type' value: {to_type!r}.\nConversion abbreviati
Error message
Invalid 'to_type' value: {to_type!r}.\nConversion abbreviations are: {', '.join(METRIC_CONVERSION)} What it means
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.
Source
Thrown at conversions/astronomical_length_scale_conversion.py:90
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)
if __name__ == "__main__":
from doctest import testmod
testmod()
View on GitHub (pinned to f5988cc097)
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
Example fix
# before
convert('1', 'au', 'kilometers')
# ValueError: Invalid 'to_type' value: 'kilometers'.
# after
convert('1', 'au', 'km') Defensive patterns
Strategy: validation
Validate before calling
from conversions.astronomical_length_scale_conversion import METRIC_CONVERSION
to = to_type.lower().strip().removesuffix('s')
if to not in METRIC_CONVERSION:
to = 'm' # safe default or raise 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 "'to_type'" in str(e):
raise ValueError('check target unit spelling') from e
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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').
Related errors
- Invalid 'from_type' value: {from_type!r}.\nConversion abbrev
- 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/9e674ea9099683ff.
Report an issue: GitHub.