TheAlgorithms/Python · error · ValueError
Invalid 'to_type' value: {to_type!r}. Supported values are:
Error message
Invalid 'to_type' value: {to_type!r}. Supported values are:
{', '.join(METRIC_CONVERSION)} What it means
Raised by volume_conversion() in conversions/volume_conversions.py when to_type is not a key in METRIC_CONVERSION (checked after from_type). Same supported set: cubic meter, litre, kilolitre, gallon, cubic yard, cubic foot, cup. Message lists all valid values.
Source
Thrown at conversions/volume_conversions.py:69
>>> volume_conversion(2, "cubic yard", "litre")
1529.1
>>> volume_conversion(4, "cubic foot", "cup")
473.396
>>> volume_conversion(1, "cup", "kilolitre")
0.000236588
>>> volume_conversion(4, "wrongUnit", "litre")
Traceback (most recent call last):
...
ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are:
cubic meter, litre, kilolitre, gallon, cubic yard, cubic foot, cup
"""
if from_type not in METRIC_CONVERSION:
raise ValueError(
f"Invalid 'from_type' value: {from_type!r} Supported values are:\n"
+ ", ".join(METRIC_CONVERSION)
)
if to_type not in METRIC_CONVERSION:
raise ValueError(
f"Invalid 'to_type' value: {to_type!r}. Supported values are:\n"
+ ", ".join(METRIC_CONVERSION)
)
return (
value
* METRIC_CONVERSION[from_type].from_factor
* METRIC_CONVERSION[to_type].to_factor
)
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Restrict targets to the seven supported keys.
- Post-convert for missing units: get litres from the library, then multiply by the needed factor (1 litre = 1.05669 US quarts).
- Maintain one canonical unit vocabulary in your app and translate at the library boundary.
Example fix
# before volume_conversion(1, 'litre', 'quart') # ValueError: Invalid 'to_type' value: 'quart' # after litres = volume_conversion(1, 'litre', 'litre') quarts = litres * 1.0566882094325937 # US liquid quart
Defensive patterns
Strategy: validation
Validate before calling
from conversions.volume_conversions import METRIC_CONVERSION
to_type = ALIASES.get(to_type.lower(), to_type)
if to_type not in METRIC_CONVERSION:
raise ValueError(f'unsupported target volume unit {to_type!r}')
volume_conversion(value, from_type, to_type) Type guard
from conversions.volume_conversions import METRIC_CONVERSION
def is_volume_unit(u: object) -> bool:
return isinstance(u, str) and u in METRIC_CONVERSION Try / catch
try:
volume_conversion(v, f, t)
except ValueError as e:
if 'to_type' in str(e):
litres = volume_conversion(v, f, 'litre')
result = litres * CUSTOM_FACTOR.get(t, 1)
else:
raise Prevention
- Only seven units supported; convert exotic targets via litre yourself
- Watch for pluralized labels from NLP or user text
- Keep a canonical unit vocabulary and translate once at the edge
When it happens
Trigger: Calling volume_conversion(1, 'litre', 'millilitre'), or targeting 'quart', 'pint', 'tablespoon' — none are in the chart.
Common situations: Cooking/measurement apps needing tsp/tbsp/fl oz, which this library lacks; pluralized unit names from natural-language parsing; output unit driven by user preference strings.
Related errors
- Invalid 'from_type' value: {from_type!r} Supported values a
- Invalid 'from_type' value: {from_type!r} Supported values a
- Invalid 'to_type' value: {to_type!r}. Supported values are:
- Incorrect 'from_type' or 'to_type' value: {unit_from!r}, {un
- 'time_value' must be a non-negative number.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/5763589979fa2155.
Report an issue: GitHub.