[1.7.x] Fixed several flake8 errors

Backport of d015c9d11c from master