TheAlgorithms/Python · error · ValueError

Invalid 'from_type' value: {from_type!r} Supported values a

Error message

Invalid 'from_type' value: {from_type!r}  Supported values are:
{', '.join(PRESSURE_CONVERSION)}

What it means

Raised by pressure_conversion() in conversions/pressure_conversions.py when from_type is not a key in PRESSURE_CONVERSION. Supported keys are atm, pascal, bar, kilopascal, megapascal, psi, inHg, torr (exact case). The error message embeds the full supported list, so it is self-documenting at runtime.

Source

Thrown at conversions/pressure_conversions.py:68

    0.986923
    >>> pressure_conversion(3, "kilopascal", "bar")
    0.029999991892499998
    >>> pressure_conversion(2, "megapascal", "psi")
    290.074434314
    >>> 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

View on GitHub (pinned to f5988cc097)

Solutions

  1. Use one of the exact keys: atm, pascal, bar, kilopascal, megapascal, psi, inHg, torr.
  2. Lowercase/normalize the unit string and map aliases (e.g. {'pa': 'pascal', 'kpa': 'kilopascal'}) before calling.
  3. If mmHg or other units are needed, convert to torr first (1 mmHg = 1 torr).

Example fix

# before
pressure_conversion(1, 'Pa', 'atm')  # ValueError: Invalid 'from_type' value: 'Pa'

# after
ALIASES = {'pa': 'pascal', 'kpa': 'kilopascal', 'mpa': 'megapascal', 'inhg': 'inHg'}
unit = ALIASES.get('Pa'.lower(), 'Pa')
pressure_conversion(1, unit, 'atm')
Defensive patterns

Strategy: validation

Validate before calling

from conversions.pressure_conversions import PRESSURE_CONVERSION

if from_type not in PRESSURE_CONVERSION:
    raise ValueError(f'unsupported pressure unit {from_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 "from_type" in str(e):
        f = ALIASES.get(f.lower(), f)
        result = pressure_conversion(v, f, t)
    else:
        raise

Prevention

When it happens

Trigger: Calling pressure_conversion(1, 'wrongUnit', 'atm'), using 'Pa' or 'PSI' instead of exact 'pascal'/'psi', or a typo like 'inhg' vs 'inHg'.

Common situations: Mapping user-facing unit names (e.g. 'mmHg', 'atmosphere') directly to this API without a translation table; case mismatches; unit catalogs from another library that use SI symbols (Pa, bar, kPa) instead of these spellings.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/bb17cad10f791d7f. Report an issue: GitHub.