{"record":{"id":"6ae65a9ab5be61df","repo":"TheAlgorithms/Python","slug":"incorrect-from-type-or-to-type-value-from-ty","errorCode":null,"errorMessage":"Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\\nValid values are: {', '.join(ENERGY_CONVERSION)}","messagePattern":"Incorrect 'from_type' or 'to_type' value: (.+?), (.+?)\\\\nValid values are: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/energy_conversions.py","lineNumber":107,"sourceCode":"    ValueError: Incorrect 'from_type' or 'to_type' value: 'wrongunit', 'joule'\n    Valid values are: joule, ... footpound\n    >>> energy_conversion(\"joule\", \"wrongunit\", 1) # doctest: +ELLIPSIS\n    Traceback (most recent call last):\n      ...\n    ValueError: Incorrect 'from_type' or 'to_type' value: 'joule', 'wrongunit'\n    Valid values are: joule, ... footpound\n    >>> energy_conversion(\"123\", \"abc\", 1) # doctest: +ELLIPSIS\n    Traceback (most recent call last):\n      ...\n    ValueError: Incorrect 'from_type' or 'to_type' value: '123', 'abc'\n    Valid values are: joule, ... footpound\n    \"\"\"\n    if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION:\n        msg = (\n            f\"Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\\n\"\n            f\"Valid values are: {', '.join(ENERGY_CONVERSION)}\"\n        )\n        raise ValueError(msg)\n    return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type]\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":89,"sourceCodeEnd":115,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/energy_conversions.py#L89-L115","documentation":"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.","triggerScenarios":"energy_conversion(1, 'joules', 'kwh') (plural and case mismatch), energy_conversion('123', 'abc', 1) swapped argument order, or any typo like 'Joule', 'BTU', 'calorie'.","commonSituations":"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.","solutions":["Use exact lowercase keys from ENERGY_CONVERSION, e.g. energy_conversion(1, 'joule', 'kwh')","Normalize input before calling: from_type.strip().lower() and strip plural 's' carefully (note 'footpound' has no plural form in the table)","Validate against list(ENERGY_CONVERSION) at your boundary and show the allowed set to the user"],"exampleFix":"# before\nenergy_conversion(1, 'Joules', 'KWH')  # ValueError\n\n# after\nfrom conversions.energy_conversions import ENERGY_CONVERSION\nfrom_type = 'Joules'.strip().lower().rstrip('s')\nfrom_type = {'footpound': 'footpound'}.get(from_type, from_type)\nif from_type not in ENERGY_CONVERSION:\n    raise ValueError(f\"unknown unit {from_type!r}; valid: {sorted(ENERGY_CONVERSION)}\")\nenergy_conversion(1, from_type, 'kwh')","handlingStrategy":"validation","validationCode":"from conversions.energy_conversions import ENERGY_CONVERSION\nfrom_type, to_type = from_type.strip().lower(), to_type.strip().lower()\nif from_type not in ENERGY_CONVERSION or to_type not in ENERGY_CONVERSION:\n    raise ValueError(f\"units must be one of {sorted(ENERGY_CONVERSION)}\")","typeGuard":null,"tryCatchPattern":"try:\n    energy_conversion(value, from_type, to_type)\nexcept ValueError as e:\n    raise ValueError(f\"bad unit in ({from_type!r} -> {to_type!r})\") from e","preventionTips":["Keep positional order in mind: (value, from_type, to_type)","Normalize units to lowercase at your boundary","Expose the valid unit list (ENERGY_CONVERSION keys) in your UI/config schema"],"tags":["python","value-error","units","energy","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}