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 length_conversion(value, from_type, to_type) in conversions/length_conversion.py:115 when the normalized source unit is not in METRIC_CONVERSION. Normalization lowercases and strips a trailing 's', and maps long names via TYPE_CONVERSION (e.g. 'Meters'->'m'), so anything that still is not one of mm/cm/m/km/in/ft/yd/mi after that — 'wrongUnit', 'nm', 'Mtr' — raises a ValueError whose second line lists the valid abbreviations.

Source

Thrown at conversions/length_conversion.py:115

    0.3
    >>> length_conversion(3, "mm", "in")
    0.1181103
    >>> length_conversion(4, "wrongUnit", "inch")
    Traceback (most recent call last):
      ...
    ValueError: Invalid 'from_type' value: 'wrongUnit'.
    Conversion abbreviations are: mm, cm, m, km, in, ft, yd, mi
    """
    new_from = from_type.lower().rstrip("s")
    new_from = TYPE_CONVERSION.get(new_from, new_from)
    new_to = to_type.lower().rstrip("s")
    new_to = TYPE_CONVERSION.get(new_to, new_to)
    if new_from 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 new_to not in METRIC_CONVERSION:
        msg = (
            f"Invalid 'to_type' value: {to_type!r}.\n"
            f"Conversion abbreviations are: {', '.join(METRIC_CONVERSION)}"
        )
        raise ValueError(msg)
    return (
        value
        * METRIC_CONVERSION[new_from].from_factor
        * METRIC_CONVERSION[new_to].to_factor
    )


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Use one of the documented keys/abbreviations: mm, cm, m, km, in, ft, yd, mi (case-insensitive, optional trailing 's')
  2. If you need other units, convert to metres yourself first (1 nm = 1e-9 m) and call with 'm'
  3. Whitelist-check units against sorted(METRIC_CONVERSION) and TYPE_CONVERSION keys before calling

Example fix

# before
length_conversion(4, 'metre', 'feet')  # 'metre' not mapped -> ValueError

# after
length_conversion(4, 'meter', 'feet')  # 13.12336
Defensive patterns

Strategy: validation

Validate before calling

from conversions.length_conversion import METRIC_CONVERSION, TYPE_CONVERSION
def norm(u: str) -> str:
    u = u.lower().rstrip('s')
    return TYPE_CONVERSION.get(u, u)
if norm(from_type) not in METRIC_CONVERSION:
    raise ValueError(f"unsupported unit {from_type!r}; use {sorted(METRIC_CONVERSION)}")

Prevention

When it happens

Trigger: length_conversion(4, 'wrongUnit', 'inch'), length_conversion(1, 'NM', 'm') (nanometer not supported), length_conversion(1, 'metre', 'ft') ('metre' is not in TYPE_CONVERSION; only 'meter' is).

Common situations: British spellings ('metre', 'kilometre') that the mapping table does not include; unsupported units (nm, um, nautical mile, 'ly'); abbreviations like 'in.' or 'FT' with punctuation; data-driven unit columns from spreadsheets.

Related errors


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