{"record":{"id":"5763589979fa2155","repo":"TheAlgorithms/Python","slug":"invalid-to-type-value-to-type-r-supported-v-576358","errorCode":null,"errorMessage":"Invalid 'to_type' value: {to_type!r}.  Supported values are:\n{', '.join(METRIC_CONVERSION)}","messagePattern":"Invalid 'to_type' value: (.+?)\\.  Supported values are:\n(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/volume_conversions.py","lineNumber":69,"sourceCode":"    >>> volume_conversion(2, \"cubic yard\", \"litre\")\r\n    1529.1\r\n    >>> volume_conversion(4, \"cubic foot\", \"cup\")\r\n    473.396\r\n    >>> volume_conversion(1, \"cup\", \"kilolitre\")\r\n    0.000236588\r\n    >>> volume_conversion(4, \"wrongUnit\", \"litre\")\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Invalid 'from_type' value: 'wrongUnit'  Supported values are:\r\n    cubic meter, litre, kilolitre, gallon, cubic yard, cubic foot, cup\r\n    \"\"\"\r\n    if from_type not in METRIC_CONVERSION:\r\n        raise ValueError(\r\n            f\"Invalid 'from_type' value: {from_type!r}  Supported values are:\\n\"\r\n            + \", \".join(METRIC_CONVERSION)\r\n        )\r\n    if to_type not in METRIC_CONVERSION:\r\n        raise ValueError(\r\n            f\"Invalid 'to_type' value: {to_type!r}.  Supported values are:\\n\"\r\n            + \", \".join(METRIC_CONVERSION)\r\n        )\r\n    return (\r\n        value\r\n        * METRIC_CONVERSION[from_type].from_factor\r\n        * METRIC_CONVERSION[to_type].to_factor\r\n    )\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    import doctest\r\n\r\n    doctest.testmod()\r\n","sourceCodeStart":51,"sourceCodeEnd":84,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/volume_conversions.py#L51-L84","documentation":"Raised by volume_conversion() in conversions/volume_conversions.py when to_type is not a key in METRIC_CONVERSION (checked after from_type). Same supported set: cubic meter, litre, kilolitre, gallon, cubic yard, cubic foot, cup. Message lists all valid values.","triggerScenarios":"Calling volume_conversion(1, 'litre', 'millilitre'), or targeting 'quart', 'pint', 'tablespoon' — none are in the chart.","commonSituations":"Cooking/measurement apps needing tsp/tbsp/fl oz, which this library lacks; pluralized unit names from natural-language parsing; output unit driven by user preference strings.","solutions":["Restrict targets to the seven supported keys.","Post-convert for missing units: get litres from the library, then multiply by the needed factor (1 litre = 1.05669 US quarts).","Maintain one canonical unit vocabulary in your app and translate at the library boundary."],"exampleFix":"# before\nvolume_conversion(1, 'litre', 'quart')  # ValueError: Invalid 'to_type' value: 'quart'\n\n# after\nlitres = volume_conversion(1, 'litre', 'litre')\nquarts = litres * 1.0566882094325937  # US liquid quart","handlingStrategy":"validation","validationCode":"from conversions.volume_conversions import METRIC_CONVERSION\n\nto_type = ALIASES.get(to_type.lower(), to_type)\nif to_type not in METRIC_CONVERSION:\n    raise ValueError(f'unsupported target volume unit {to_type!r}')\nvolume_conversion(value, from_type, to_type)","typeGuard":"from conversions.volume_conversions import METRIC_CONVERSION\n\ndef is_volume_unit(u: object) -> bool:\n    return isinstance(u, str) and u in METRIC_CONVERSION","tryCatchPattern":"try:\n    volume_conversion(v, f, t)\nexcept ValueError as e:\n    if 'to_type' in str(e):\n        litres = volume_conversion(v, f, 'litre')\n        result = litres * CUSTOM_FACTOR.get(t, 1)\n    else:\n        raise","preventionTips":["Only seven units supported; convert exotic targets via litre yourself","Watch for pluralized labels from NLP or user text","Keep a canonical unit vocabulary and translate once at the edge"],"tags":["python","validation","units","volume","conversion"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}