def maybe_convert_from_base_unit(base_value: str, vartype: str, base_unit: Optional[str]) -> str:
    """Try to convert integer or real value in a base unit to a human-readable unit.

    Value is passed as a string. If parsing or subsequent conversion fails, the original
    value is returned.

    :param base_value: value to be converted from a base unit.
    :param vartype: the target type to parse *base_value* before converting (``integer``
        or ``real`` is expected, any other type results in return value being equal to the
        *base_value* string).
    :param base_unit: unit of *value*. Should be one of the base units (case sensitive):

            * For space: ``B``, ``kB``, ``MB``;
            * For time: ``ms``, ``s``, ``min``.

    :returns: :class:`str` value representing *base_value* converted from *base_unit* to the greatest
        possible human-friendly unit, or *base_value* string if conversion failed.

    :Example:

        >>> maybe_convert_from_base_unit('5', 'integer', 'ms')
        '5ms'

        >>> maybe_convert_from_base_unit('4.2', 'real', 'ms')
        '4200us'

        >>> maybe_convert_from_base_unit('on', 'bool', None)
        'on'

        >>> maybe_convert_from_base_unit('', 'integer', '256MB')
        ''
    """
    converters: Dict[str, Tuple[Callable[[str, Optional[str]], Union[int, float, str, None]],
                                Callable[[Any, Optional[str]], Optional[str]]]] = {
        'integer': (parse_int, convert_int_from_base_unit),
        'real': (parse_real, convert_real_from_base_unit),
        'default': (lambda v, _: v, lambda v, _: v)
    }
    parser, converter = converters.get(vartype, converters['default'])
    parsed_value = parser(base_value, None)
    if parsed_value:
        return converter(parsed_value, base_unit) or base_value
    return base_value
Try to convert integer or real value in a base unit to a human-readable unit.

    Value is passed as a string. If parsing or subsequent conversion fails, the original
    value is returned.

    :param base_value: value to be converted from a base unit.
    :param vartype: the target type to parse *base_value* before converting (``integer``
        or ``real`` is expected, any other type results in return value being equal to the
        *base_value* string).
    :param base_unit: unit of *value*. Should be one of the base units (case sensitive):

            * For space: ``B``, ``kB``, ``MB``;
            * For time: ``ms``, ``s``, ``min``.

    :returns: :class:`str` value representing *base_value* converted from *base_unit* to the greatest
        possible human-friendly unit, or *base_value* string if conversion failed.

    :Example:

        >>> maybe_convert_from_base_unit('5', 'integer', 'ms')
        '5ms'

        >>> maybe_convert_from_base_unit('4.2', 'real', 'ms')
        '4200us'

        >>> maybe_convert_from_base_unit('on', 'bool', None)
        'on'

        >>> maybe_convert_from_base_unit('', 'integer', '256MB')
        ''
    
patroni/utils.py