In this case, the auto-indentation seems confused because the
def __init__() is indented by four spaces, while the others are indented by two spaces. This is valid Python, but the Vim Python indentation code doesn't seem to be advanced enough to deal with this.
In the Python source there's
Tools/scripts/reindent.py, which can fix indentation. When I run it on your example it seems to work as expected.
You can use
'equalprg' to run this with
autocmd FileType python setlocal equalprg=/home/martin/src/Python-3.5.1/Tools/scripts/reindent.py
Some distros also package this script with the main Python package. Debian/Ubuntu puts it in the
There are also some other tools, like autopep8. Personally, I don't like autoformatting tools that do too much, as they break the most important style rule of all: think and do something sane.
I looked at the source of
reindent.py, and it seems like a well-built script (Carpetsmoker seal of approval™). I believe
autopep8 is used quite a lot, so I'll assume that works well enough too.