TheAlgorithms/Python · error · ValueError

Invalid 'from_type' or 'to_type' value: {from_type!r}, {to_t

Error message

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

What it means

Raised by weight_conversion() in conversions/weight_conversion.py when from_type is not in WEIGHT_TYPE_CHART or to_type is not in KILOGRAM_CHART. Supported units: kilogram, gram, milligram, metric-ton, long-ton, short-ton, pound, stone, ounce, carrat, atomic-mass-unit (note the misspelled 'carrat' is the required token). Argument order is (from_type, to_type, value) — value last.

Source

Thrown at conversions/weight_conversion.py:312

    >>> weight_conversion("atomic-mass-unit","ounce",2)
    1.1714775914938915e-25
    >>> weight_conversion("atomic-mass-unit","carrat",2)
    1.660540199e-23
    >>> weight_conversion("atomic-mass-unit","atomic-mass-unit",2)
    1.999999998903455
    >>> weight_conversion("slug", "kilogram", 1)
    Traceback (most recent call last):
    ...
    ValueError: Invalid 'from_type' or 'to_type' value: 'slug', 'kilogram'
    Supported values are: kilogram, gram, milligram, metric-ton, long-ton, short-ton, \
pound, stone, ounce, carrat, atomic-mass-unit
    """
    if to_type not in KILOGRAM_CHART or from_type not in WEIGHT_TYPE_CHART:
        msg = (
            f"Invalid 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n"
            f"Supported values are: {', '.join(WEIGHT_TYPE_CHART)}"
        )
        raise ValueError(msg)
    return value * KILOGRAM_CHART[to_type] * WEIGHT_TYPE_CHART[from_type]


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Use exact tokens: kilogram, gram, milligram, metric-ton, long-ton, short-ton, pound, stone, ounce, carrat, atomic-mass-unit.
  2. Fix argument order: weight_conversion(from_type, to_type, value) — value goes last.
  3. Alias-map user input: {'carat': 'carrat', 'ton': 'metric-ton', 'tonne': 'metric-ton', 'lb': 'pound', 'oz': 'ounce', 'amu': 'atomic-mass-unit'}.

Example fix

# before
weight_conversion(1, 'kilogram', 'gram')  # ValueError: Invalid 'from_type' or 'to_type' value: 1, 'kilogram'

# after
weight_conversion('kilogram', 'gram', 1)  # 1000.0
Defensive patterns

Strategy: validation

Validate before calling

from conversions.weight_conversion import WEIGHT_TYPE_CHART, KILOGRAM_CHART

from_type = ALIASES.get(from_type, from_type)
to_type = ALIASES.get(to_type, to_type)
if from_type not in WEIGHT_TYPE_CHART or to_type not in KILOGRAM_CHART:
    raise ValueError(f'unsupported weight unit(s) {from_type!r}, {to_type!r}')
weight_conversion(from_type, to_type, value)

Type guard

from conversions.weight_conversion import WEIGHT_TYPE_CHART

def is_weight_unit(u: object) -> bool:
    return isinstance(u, str) and u in WEIGHT_TYPE_CHART

Try / catch

try:
    weight_conversion(f, t, v)
except ValueError as e:
    if 'from_type' or 'to_type' in str(e):
        raise UserInputError(f'unknown weight unit {f!r}->{t!r}') from e
    raise

Prevention

When it happens

Trigger: Calling weight_conversion('slug', 'kilogram', 1) as in the doctest; using 'carat' (correct spelling — rejected); swapping arguments so a number lands in from_type; using 'ton' instead of 'metric-ton'.

Common situations: Assuming standard spellings ('carat', 'ton') instead of this library's tokens; argument-order mistakes because most sibling converters take (value, from, to) while this one takes (from, to, value); adding unsupported units like slug, troy ounce, or microgram.

Related errors


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