TheAlgorithms/Python · error · ValueError
Invalid 'to_type' value: {to_type!r}. Supported values are:
Error message
Invalid 'to_type' value: {to_type!r}. Supported values are:
{', '.join(PRESSURE_CONVERSION)} What it means
Raised by pressure_conversion() in conversions/pressure_conversions.py when to_type is not a key in PRESSURE_CONVERSION (checked after from_type passes). Supported target units are the same set: atm, pascal, bar, kilopascal, megapascal, psi, inHg, torr. The ValueError message lists every valid value.
Source
Thrown at conversions/pressure_conversions.py:73
>>> pressure_conversion(4, "psi", "torr")
206.85984
>>> pressure_conversion(1, "inHg", "atm")
0.0334211
>>> pressure_conversion(1, "torr", "psi")
0.019336718261000002
>>> pressure_conversion(4, "wrongUnit", "atm")
Traceback (most recent call last):
...
ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are:
atm, pascal, bar, kilopascal, megapascal, psi, inHg, torr
"""
if from_type not in PRESSURE_CONVERSION:
raise ValueError(
f"Invalid 'from_type' value: {from_type!r} Supported values are:\n"
+ ", ".join(PRESSURE_CONVERSION)
)
if to_type not in PRESSURE_CONVERSION:
raise ValueError(
f"Invalid 'to_type' value: {to_type!r}. Supported values are:\n"
+ ", ".join(PRESSURE_CONVERSION)
)
return (
value
* PRESSURE_CONVERSION[from_type].from_factor
* PRESSURE_CONVERSION[to_type].to_factor
)
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Use an exact supported key for to_type (atm, pascal, bar, kilopascal, megapascal, psi, inHg, torr).
- Keep a single constant tuple of valid units shared by your UI dropdown and the conversion call so they cannot drift.
- Map 'mmHg' -> 'torr' (numerically identical) if mercury units are required.
Example fix
# before pressure_conversion(1, 'atm', 'mmHg') # ValueError: Invalid 'to_type' value: 'mmHg' # after pressure_conversion(1, 'atm', 'torr') # 0.76 (1 mmHg == 1 torr)
Defensive patterns
Strategy: validation
Validate before calling
from conversions.pressure_conversions import PRESSURE_CONVERSION
if to_type not in PRESSURE_CONVERSION:
raise ValueError(f'unsupported target unit {to_type!r}')
pressure_conversion(value, from_type, to_type) Type guard
from conversions.pressure_conversions import PRESSURE_CONVERSION
def is_pressure_unit(u: object) -> bool:
return isinstance(u, str) and u in PRESSURE_CONVERSION Try / catch
try:
pressure_conversion(v, f, t)
except ValueError as e:
if "to_type" in str(e):
raise UserInputError(f'unknown target unit {t!r}') from e
raise Prevention
- Share one constant list of units between UI and converter calls
- Case-sensitive matching: 'inHg' not 'inhg'
- Do not title-case units when rendering labels
When it happens
Trigger: Calling pressure_conversion(1, 'atm', 'mmHg') or pressure_conversion(1, 'atm', 'Bar') — any destination unit misspelled, wrong-cased, or unsupported such as 'mmHg'.
Common situations: Hardcoding a display unit string in a UI that drifted from this library's vocabulary; serializing units through a case-transforming layer (title-casing 'inHg' to 'Inhg'); supporting a unit in your product that this library simply lacks.
Related errors
- Invalid 'from_type' value: {from_type!r} Supported values a
- Incorrect 'from_type' or 'to_type' value: {unit_from!r}, {un
- '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/efd2af6795ea2ff5.
Report an issue: GitHub.