TheAlgorithms/Python · error · ValueError
Incorrect 'from_type' or 'to_type' value: {unit_from!r}, {un
Error message
Incorrect 'from_type' or 'to_type' value: {unit_from!r}, {unit_to!r}
Valid values are: {', '.join(speed_chart_inverse)} What it means
Raised by convert_speed() in conversions/speed_conversions.py when unit_to is not a key of speed_chart or unit_from is not a key of speed_chart_inverse (same unit set). Valid units are km/h, mph, knot, m/s (exact strings). The ValueError message lists the valid values.
Source
Thrown at conversions/speed_conversions.py:64
>>> convert_speed(100, "mph", "km/h")
160.934
>>> convert_speed(100, "mph", "m/s")
44.704
>>> convert_speed(100, "mph", "knot")
86.898
>>> convert_speed(100, "knot", "km/h")
185.2
>>> convert_speed(100, "knot", "m/s")
51.444
>>> convert_speed(100, "knot", "mph")
115.078
"""
if unit_to not in speed_chart or unit_from not in speed_chart_inverse:
msg = (
f"Incorrect 'from_type' or 'to_type' value: {unit_from!r}, {unit_to!r}\n"
f"Valid values are: {', '.join(speed_chart_inverse)}"
)
raise ValueError(msg)
return round(speed * speed_chart[unit_from] * speed_chart_inverse[unit_to], 3)
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Use exactly one of: km/h, mph, knot, m/s.
- Add an alias map at your boundary: {'kph': 'km/h', 'kmh': 'km/h', 'knots': 'knot', 'mps': 'm/s'}.
- If you need ft/s or other units, convert yourself via m/s (1 ft/s = 0.3048 m/s) after calling with m/s.
Example fix
# before
convert_speed(100, 'kph', 'mph') # ValueError: Incorrect 'from_type' or 'to_type' value
# after
ALIASES = {'kph': 'km/h', 'kmh': 'km/h', 'knots': 'knot', 'mph': 'mph'}
convert_speed(100, ALIASES['kph'], 'mph') # 62.137 Defensive patterns
Strategy: validation
Validate before calling
from conversions.speed_conversions import speed_chart_inverse
unit_from = ALIASES.get(unit_from.lower(), unit_from)
unit_to = ALIASES.get(unit_to.lower(), unit_to)
if unit_from not in speed_chart_inverse or unit_to not in speed_chart_inverse:
raise ValueError(f'unsupported speed unit(s) {unit_from!r}, {unit_to!r}')
convert_speed(speed, unit_from, unit_to) Type guard
from conversions.speed_conversions import speed_chart_inverse
def is_speed_unit(u: object) -> bool:
return isinstance(u, str) and u in speed_chart_inverse Try / catch
try:
convert_speed(v, f, t)
except ValueError as e:
if 'Incorrect' in str(e):
raise UserInputError(f'unsupported speed unit: {f!r} -> {t!r}') from e
raise Prevention
- Only km/h, mph, knot, m/s are valid — build aliases for kph/kmh/knots
- Match case exactly
- Generate UI options from speed_chart_inverse keys
When it happens
Trigger: Calling convert_speed(100, 'KM/H', 'mph') (case), convert_speed(100, 'kph', 'mph') (alias), or convert_speed(100, 'ft/s', 'm/s') (unsupported unit).
Common situations: UI dropdowns using display labels like 'Kilometers per hour' instead of the library's tokens; synonyms (kph, kmh, kmph) not matching 'km/h'; adding units like ft/s or mach that the chart does not contain.
Related errors
- Invalid 'from_type' value: {from_type!r} Supported values a
- Invalid 'to_type' value: {to_type!r}. Supported values are:
- 'time_value' must be a non-negative number.
- Invalid unit {invalid_unit} is not in {', '.join(time_chart)
- Invalid 'from_type' value: {from_type!r} Supported values a
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/4ddb305eb157b966.
Report an issue: GitHub.