def _roman_to_int(r):
    """ Convert a Roman numeral to an integer. """
    if not isinstance(r, str):
        raise TypeError(f'Expected string, got type(input)')
    r = r.upper()
    nums = {'M': 1000, 'D': 500, 'C': 100, 'L': 50, 'X': 10, 'V': 5, 'I': 1}
    integer = 0
    for i in range(len(r)):
        try:
            value = nums[r[i]]
            if i+1 < len(r) and nums[r[i + 1]] > value:
                integer -= value
            else:
                integer += value
        except KeyError:
            raise ValueError('Input is not a valid Roman numeral: %s' % r)
    return integer