import re def detect_too_much_indent(code): regex = re.compile( r"^(?P +)\S.*\n(?P=indent)(?P +)\S.*\n(?P=indent)(?P=indent2)(?P +)\S.*", re.MULTILINE, ) result = regex.search(code) return bool(result)