TheAlgorithms/Python · error · ValueError

Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to

Error message

Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\nValid values are: {', '.join(ENERGY_CONVERSION)}

What it means

Raised by energy_conversion(value, from_type, to_type) in conversions/energy_conversions.py:107 when either unit name is not a key in the ENERGY_CONVERSION table. The function normalizes nothing (no case folding, no plural stripping), so both from_type and to_type must exactly match supported keys such as 'joule' or 'footpound'; otherwise a ValueError listing the valid keys is raised.

Source

Thrown at conversions/energy_conversions.py:107

    ValueError: Incorrect 'from_type' or 'to_type' value: 'wrongunit', 'joule'
    Valid values are: joule, ... footpound
    >>> energy_conversion("joule", "wrongunit", 1) # doctest: +ELLIPSIS
    Traceback (most recent call last):
      ...
    ValueError: Incorrect 'from_type' or 'to_type' value: 'joule', 'wrongunit'
    Valid values are: joule, ... footpound
    >>> energy_conversion("123", "abc", 1) # doctest: +ELLIPSIS
    Traceback (most recent call last):
      ...
    ValueError: Incorrect 'from_type' or 'to_type' value: '123', 'abc'
    Valid values are: joule, ... footpound
    """
    if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION:
        msg = (
            f"Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n"
            f"Valid values are: {', '.join(ENERGY_CONVERSION)}"
        )
        raise ValueError(msg)
    return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type]


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Use exact lowercase keys from ENERGY_CONVERSION, e.g. energy_conversion(1, 'joule', 'kwh')
  2. Normalize input before calling: from_type.strip().lower() and strip plural 's' carefully (note 'footpound' has no plural form in the table)
  3. Validate against list(ENERGY_CONVERSION) at your boundary and show the allowed set to the user

Example fix

# before
energy_conversion(1, 'Joules', 'KWH')  # ValueError

# after
from conversions.energy_conversions import ENERGY_CONVERSION
from_type = 'Joules'.strip().lower().rstrip('s')
from_type = {'footpound': 'footpound'}.get(from_type, from_type)
if from_type not in ENERGY_CONVERSION:
    raise ValueError(f"unknown unit {from_type!r}; valid: {sorted(ENERGY_CONVERSION)}")
energy_conversion(1, from_type, 'kwh')
Defensive patterns

Strategy: validation

Validate before calling

from conversions.energy_conversions import ENERGY_CONVERSION
from_type, to_type = from_type.strip().lower(), to_type.strip().lower()
if from_type not in ENERGY_CONVERSION or to_type not in ENERGY_CONVERSION:
    raise ValueError(f"units must be one of {sorted(ENERGY_CONVERSION)}")

Try / catch

try:
    energy_conversion(value, from_type, to_type)
except ValueError as e:
    raise ValueError(f"bad unit in ({from_type!r} -> {to_type!r})") from e

Prevention

When it happens

Trigger: energy_conversion(1, 'joules', 'kwh') (plural and case mismatch), energy_conversion('123', 'abc', 1) swapped argument order, or any typo like 'Joule', 'BTU', 'calorie'.

Common situations: Passing abbreviations ('J', 'kW·h') or capitalized names when the API expects lowercase full names; argument order confusion because from_type is the second positional parameter; unit names sourced from user input or headers without normalization.

Related errors


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