TheAlgorithms/Python · error · ValueError

Invalid 'from_type' value: {from_type!r} Supported values a

Error message

Invalid 'from_type' value: {from_type!r}  Supported values are:
{', '.join(METRIC_CONVERSION)}

What it means

Raised by volume_conversion() in conversions/volume_conversions.py when from_type is not a key in METRIC_CONVERSION. Supported units (exact strings): cubic meter, litre, kilolitre, gallon, cubic yard, cubic foot, cup. The message embeds the full list at runtime.

Source

Thrown at conversions/volume_conversions.py:64

    0.264172
    >>> volume_conversion(1, "kilolitre", "cubic meter")
    1
    >>> volume_conversion(3, "gallon", "cubic yard")
    0.017814279
    >>> 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

View on GitHub (pinned to f5988cc097)

Solutions

  1. Use one of: cubic meter, litre, kilolitre, gallon, cubic yard, cubic foot, cup (singular, British spelling for litre).
  2. Normalize aliases first: {'liter': 'litre', 'liters': 'litre', 'ml': 'litre' with a 0.001 factor, 'gallons': 'gallon'}.
  3. For ml/cm3, convert via litre yourself (1 ml = 0.001 litre, 1 cm3 = 1 ml) before or after calling.

Example fix

# before
volume_conversion(1, 'liter', 'cup')  # ValueError: Invalid 'from_type' value: 'liter'

# after
ALIASES = {'liter': 'litre', 'liters': 'litre', 'gallons': 'gallon', 'cubic meters': 'cubic meter'}
volume_conversion(1, ALIASES['liter'], 'cup')
Defensive patterns

Strategy: validation

Validate before calling

from conversions.volume_conversions import METRIC_CONVERSION

from_type = ALIASES.get(from_type.lower(), from_type)
if from_type not in METRIC_CONVERSION:
    raise ValueError(f'unsupported volume unit {from_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 'from_type' in str(e):
        f = {'liter': 'litre', 'liters': 'litre'}.get(f.lower(), f)
        result = volume_conversion(v, f, t)
    else:
        raise

Prevention

When it happens

Trigger: Calling volume_conversion(4, 'wrongUnit', 'litre'), using 'liter' (US spelling) instead of 'litre', or 'millilitre'/'ml' which the chart does not contain.

Common situations: US-locale data using 'liter'/'gallons' (plural) tokens; metric sub-units (ml, cm3, teaspoon) not covered by this seven-unit chart; display labels passed unmodified.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/6e9ea1e7d1e2ea55. Report an issue: GitHub.