Can't pass flake8-import-order without raising I201 and I202

Question:

I’m currently contributing to a cool little python project, namey PhotoCollage, and the maintainer requests contributers to pass flake8 checks before considering pull requests (fair enough).

My problem is that I can’t get these checks to pass: on my computer, no complaint. But on Travis, I always get the following errors:

$ flake8 .
./photocollage/gtkgui.py:29:1: I202 Additional newline in a section of imports.
./photocollage/gtkgui.py:32:1: I202 Additional newline in a section of imports.
./photocollage/render.py:25:1: I202 Additional newline in a section of imports.

The command "flake8 ." exited with 1.

However, my code looks like this:

gtkgui.py

# -*- coding: utf-8 -*-
# Copyright (C) 2013 Adrien Vergé
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import copy
import gettext
from io import BytesIO
import math
import os.path
import random
import sys

import cairo
import gi
gi.require_version('Gtk', '3.0')  # noqa
from gi.repository import Gtk, Gdk, GObject, GdkPixbuf
from six.moves import urllib  # Python 2 backward compatibility

from photocollage import APP_NAME, artwork, collage, render
from photocollage.render import PIL_SUPPORTED_EXTS as EXTS


gettext.textdomain(APP_NAME)
_ = gettext.gettext
_n = gettext.ngettext

(...)

I did not change the imports wrt. previous commits, so I suspect flake8-import-order changed somehow. Any idea?

Asked By: Joël

||

Answers:

To get these errors on your computer

Try using up-to-date versions of flake8 and flake8-import-order:

pip3 install --user --upgrade flake8 flake8-import-order

Also since this is Python 3 project, are you running flake8 with Python 3? Depending on your OS, the command to run could be:

flake8 .

or

python3 -m flake8 .

About these new errors

You are right: flake8-import-order seems to have changed recently, and now detect false positives. There’re due to the weird way of gi.repository to set imports versions (gi.require_version() has to be called between imports).

I guess there’s not much you can do except disabling these flake8 rules on those specific lines:

import gi
gi.require_version('Gtk', '3.0')  # noqa: E402
from gi.repository import Gtk, Gdk, GObject, GdkPixbuf  # noqa: I202
Answered By: Adrien Vergé
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.