{"record":{"id":"4ddb305eb157b966","repo":"TheAlgorithms/Python","slug":"incorrect-from-type-or-to-type-value-unit-fr","errorCode":null,"errorMessage":"Incorrect 'from_type' or 'to_type' value: {unit_from!r}, {unit_to!r}\nValid values are: {', '.join(speed_chart_inverse)}","messagePattern":"Incorrect 'from_type' or 'to_type' value: (.+?), (.+?)\nValid values are: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/speed_conversions.py","lineNumber":64,"sourceCode":"    >>> convert_speed(100, \"mph\", \"km/h\")\n    160.934\n    >>> convert_speed(100, \"mph\", \"m/s\")\n    44.704\n    >>> convert_speed(100, \"mph\", \"knot\")\n    86.898\n    >>> convert_speed(100, \"knot\", \"km/h\")\n    185.2\n    >>> convert_speed(100, \"knot\", \"m/s\")\n    51.444\n    >>> convert_speed(100, \"knot\", \"mph\")\n    115.078\n    \"\"\"\n    if unit_to not in speed_chart or unit_from not in speed_chart_inverse:\n        msg = (\n            f\"Incorrect 'from_type' or 'to_type' value: {unit_from!r}, {unit_to!r}\\n\"\n            f\"Valid values are: {', '.join(speed_chart_inverse)}\"\n        )\n        raise ValueError(msg)\n    return round(speed * speed_chart[unit_from] * speed_chart_inverse[unit_to], 3)\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":46,"sourceCodeEnd":72,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/speed_conversions.py#L46-L72","documentation":"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.","triggerScenarios":"Calling convert_speed(100, 'KM/H', 'mph') (case), convert_speed(100, 'kph', 'mph') (alias), or convert_speed(100, 'ft/s', 'm/s') (unsupported unit).","commonSituations":"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.","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."],"exampleFix":"# before\nconvert_speed(100, 'kph', 'mph')  # ValueError: Incorrect 'from_type' or 'to_type' value\n\n# after\nALIASES = {'kph': 'km/h', 'kmh': 'km/h', 'knots': 'knot', 'mph': 'mph'}\nconvert_speed(100, ALIASES['kph'], 'mph')  # 62.137","handlingStrategy":"validation","validationCode":"from conversions.speed_conversions import speed_chart_inverse\n\nunit_from = ALIASES.get(unit_from.lower(), unit_from)\nunit_to = ALIASES.get(unit_to.lower(), unit_to)\nif unit_from not in speed_chart_inverse or unit_to not in speed_chart_inverse:\n    raise ValueError(f'unsupported speed unit(s) {unit_from!r}, {unit_to!r}')\nconvert_speed(speed, unit_from, unit_to)","typeGuard":"from conversions.speed_conversions import speed_chart_inverse\n\ndef is_speed_unit(u: object) -> bool:\n    return isinstance(u, str) and u in speed_chart_inverse","tryCatchPattern":"try:\n    convert_speed(v, f, t)\nexcept ValueError as e:\n    if 'Incorrect' in str(e):\n        raise UserInputError(f'unsupported speed unit: {f!r} -> {t!r}') from e\n    raise","preventionTips":["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"],"tags":["python","validation","units","speed","conversion"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}